Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Haskell for Scala-ists
        Chris Eidhof
Three things

• Datatypes
• Purity
• Laziness
List


•   data [a] = [] | a : [a]
List


 •   data [a] = [] | a : [a]


Type
List
       Type-parameter



 •   data [a] = [] | a : [a]


Type
List
       Type-parameter



 •   data [a] = [] | a : [a]


Type
               Constructor
List
       Type-parameter



 •   data [a] = [] | a : [a]

                               Case distinction
Type
         ...
List
       Type-parameter
                             Constructor



 •   data [a] = [] | a : [a]

                     ...
Functions

•   reverse :: [a] -> [a]

•   reverse []     = []

•   reverse (x:xs) = reverse xs ++ [x]
Functions
        Give me a list of a



•   reverse :: [a] -> [a]

•   reverse []        = []

•   reverse (x:xs) = rever...
Functions
        Give me a list of a And I’ll return a
                               list of a

•   reverse :: [a] -> [a...
Functions
        Give me a list of a And I’ll return a
                               list of a

•   reverse :: [a] -> [a...
Purity



•   State = Evil
Purity

•    Same Input
•        =
•   Same output
Purity

•             Same Input
•       ntial
 Refere cy
Transp aren
                  =
•           Same output
Purity

• No side effects:
• * Variables / Mutable state
• * I/O
• * launchMissiles()
Example


•   sort :: [Int] -> [Int]


How do we know sort doesn’t launch missiles?
Doing I/O


•   putStrLn :: String -> IO ()
Doing I/O


•   putStrLn :: String -> IO ()


       The IO type shows us it’s not pure
Doing I/O
(>>=)   :: IO a -> (a -> IO b) -> IO b

main = putStrLn "hello! what's your name?"
   >>= ()   -> getLine
   >>=...
Doing I/O
(>>=)   :: IO a -> (a -> IO b) -> IO b

main = putStrLn "hello! what's your name?"
   >>= ()   -> getLine
   >>=...
Doing I/O


main = do
 putStrLn "hello! what's your name?"
 name <- getLine
 putStrLn ("hello, " ++ name)
Fusion



•   myFunction = map square . map toInt
Fusion



•   myFunction = map square . map toInt

•              = map (square . toInt)
Quickcheck


• Automatic testing of pure code.
Parallel code


•   map    :: (a -> b) -> [a] -> [b]

•   parMap :: (a -> b) -> [a] -> [b]
Composability

• Pure code is easy to compose, it is
• * Stateless
• * Self-contained
Effects
 Useful
              Most
            languages




                            Haskell

Useless
          Danger...
Effects
 Useful
              Most            Nirvana
            languages




                            Haskell

Usele...
Effects
 Useful
              Most            Nirvana
            languages

                            Haskell




Usele...
Laziness


•   if (x < 10 && x > 5)
Laziness: Quicksort
qsort []     =    []
qsort (x:xs) =    qsort (filter (< x) xs)
             ++   [x]
             ++  ...
Laziness: Quicksort
qsort []     =    []
qsort (x:xs) =    qsort (filter (< x) xs)
             ++   [x]
             ++  ...
Read more
• Real World Haskell - http://book.realworldhaskell.org/
• Haskell.org - http://haskell.org
• Haskell Café - htt...
Getting Started

• 1. Install the Haskell Platform
• http://hackage.haskell.org/platform/
• 2. Haskell in 10 minutes
• htt...
Keep in touch


• http://github.com/chriseidhof
• @chriseidhof
Upcoming SlideShare
Loading in …5
×

Haskell for Scala-ists

1,914 views

Published on

Slides of a talk I gave about Haskell for scalaists.

  • Be the first to comment

Haskell for Scala-ists

  1. 1. Haskell for Scala-ists Chris Eidhof
  2. 2. Three things • Datatypes • Purity • Laziness
  3. 3. List • data [a] = [] | a : [a]
  4. 4. List • data [a] = [] | a : [a] Type
  5. 5. List Type-parameter • data [a] = [] | a : [a] Type
  6. 6. List Type-parameter • data [a] = [] | a : [a] Type Constructor
  7. 7. List Type-parameter • data [a] = [] | a : [a] Case distinction Type Constructor
  8. 8. List Type-parameter Constructor • data [a] = [] | a : [a] Case distinction Type Constructor
  9. 9. Functions • reverse :: [a] -> [a] • reverse [] = [] • reverse (x:xs) = reverse xs ++ [x]
  10. 10. Functions Give me a list of a • reverse :: [a] -> [a] • reverse [] = [] • reverse (x:xs) = reverse xs ++ [x]
  11. 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. 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. 13. Purity • State = Evil
  14. 14. Purity • Same Input • = • Same output
  15. 15. Purity • Same Input • ntial Refere cy Transp aren = • Same output
  16. 16. Purity • No side effects: • * Variables / Mutable state • * I/O • * launchMissiles()
  17. 17. Example • sort :: [Int] -> [Int] How do we know sort doesn’t launch missiles?
  18. 18. Doing I/O • putStrLn :: String -> IO ()
  19. 19. Doing I/O • putStrLn :: String -> IO () The IO type shows us it’s not pure
  20. 20. Doing I/O (>>=) :: IO a -> (a -> IO b) -> IO b main = putStrLn "hello! what's your name?" >>= () -> getLine >>= name -> putStrLn ("hello, " ++ name)
  21. 21. Doing I/O (>>=) :: IO a -> (a -> IO b) -> IO b main = putStrLn "hello! what's your name?" >>= () -> getLine >>= name -> putStrLn ("hello, " ++ name)
  22. 22. Doing I/O main = do putStrLn "hello! what's your name?" name <- getLine putStrLn ("hello, " ++ name)
  23. 23. Fusion • myFunction = map square . map toInt
  24. 24. Fusion • myFunction = map square . map toInt • = map (square . toInt)
  25. 25. Quickcheck • Automatic testing of pure code.
  26. 26. Parallel code • map  :: (a -> b) -> [a] -> [b] • parMap :: (a -> b) -> [a] -> [b]
  27. 27. Composability • Pure code is easy to compose, it is • * Stateless • * Self-contained
  28. 28. Effects Useful Most languages Haskell Useless Dangerous Safe Simon Peyton-Jones, Caging The Effects Monster
  29. 29. Effects Useful Most Nirvana languages Haskell Useless Dangerous Safe Simon Peyton-Jones, Caging The Effects Monster
  30. 30. Effects Useful Most Nirvana languages Haskell Useless Dangerous Safe Simon Peyton-Jones, Caging The Effects Monster
  31. 31. Laziness • if (x < 10 && x > 5)
  32. 32. Laziness: Quicksort qsort [] = [] qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs) minimum ls = head (quickSort ls)
  33. 33. Laziness: Quicksort qsort [] = [] qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs) minimum ls = head (quickSort ls)
  34. 34. Read more • Real World Haskell - http://book.realworldhaskell.org/ • Haskell.org - http://haskell.org • Haskell Café - http://haskell.org/haskellwiki/Mailing_lists • Planet Haskell - http://planet.haskell.org/ • Haskell reddit - http://haskell.reddit.com •
  35. 35. Getting Started • 1. Install the Haskell Platform • http://hackage.haskell.org/platform/ • 2. Haskell in 10 minutes • http://haskell.org/haskellwiki/ Learn_Haskell_in_10_minutes
  36. 36. Keep in touch • http://github.com/chriseidhof • @chriseidhof

×