Upgrade to Pro — share decks privately, control downloads, hide ads and more …

San Diego Ruby: A Tour of Haskell

xrl
July 05, 2012

San Diego Ruby: A Tour of Haskell

A Rubyist's Take on Functional Programming

xrl

July 05, 2012
Tweet

Other Decks in Technology

Transcript

  1. Xavier • Ruby hacker • Detour to Erlang hacking •

    Drank the functional Kool-Aid • Went back to Ruby • Tried some Haskell
  2. • You manipulate values not state • Functions are values

    • Recursion is idiomatic Well, a language is functional if...
  3. Haskell is a functional language and... • All the pieces

    fit together before it runs • “strongly-typed” • Work is only done when you need it • “lazy”, “demand-driven” • Don’t need all the arguments • “currying”
  4. Other features of Haskell • The Glasgow Haskell Compiler •

    optimizing compiler • emits standalone executables • or libraries
  5. Other features of Haskell (cont.) • Hackage • Large library

    ecosystem • Cabal build system • Standardized • Ubiquitous • Manage dependencies
  6. Some Types 3 :: Integer "3" :: String "3" ::

    [Char] 1.0 :: Float True :: Bool False :: Bool [1,2,3] :: [Integer]
  7. data List a = Nil | Cons a (List a)

    cons :: a -> List a -> List a cons x xs = Cons x xs
  8. data List a = Nil | Cons a (List a)

    Nil => Nil cons 1 Nil => Cons 1 Nil cons 2 (cons 1 Nil) => Cons 2 (Cons 1 Nil) cons 3 (cons 2 (cons 1 Nil)) => Cons 3 (Cons 2 (Cons 1 Nil))
  9. data List a = Nil | Cons a (List a)

    length :: List a -> Integer length Nil = 0 length Cons _ rest = 1 + (length rest)
  10. length (Cons 3 (Cons 2 (Cons 1 Nil))) 1 +

    (length (Cons 2 (Cons 1 Nil))) 1 + 1 + (length (Cons 1 Nil)) 1 + 1 + 1 + (length Nil) 1 + 1 + 1 + 0 2 + 1 + 0 3 + 0 3
  11. A Note On Laziness • In ruby • Elements of

    list must be built first • In Haskell • Elements of list are built when needed
  12. zipWith :: (a -> b -> c) -> [a] ->

    [b] -> [c] (+) :: Num a => a -> a -> a zipWith (+) [10,11,12] [1..] [11,13,15] zipWith (,) [10,11,12] [1..] [(10,1),(11,2),(12,3)]
  13. data FireShow = Expl ExplType Dur | Rest Dur --

    sequential | FireShow :+: FireShow -- parallel | FireShow :=: FireShow
  14. type Dur = Integer data ExplType = Sparkle | Bang

    | Red | White | Blue | SmileyFace
  15. sanDiegoShow :: FireShow sanDiegoShow = (((Expl Sparkle 5) :=: (Expl

    Red 10)) :=: ((Expl Bang 7) :=: (Expl Sparkle 4))) :=: (((Rest 10) :=: (Rest 10))) :=: ((Expl Red 10) :=: (Expl White 10))
  16. data FireShow = Expl ExplType Dur | Rest Dur --

    sequential | FireShow :+: FireShow -- parallel | FireShow :=: FireShow duration :: FireShow -> Dur duration (Expl _ dur) = dur duration (Rest dur) = dur duration (lShow :+: rShow) = (duration lShow) + (duration rShow) duration (lShow :=: rShow) = greaterOf (duration lShow) (duration rShow)
  17. -- greaterOf :: Dur -> Dur -> Dur greaterOf lDur

    rDur | lDur > rDur = lDur | otherwise = rDur
  18. sanDiegoShow :: FireShow sanDiegoShow = (((Expl Sparkle 5) :=: (Expl

    Red 10)) :=: ((Expl Bang 7) :=: (Expl Sparkle 4))) :=: (((Rest 10) :=: (Rest 10))) :=: ((Expl Red 10) :=: (Expl White 10)) duration sanDiegoShow => 10
  19. xaviersShow :: FireShow xaviersShow = (((Expl Sparkle 5) :=: (Expl

    Red 10)) :+: ((Expl Bang 7) :=: (Expl Sparkle 4))) :+: (((Rest 10) :=: (Rest 10))) :+: ((Expl Red 10) :=: (Expl White 10)) duration xaviersShow => 37
  20. Is laziness worth it? • Instead of doing unused work

    your runtime does book-keeping • Now in Ruby 1.9.2+ • Not quite as idiomatic identity = lambda{|x,y| x+y}.curry.(0)
  21. Haskell is a big subject • Takes time to sink

    in • Be patient • And remember your goal...