Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

1,287

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
1,287
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
5
0
Likes
1
Embeds 0
No embeds

No notes for slide

• ### Transcript

• 1. Haskell for Scala-ists Chris Eidhof
• 2. Three things • Datatypes • Purity • Laziness
• 3. List • data [a] = [] | a : [a]
• 4. List • data [a] = [] | a : [a] Type
• 5. List Type-parameter • data [a] = [] | a : [a] Type
• 6. List Type-parameter • data [a] = [] | a : [a] Type Constructor
• 7. List Type-parameter • data [a] = [] | a : [a] Case distinction Type Constructor
• 8. List Type-parameter Constructor • data [a] = [] | a : [a] Case distinction Type Constructor
• 9. Functions • reverse :: [a] -> [a] • reverse [] = [] • reverse (x:xs) = reverse xs ++ [x]
• 10. Functions Give me a list of a • reverse :: [a] -> [a] • reverse [] = [] • reverse (x:xs) = reverse xs ++ [x]
• 11. Functions Give me a list of a And I’ll return a list of a • reverse :: [a] -> [a] • reverse [] = [] • reverse (x:xs) = reverse xs ++ [x]
• 12. Functions Give me a list of a And I’ll return a list of a • reverse :: [a] -> [a] • reverse [] = [] • reverse (x:xs) = reverse xs ++ [x] Pattern Matching
• 13. Purity • State = Evil
• 14. Purity • Same Input • = • Same output
• 15. Purity • Same Input • ntial Refere cy Transp aren = • Same output
• 16. Purity • No side effects: • * Variables / Mutable state • * I/O • * launchMissiles()
• 17. Example • sort :: [Int] -> [Int] How do we know sort doesn’t launch missiles?
• 18. Doing I/O • putStrLn :: String -> IO ()
• 19. Doing I/O • putStrLn :: String -> IO () The IO type shows us it’s not pure
• 20. Doing I/O (>>=) :: IO a -> (a -> IO b) -> IO b main = putStrLn "hello! what's your name?" >>= () -> getLine >>= name -> putStrLn ("hello, " ++ name)
• 21. Doing I/O (>>=) :: IO a -> (a -> IO b) -> IO b main = putStrLn "hello! what's your name?" >>= () -> getLine >>= name -> putStrLn ("hello, " ++ name)
• 22. Doing I/O main = do putStrLn "hello! what's your name?" name <- getLine putStrLn ("hello, " ++ name)
• 23. Fusion • myFunction = map square . map toInt
• 24. Fusion • myFunction = map square . map toInt • = map (square . toInt)
• 25. Quickcheck • Automatic testing of pure code.
• 26. Parallel code • map  :: (a -> b) -> [a] -> [b] • parMap :: (a -> b) -> [a] -> [b]
• 27. Composability • Pure code is easy to compose, it is • * Stateless • * Self-contained
• 28. Effects Useful Most languages Haskell Useless Dangerous Safe Simon Peyton-Jones, Caging The Effects Monster
• 29. Effects Useful Most Nirvana languages Haskell Useless Dangerous Safe Simon Peyton-Jones, Caging The Effects Monster
• 30. Effects Useful Most Nirvana languages Haskell Useless Dangerous Safe Simon Peyton-Jones, Caging The Effects Monster
• 31. Laziness • if (x < 10 && x > 5)
• 32. Laziness: Quicksort qsort [] = [] qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs) minimum ls = head (quickSort ls)
• 33. Laziness: Quicksort qsort [] = [] qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs) minimum ls = head (quickSort ls)