Start ghci
ghci
List manipulation:
head [1,2,3] 1
tail [1,2,3,4,5] [2,3,4,5]
[1,2,3,4,5] !! 2 3 *Note: !! is O(n). Not recommended!
take 3 [1,2,3,4,5] [1,2,3]
drop 3 [1,2,3,4,5] [4,5]
length [1,2,3,4,5] 5 *Note: length is O(n).
sum [1,2,3,4,5] 15
product [1,2,3,45] 120
[1,2,3] ++ [4,5] [1,2,3,4,5]
reverse [1,2,3,4,5] [5,4,3,2,1]
Sample functions:
double x = x + x
quadruple x = double (double x)
factorial n = product [1..n]
average ns = sum ns div
length ns
Note: x f
y is equivalent to f x y
To load a file:
ghci test.hs
To reload
:reload
To find type of an expression
:type expr
To quit ghci
:quit