##environment
ghci
brew install ghc cabal-install
2 + 2
2 + -3
2 != 3
2 /= 3
create functions.hs a file with these methods
doubleMe x = x + x
doubleUs x y = doubleMe x + doubleMe y
doubleSmallNumber x = (if x > 100 then x else x*2) + 1
doubleSmallNumber' x = (if x > 100 then x else x*2) + 1
conanO'Brien = "It's a-me, Conan O'Brien!"
:l functions
doubleSmallNumber x = if x > 100 then x else x*2
##lists
[1,2,3,4] ++ [9,10,11,12]
"hello" ++ " " ++ "world"
['c','r'] ++ ['i','s']
'A':" SMALL CAT" //better perfromance
5:[1,2,3,4,5]
1:2:3:[] // same as [1,2,3]
"Abrakadabra" !! 6
let b = [[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3]]
b !! 2
[3,2,1] > [2,10,100]
head [5,4,3,2,1]
tail [5,4,3,2,1]
last [5,4,3,2,1]
init [5,4,3,2,1]
tail []
tail [1]
null []
reverse [5,4,3,2,1]
take 3 [5,4,3,2,1]
take 5 [1,2]
drop 3 [8,4,2,1,5,6] //like skip
minimum [8,4,2,1,5,6]
maximum [8,4,2,1,5,6]
sum [5,2,1,6,3,2,5,7]
product [6,2,1,2]
4 `elem` [3,4,5,6] // same as elem 4 [1,2,3,4]
##ranges
[1..20]
['a'..'z']
['K'..'Z']
last (take 4 ['K'..'Z'])
[2,4..20]
[1,2,4,8,16..100]
[0.1, 0.3 .. 1]
[13,26..]
take 24 [13,26..]
cycle [1,2,3]
cycle "LOL "
take 10 (cycle "LOL ")
repeat 5
replicate 3 10
##list comprehensions
[x*2 | x <- [1..10]]
[x*2 | x <- [1..10], x*2 >= 12]
[ x | x <- [50..100], x `mod` 7 == 3]
boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x] // this is a function
boomBangs [1..100]
[ x | x <- [10..20], x /= 13, x /= 15, x /= 19]
[ x*y | x <- [2,5,10], y <- [8,10,11]]
removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]
removeNonUppercase "IdontLIKEGIRLS"
let xxs = [[1,3,5,2,3,1,2,4,5],[1,2,3,4,5,6,7,8,9],[1,2,4,2,1,6,3,1,3,2,3,6]]
[ [ x | x <- xs, even x ] | xs <- xxs]
##tuples
[(1,2),(8,11),(4,5)]
[(1,2),(8,11),(4,5,6)] // tuples have their own type
fst (8,11)
fst (1,2,3) //only works on pairs!
snd ("Wow", False)
zip [1,2,3,4,5] [5,5,5,5,5]
zip [1 .. 5] ["one", "two", "three", "four", "five"]
zip [5,3,2,6,2,7,2,5,4,6,6] ["im","a","turtle"]
zip [1..] ["apple", "orange", "cherry", "mango"]
let triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ]
let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]
let rightTriangles' = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2, a+b+c == 24]