Recollecting Haskell, Part I
(Based on Chapters 1 and 2 of LYH⋆) CIS 352: Programming Languages January 15, 2019
⋆LYH = Learn You a Haskell for Great Good
CIS 352 Recollecting Haskell, Part I January 15, 2019 1 / 24
Recollecting Haskell, Part I (Based on Chapters 1 and 2 of LYH ) - - PowerPoint PPT Presentation
Recollecting Haskell, Part I (Based on Chapters 1 and 2 of LYH ) CIS 352: Programming Languages January 15, 2019 LYH = Learn You a Haskell for Great Good CIS 352 Recollecting Haskell, Part I January 15, 2019 1 / 24 Two (too?) big
⋆LYH = Learn You a Haskell for Great Good
CIS 352 Recollecting Haskell, Part I January 15, 2019 1 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 2 / 24
1
CIS 352 Recollecting Haskell, Part I January 15, 2019 2 / 24
1
2
CIS 352 Recollecting Haskell, Part I January 15, 2019 2 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 3 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 4 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 4 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 5 / 24
[Post:~] jimroyer% ghci GHCi, version 8.6.3: http://www.haskell.org/ghc/ :? for help Loaded GHCi configuration from /Users/jimroyer/.ghci ghci> 2+3 5 ghci> 2*3 6 ghci> 2-3
ghci> 2/3 0.6666666666666666 ghci> :q Leaving GHCi. [Post:~] jimroyer%
CIS 352 Recollecting Haskell, Part I January 15, 2019 6 / 24
ghci> 5 + True <interactive>:2:3: error: (What does all this mean?) No instance for (Num Bool) arising from a use of ‘+’ In the expression: 5 + True In an equation for ‘it’: it = 5 + True
CIS 352 Recollecting Haskell, Part I January 15, 2019 7 / 24
ghci> succ 4 5 ghci> succ 4 * 10 50 ghci> succ (4 * 10) 41 ghci> max 5 3 5 ghci> 1 + max 5 3 6 ghci> max 5 3 + 1 6 ghci> max 5 (3 + 1) 5 ghci> 10 ‘max‘ 23 23 ghci> (+) 3 5 8
CIS 352 Recollecting Haskell, Part I January 15, 2019 8 / 24
ghci> succ 4 5 ghci> succ 4 * 10 50 ghci> succ (4 * 10) 41 ghci> max 5 3 5 ghci> 1 + max 5 3 6 ghci> max 5 3 + 1 6 ghci> max 5 (3 + 1) 5 ghci> 10 ‘max‘ 23 23 ghci> (+) 3 5 8
doubleMe x = x + x
CIS 352 Recollecting Haskell, Part I January 15, 2019 8 / 24
ghci> succ 4 5 ghci> succ 4 * 10 50 ghci> succ (4 * 10) 41 ghci> max 5 3 5 ghci> 1 + max 5 3 6 ghci> max 5 3 + 1 6 ghci> max 5 (3 + 1) 5 ghci> 10 ‘max‘ 23 23 ghci> (+) 3 5 8
doubleMe x = x + x
ghci> :load baby [1 of 1] Compiling Main ( baby.hs, interpreted ) Ok, modules loaded: Main. ghci> doubleMe 5 10 ghci> doubleMe 7.7 15.4
CIS 352 Recollecting Haskell, Part I January 15, 2019 8 / 24
doubleMe x = x + x doubleUs x y = 2*x+2*y doubleUs’ x y = doubleMe x + doubleMe y doubleSmallNumber x = if x > 100 then x else x * 2 doubleSmallNumber’ x = (if x > 100 then x else x * 2)+1 conanO’Brien = "It’s a-me, Conan O’Brien!"
CIS 352 Recollecting Haskell, Part I January 15, 2019 9 / 24
✔ [2,3,5,7,11,13,17,19] ::[Int] ✔ [True,False,False,True] ::[Bool] ✔ [’b’,’o’,’b’,’c’,’a’,’t’] ≡ "bobcat" ::[Char] ✔ [] ::[a] ✔ [[],[1],[2,3],[4,5,6]] ::[[Int]] ✘ [[1],[[2],[3]]] fuss ✘ [2,True,"foo"] fuss
CIS 352 Recollecting Haskell, Part I January 15, 2019 10 / 24
✔ [2,3,5,7,11,13,17,19] ::[Int] ✔ [True,False,False,True] ::[Bool] ✔ [’b’,’o’,’b’,’c’,’a’,’t’] ≡ "bobcat" ::[Char] ✔ [] ::[a] ✔ [[],[1],[2,3],[4,5,6]] ::[[Int]] ✘ [[1],[[2],[3]]] fuss ✘ [2,True,"foo"] fuss
CIS 352 Recollecting Haskell, Part I January 15, 2019 10 / 24
(Actually, a design error.)
CIS 352 Recollecting Haskell, Part I January 15, 2019 11 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 12 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 13 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 13 / 24
∗Or the “closest” number before n that is in the sequence.
CIS 352 Recollecting Haskell, Part I January 15, 2019 14 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 15 / 24
squares :: [Integer] -> [Integer] squares xs = [ x*x | x <- xs ]
CIS 352 Recollecting Haskell, Part I January 15, 2019 16 / 24
squares :: [Integer] -> [Integer] squares xs = [ x*x | x <- xs ]
CIS 352 Recollecting Haskell, Part I January 15, 2019 16 / 24
squares :: [Integer] -> [Integer] squares xs = [ x*x | x <- xs ]
CIS 352 Recollecting Haskell, Part I January 15, 2019 16 / 24
squares :: [Integer] -> [Integer] squares xs = [ x*x | x <- xs ]
CIS 352 Recollecting Haskell, Part I January 15, 2019 16 / 24
squares :: [Integer] -> [Integer] squares xs = [ x*x | x <- xs ]
Example lifted from Phil Wadler.
CIS 352 Recollecting Haskell, Part I January 15, 2019 16 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 17 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 17 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 17 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 17 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 17 / 24
Example lifted from Phil Wadler.
CIS 352 Recollecting Haskell, Part I January 15, 2019 17 / 24
squares :: [Integer] -> [Integer] squares xs = [ x*x | x <- xs ]
= [ x | x <- xs, odd x] f :: [Integer] -> Integer f xs = sum (squares (odds xs)) f’ :: [Integer] -> Integer f’ xs = sum [ x*x | x <- xs, odd x ] Another example lifted from Phil Wadler.
CIS 352 Recollecting Haskell, Part I January 15, 2019 18 / 24
f xs = sum (squares (odds xs))
CIS 352 Recollecting Haskell, Part I January 15, 2019 19 / 24
f xs = sum (squares (odds xs))
CIS 352 Recollecting Haskell, Part I January 15, 2019 19 / 24
f xs = sum (squares (odds xs))
CIS 352 Recollecting Haskell, Part I January 15, 2019 19 / 24
f xs = sum (squares (odds xs))
CIS 352 Recollecting Haskell, Part I January 15, 2019 19 / 24
f xs = sum (squares (odds xs))
CIS 352 Recollecting Haskell, Part I January 15, 2019 19 / 24
f’ xs = sum [ x*x | x <- xs, odd x] f’ [1,2,3]
CIS 352 Recollecting Haskell, Part I January 15, 2019 20 / 24
f’ xs = sum [ x*x | x <- xs, odd x] f’ [1,2,3] = { xs = [1,2,3] } sum [ x*x | x <- [1,2,3], odd x]
CIS 352 Recollecting Haskell, Part I January 15, 2019 20 / 24
f’ xs = sum [ x*x | x <- xs, odd x] f’ [1,2,3] = { xs = [1,2,3] } sum [ x*x | x <- [1,2,3], odd x] = { x=1 }, { x=2 }, { x=3 } sum ([ 1*1 | odd 1 ] ++ [ 2*2 | odd 2 ] ++ [ 3*3 | odd 3 ])
CIS 352 Recollecting Haskell, Part I January 15, 2019 20 / 24
f’ xs = sum [ x*x | x <- xs, odd x] f’ [1,2,3] = { xs = [1,2,3] } sum [ x*x | x <- [1,2,3], odd x] = { x=1 }, { x=2 }, { x=3 } sum ([ 1*1 | odd 1 ] ++ [ 2*2 | odd 2 ] ++ [ 3*3 | odd 3 ]) = sum ([ 1 | True ] ++ [ 4 | False ] ++ [ 9 | True])
CIS 352 Recollecting Haskell, Part I January 15, 2019 20 / 24
f’ xs = sum [ x*x | x <- xs, odd x] f’ [1,2,3] = { xs = [1,2,3] } sum [ x*x | x <- [1,2,3], odd x] = { x=1 }, { x=2 }, { x=3 } sum ([ 1*1 | odd 1 ] ++ [ 2*2 | odd 2 ] ++ [ 3*3 | odd 3 ]) = sum ([ 1 | True ] ++ [ 4 | False ] ++ [ 9 | True]) = sum ([ 1 ] ++ [] ++ [ 9 ])
CIS 352 Recollecting Haskell, Part I January 15, 2019 20 / 24
f’ xs = sum [ x*x | x <- xs, odd x] f’ [1,2,3] = { xs = [1,2,3] } sum [ x*x | x <- [1,2,3], odd x] = { x=1 }, { x=2 }, { x=3 } sum ([ 1*1 | odd 1 ] ++ [ 2*2 | odd 2 ] ++ [ 3*3 | odd 3 ]) = sum ([ 1 | True ] ++ [ 4 | False ] ++ [ 9 | True]) = sum ([ 1 ] ++ [] ++ [ 9 ]) = sum [1,9]
CIS 352 Recollecting Haskell, Part I January 15, 2019 20 / 24
f’ xs = sum [ x*x | x <- xs, odd x] f’ [1,2,3] = { xs = [1,2,3] } sum [ x*x | x <- [1,2,3], odd x] = { x=1 }, { x=2 }, { x=3 } sum ([ 1*1 | odd 1 ] ++ [ 2*2 | odd 2 ] ++ [ 3*3 | odd 3 ]) = sum ([ 1 | True ] ++ [ 4 | False ] ++ [ 9 | True]) = sum ([ 1 ] ++ [] ++ [ 9 ]) = sum [1,9] = 10
CIS 352 Recollecting Haskell, Part I January 15, 2019 20 / 24
import Test.QuickCheck squares :: [Integer] -> [Integer] squares xs = [ x*x | x <- xs ]
= [ x | x <- xs, odd x] f :: [Integer] -> Integer f xs = sum (squares (odds xs)) f’ :: [Integer] -> Integer f’ xs = sum [ x*x | x <- xs, odd x] f_prop :: [Integer] -> Bool f_prop xs = f xs == f’ xs
ghci> :load sumSqOdds [1 of 1] Compiling Main ( sumSqOdds.hs, interpreted ) Ok, modules loaded: Main. ghci> quickCheck f_prop +++ OK, passed 100 tests. ghci>
CIS 352 Recollecting Haskell, Part I January 15, 2019 21 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 22 / 24
✔ fst :: (a,b) -> a fst ("muffin",99) ❀ "muffin" ✔ snd :: (a,b) -> b snd ("muffin",99) ❀ 99 ✘ fst (4.1,True,’a’) ❀ ERROR ✘ snd (4.1,True,’a’) ❀ ERROR ✔ zip :: [a] -> [b] -> [(a,b)] zip [1..5] [’a’,’b’,’c’,’d’,’e’] ❀ [(1,’a’),(2,’b’),(3,’c’),(4,’d’),(5,’e’)] zip [1..] "abcde" ❀ [(1,’a’),(2,’b’),(3,’c’),(4,’d’),(5,’e’)]
CIS 352 Recollecting Haskell, Part I January 15, 2019 23 / 24
CIS 352 Recollecting Haskell, Part I January 15, 2019 24 / 24
triples1 = [(a,b,c) | a <- [1..10], b <- [1..10], c <- [1..10]]
CIS 352 Recollecting Haskell, Part I January 15, 2019 24 / 24
triples1 = [(a,b,c) | a <- [1..10], b <- [1..10], c <- [1..10]] triples2 = [(a,b,c) | a <- [1..10], b <- [a..10], c <- [b..10]]
CIS 352 Recollecting Haskell, Part I January 15, 2019 24 / 24
triples1 = [(a,b,c) | a <- [1..10], b <- [1..10], c <- [1..10]] triples2 = [(a,b,c) | a <- [1..10], b <- [a..10], c <- [b..10]] triples3 = [(a,b,c) | a <- [1..10], b <- [a..10], c <- [b..10], a*a + b*b == c*c]
CIS 352 Recollecting Haskell, Part I January 15, 2019 24 / 24
triples1 = [(a,b,c) | a <- [1..10], b <- [1..10], c <- [1..10]] triples2 = [(a,b,c) | a <- [1..10], b <- [a..10], c <- [b..10]] triples3 = [(a,b,c) | a <- [1..10], b <- [a..10], c <- [b..10], a*a + b*b == c*c] triples4 = [(a,b,c) | a <- [1..10], b <- [a..10], c <- [b..10], a*a + b*b == c*c, a+b+c == 24]
CIS 352 Recollecting Haskell, Part I January 15, 2019 24 / 24