haskell for scala-ists

21
Haskell for Scala-ists Chris Eidhof

Upload: chriseidhof

Post on 06-May-2015

1.296 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Haskell for Scala-ists

Haskell for Scala-istsChris Eidhof

Page 2: Haskell for Scala-ists

Three things

• Datatypes

• Purity

• Laziness

Page 3: Haskell for Scala-ists

List

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

Type

Type-parameter

Constructor

Constructor

Case distinction

Page 4: Haskell for Scala-ists

Functions

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

• reverse [] = []

• reverse (x:xs) = reverse xs ++ [x]

Give me a list of a And I’ll return alist of a

Pattern Matching

Page 5: Haskell for Scala-ists

Purity

• State = Evil

Page 6: Haskell for Scala-ists

Purity

• Same Input

• =

• Same output

Referential

Transparency

Page 7: Haskell for Scala-ists

Purity

• No side effects:

• * Variables / Mutable state

• * I/O

• * launchMissiles()

Page 8: Haskell for Scala-ists

Example

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

How do we know sort doesn’t launch missiles?

Page 9: Haskell for Scala-ists

• putStrLn :: String -> IO ()

The IO type shows us it’s not pure

Doing I/O

Page 10: Haskell for Scala-ists

Doing I/O

(>>=) :: IO a -> (a -> IO b) -> IO b

main = putStrLn "hello! what's your name?" >>= \() -> getLine >>= \name -> putStrLn ("hello, " ++ name)

Page 11: Haskell for Scala-ists

Doing I/O

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

Page 12: Haskell for Scala-ists

Fusion

• myFunction = map square . map toInt

• = map (square . toInt)

Page 13: Haskell for Scala-ists

Quickcheck

• Automatic testing of pure code.

Page 14: Haskell for Scala-ists

Parallel code

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

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

Page 15: Haskell for Scala-ists

Composability

• Pure code is easy to compose, it is

• * Stateless

• * Self-contained

Page 16: Haskell for Scala-ists

Effects

Dangerous SafeUseless

UsefulMost

languages

Haskell

Nirvana

Simon Peyton-Jones, Caging The Effects Monster

Page 17: Haskell for Scala-ists

Laziness

• if (x < 10 && x > 5)

Page 18: Haskell for Scala-ists

Laziness: Quicksort

qsort [] = []qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)

minimum ls = head (quickSort ls)

Page 19: Haskell for Scala-ists

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

Page 21: Haskell for Scala-ists

Keep in touch

• http://github.com/chriseidhof

• @chriseidhof