$30 off During Our Annual Pro Sale. View Details »

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. A Tour of Haskell
    A Rubyist’s Take on Functional Programming
    Xavier Lange

    View Slide

  2. Xavier
    • Ruby hacker
    • Detour to Erlang hacking
    • Drank the functional Kool-Aid
    • Went back to Ruby
    • Tried some Haskell

    View Slide

  3. You can never understand
    a language until you
    understand at least two.
    – Ron Searle

    View Slide

  4. • You manipulate values not state
    • Functions are values
    • Recursion is idiomatic
    Well, a language is
    functional if...

    View Slide

  5. 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”

    View Slide

  6. Other features of
    Haskell
    • The Glasgow Haskell Compiler
    • optimizing compiler
    • emits standalone executables
    • or libraries

    View Slide

  7. Other features of
    Haskell (cont.)
    • Hackage
    • Large library ecosystem
    • Cabal build system
    • Standardized
    • Ubiquitous
    • Manage dependencies

    View Slide

  8. Ruby
    http://avatarbriefs.blogspot.com/2012/06/duck-friday_15.html

    View Slide

  9. Haskell
    http://playducation.blogspot.com/2010/05/wooden-shape-sorters-are-favourite.html

    View Slide

  10. Haskell Makes a Quiet
    Introduction

    View Slide

  11. #!/usr/bin/env runhaskell
    main = do
    putStrLn "hello SDRuby!"

    View Slide

  12. Interpreter
    #!/usr/bin/env runhaskell
    main = do
    putStrLn "hello SDRuby!"

    View Slide

  13. Defines a
    function
    #!/usr/bin/env runhaskell
    main = do
    putStrLn "hello SDRuby!"

    View Slide

  14. Please,
    ignore :)
    #!/usr/bin/env runhaskell
    main = do
    putStrLn "hello SDRuby!"

    View Slide

  15. A list of
    chars
    #!/usr/bin/env runhaskell
    main = do
    putStrLn "hello SDRuby!"

    View Slide

  16. Function
    Application
    #!/usr/bin/env runhaskell
    main = do
    putStrLn "hello SDRuby!"

    View Slide

  17. Easy, right?

    View Slide

  18. Some Types
    3 :: Integer
    "3" :: String
    "3" :: [Char]
    1.0 :: Float
    True :: Bool
    False :: Bool
    [1,2,3] :: [Integer]

    View Slide

  19. Learn by Starting Over

    View Slide

  20. data Bool = True | False

    View Slide

  21. data Bool = True | False
    Type

    View Slide

  22. data Bool = True | False
    Value
    Constructor

    View Slide

  23. data Bool = True | False
    Value
    Constructor

    View Slide

  24. not :: Bool -> Bool
    not True = False
    not False = True

    View Slide

  25. (&&) :: Bool -> Bool -> Bool
    False && _ = False
    True && x = x

    View Slide

  26. Your first container
    data List a = Nil
    | Cons a (List a)

    View Slide

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

    View Slide

  28. 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))

    View Slide

  29. length :: List a -> Integer

    View Slide

  30. data List a = Nil
    | Cons a (List a)
    length :: List a -> Integer
    length Nil = 0
    length Cons _ rest = 1 + (length rest)

    View Slide

  31. 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

    View Slide

  32. A Note On Laziness
    • In ruby
    • Elements of list must be built first
    • In Haskell
    • Elements of list are built when needed

    View Slide

  33. Case in Point
    zipWith and lazy lists

    View Slide

  34. [1..] :: (Enum t, Num t) => [t]

    View Slide

  35. zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]

    View Slide

  36. 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)]

    View Slide

  37. zipWith (+) [10,11,12] [1,1..]
    [11,12,13]
    zipWith (,) [10,11,12] [1,1..]
    [(10,1),(11,1),(12,1)]

    View Slide

  38. OK, Where Does
    Haskell Really Shine?

    View Slide

  39. Where Does It Sparkle
    and Boom?

    View Slide

  40. data FireShow = Expl ExplType Dur
    | Rest Dur
    -- sequential
    | FireShow :+: FireShow
    -- parallel
    | FireShow :=: FireShow

    View Slide

  41. type Dur = Integer
    data ExplType = Sparkle
    | Bang
    | Red
    | White
    | Blue
    | SmileyFace

    View Slide

  42. 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))

    View Slide

  43. Did You See $125k
    Problem?
    http://www.foxnews.com/us/2012/07/05/san-diego-fireworks-malfunction-in-big-fast-flash/

    View Slide

  44. @benballer, 7/4/2012

    View Slide

  45. 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)

    View Slide

  46. -- greaterOf :: Dur -> Dur -> Dur
    greaterOf lDur rDur
    | lDur > rDur = lDur
    | otherwise = rDur

    View Slide

  47. 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

    View Slide

  48. 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

    View Slide

  49. #BigBayBoom fail
    averted with Haskell?
    News at 11.

    View Slide

  50. In Any Language You
    Model Your Problem
    Domain

    View Slide

  51. Functional Modeling is
    Value-Oriented

    View Slide

  52. Recursive Values Are
    Your Friend

    View Slide

  53. 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)

    View Slide

  54. Haskell is a big subject
    • Takes time to sink in
    • Be patient
    • And remember your goal...

    View Slide

  55. Thanks
    • Feel free to email me, [email protected]
    • Or say hi @tureus

    View Slide