1 What is Haskell?
Haskell is a purely functional programming language- Variables cannot change state
- Functions have no side-effects: as in only io
- Referential Transparency: Calling the same function with the same inputs and receiving the same outputs
- Unless told otherwise, it won't execute a function unless "it really has to"
Combining Referential Transparency and Laziness lets you think about programming as a series of Transformations on Data. This is allegedly cool because it allows for infinite data structures!
Haskell is Statically Typed Language
- Errors are caught at compile time
- Haskell has good Type Inference, or the abillity to infer the type of the variable used, thus not requiring explicit typing.
2 Haskell Operations
- Negative numbers are funny. Always wrap them in parenthses.
- NOT OKAY: 5 * -3
- OKAY: 5 * (-3)
- Boolean Values are Capitalized
- True/False
- Negation
- Boolean: not means negate
- Arithmetic: /= means not equal to
- Type Coercion
- Only exists from int -> float:
- Infix, Prefix, Suffix operations
- Infix: *, +, -, /. Put these between numbers
- Prefix: *, +, -, /. Put these between numbers
- Function Calls
- FunctionName param1 param2 param3
- 2 Parameter Prefix functions can be written as infix functions
- div 92 10 can be written as 92 `div` 10
3 Haskell Data Structures
- Lists
- let or nothing for variable assignment in interpreter (not .hs files)
- Strings are lists of chars: you can perform list functions on strings. Ex: Concatenation
- "hello" ++ " " ++ "world" = "hello world"
- [1,2,3] ++ [4,5,6] = [1,2,3,4,5,6]
- ['w','o'] ++ ['o','t'] = "woot"
- Operators
- ++ - Slower Concatenation, must iterate through entire left side of operator
- : - Faster Concatenation, appends singletons to front of right structure instantly
- [1,2,3] == 1:2:3:[]
- 3:[1,2] == [3,1,2]
- !! - Selects element at specified index
- [1,2,3] !! 2 == 3
- List Comparison: >,<, ==
- >, < will evaluate heads of lists and move its way down.
- [1,2,3] > [1, 2, 1] == True
- [1,2,3] > [1, 2] == True
- [1,2,3] > [1, 2, 3, 4] == False
- [1,2,3] > [1, 2, 4] == False
- == every element must match
- >, < will evaluate heads of lists and move its way down.
- ++ - Slower Concatenation, must iterate through entire left side of operator
- Other functions
- head returns the first element in list
- head [6,5,2] == 6
- tail returns all but the first element in list
- tail [6,5,2] == [5,2]
- last returns last element in list
- last [6,5,2] == 2
- init returns all but the last element in list
- init [6,5,2] == [6,5]
- DO NOT USE PREVIOUS FNS ON EMPTY LIST these are runtime exceptions as they cannot be caught at compile time
- tail [] == *** Exception: Prelude.head: empty list
- length returns length of list
- length [6,5,2] == 3
- null returns true if empty, returns false if not
- null [6,5,2] == False
- reverse returns a reversed list
- reverse [6,5,2] == [2,5,6]
- take returns first n elements from l list
- take 2 [6,5,2] == [6,5]
- take returns first n elements from l list
- take 2 [6,5,2] == [6,5]
- drop returns remainder of list after "dropping" first n elements
- take 2 [6,5,2] == [2]
- maximum returns largest element in list
- minimum returns smallest element in list
- sum returns sum of a list
- sum [6,5,2] == 13
- product returns product of a list of elements
- product [6,5,2] == 60
- elem returns whether or not element exists in a list. usually called as an Infix function
- 2 `elem` [6,5,2] == True
- 10 `elem` [6,5,2] == False
- elem 2 [6,5,2] == True
- cycle returns an infinitely repeating list. Usually used with take to slice it
- take 9 (cycle [1,4,5]) == [1,4,5,1,4,5,1,4,5]
- take 8 (cycle "LOL ") == "LOL LOL "
- repeat returns an infinitely repeating list of one element. Usually used with take to slice it
- take 5 (repeat 3) == [3,3,3,3,3]
- head returns the first element in list
- Ranges can be enumerated with numbers and characters. YOU CAN ALSO SPECIFY STEPS (see examples 4 & 5). You cannot specify non-linear steps [1,2,4,8..100] does not exponentially grow. Can do descending ranges
- [1..20] == [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
- ['a'..'z'] == "abcdefghijklmnopqrstuvwxyz"
- ['K'..'Z'] == "KLMNOPQRSTUVWXYZ"
- [2, 4..20] == [2,4,6,8,10,12,14,16,18,20]
- [3, 6..20] == [3,6,9,12,15,18]
- [6,5..1] == [6,5,4,3,2,1]
- [8,6..1] == [8,6,4,2]
- You can do floating point ranges, but on god, just don't. The floating point percision just ain't there.
- List Comprehensions
- Standard - transform list range
- [x*2 | x <- [1..10]] == [2,4,6,8,10,12,14,16,18,20]
- Conditional - decide condition which list to return
- [x*2 | x <- [1..10], x*2 >= 12]
- Standard - transform list range