0
Haskell for Scala-ists
        Chris Eidhof
Three things


• Datatypes
• Purity
• Laziness
List
       Type-parameter
                             Constructor



 •   data [a] = [] | 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
•      ntial
 Refere cy
Transparen
                 =
•          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 ()


       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


main = do
 putStrLn "hello! what's your name?"
 name <- getLine
 putStrLn ("hello, " ++ name)
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            Nirvana
            languages




                            Haskell

Usele...
Laziness


•   if (x < 10 && x > 5)
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,307

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,307
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Haskell for Scala-ists"

  1. 1. Haskell for Scala-ists Chris Eidhof
  2. 2. Three things • Datatypes • Purity • Laziness
  3. 3. List Type-parameter Constructor • data [a] = [] | a : [a] Case distinction Type Constructor
  4. 4. 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
  5. 5. Purity • State = Evil
  6. 6. Purity • Same Input • ntial Refere cy Transparen = • Same output
  7. 7. Purity • No side effects: • * Variables / Mutable state • * I/O • * launchMissiles()
  8. 8. Example • sort :: [Int] -> [Int] How do we know sort doesn’t launch missiles?
  9. 9. Doing I/O • putStrLn :: String -> IO () The IO type shows us it’s not pure
  10. 10. Doing I/O (>>=) :: IO a -> (a -> IO b) -> IO b main = putStrLn "hello! what's your name?" >>= () -> getLine >>= name -> putStrLn ("hello, " ++ name)
  11. 11. Doing I/O main = do putStrLn "hello! what's your name?" name <- getLine putStrLn ("hello, " ++ name)
  12. 12. Fusion • myFunction = map square . map toInt • = map (square . toInt)
  13. 13. Quickcheck • Automatic testing of pure code.
  14. 14. Parallel code • map  :: (a -> b) -> [a] -> [b] • parMap :: (a -> b) -> [a] -> [b]
  15. 15. Composability • Pure code is easy to compose, it is • * Stateless • * Self-contained
  16. 16. Effects Useful Most Nirvana languages Haskell Useless Dangerous Safe Simon Peyton-Jones, Caging The Effects Monster
  17. 17. Laziness • if (x < 10 && x > 5)
  18. 18. Laziness: Quicksort qsort [] = [] qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs) minimum ls = head (quickSort ls)
  19. 19. 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 •
  20. 20. 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
  21. 21. Keep in touch • http://github.com/chriseidhof • @chriseidhof
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×