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

PureScript Lunch-n-Learn Lesson 6

PureScript Lunch-n-Learn Lesson 6

Slides for the sixth lesson in my PureScript lunch-n-learn series. This lesson takes a closer look at pattern matching, starting at the top of chapter 5 of "PureScript by Example."

Jim Fitzpatrick

August 04, 2016
Tweet

More Decks by Jim Fitzpatrick

Other Decks in Programming

Transcript

  1. Last lesson’s “homework” count :: forall a. (a -> Boolean)

    -> Array a -> Int count = count' 0 where count' acc _ [] = acc count' acc p xs = count' (acc + (if p (unsafePartial head xs) then 1 else 0)) p (unsafePartial tail xs)
  2. • Focus on pattern matching, covering simple cases, guards, arrays,

    and records • Solve related problems in an interactive, “dojo” style Goals
  3. This lesson is built upon Ch. 5 of PureScript by

    Example. Be sure to check it out: https://leanpub.com/purescript/read#leanpub-auto-pattern-matching Note
  4. • Commonly used in functional programming • Allows for functions

    to be defined in terms of declarative cases of input • Maps well to mathematical definitions • Cases are tried in order Pattern Matching
  5. Pattern Matching gcd :: Int -> Int -> Int gcd

    n 0 = n gcd 0 m = m gcd n m = if n > m then gcd (n - m) m else gcd n (m - n)
  6. Pattern Matching gcd :: Int -> Int -> Int gcd

    n 0 = n gcd 0 m = m gcd n m = if n > m then gcd (n - m) m else gcd n (m - n) Cases are matched in order
  7. Pattern Matching gcd :: Int -> Int -> Int gcd

    n 0 = n gcd 0 m = m gcd n m = if n > m then gcd (n - m) m else gcd n (m - n) Cases are matched in order Cases can bind names to values
  8. Pattern Matching: Literal cases stringToBool :: String -> Boolean stringToBool

    "true" = true stringToBool _ = false boolToString :: Boolean -> String boolToString true = "true" boolToString false = "false"
  9. Pattern Matching: Literal cases stringToBool :: String -> Boolean stringToBool

    "true" = true stringToBool _ = false boolToString :: Boolean -> String boolToString true = "true" boolToString false = "false" Can match on literal values
  10. Pattern Matching: Literal Cases stringToBool :: String -> Boolean stringToBool

    "true" = true stringToBool _ = false boolToString :: Boolean -> String boolToString true = "true" boolToString false = "false" Can match on literal values _ is a special wildcard to match any value without binding a name
  11. Pattern Matching: guards -- gcd rewritten to use guards gcd'

    :: Int -> Int -> Int gcd' n 0 = n gcd' 0 m = m gcd' n m | n > m = gcd' (n - m) m | otherwise = gcd' n (m - n)
  12. Pattern Matching: guards -- gcd rewritten to use guards gcd'

    :: Int -> Int -> Int gcd' n 0 = n gcd' 0 m = m gcd' n m | n > m = gcd' (n - m) m | otherwise = gcd' n (m - n) Guards use `|` to place additional constraints on the case
  13. Pattern Matching: guards -- gcd rewritten to use guards gcd'

    :: Int -> Int -> Int gcd' n 0 = n gcd' 0 m = m gcd' n m | n > m = gcd' (n - m) m | otherwise = gcd' n (m - n) Guards use `|` to place additional constraints on the case Special keyword to match anything else
  14. Pattern Matching: arrays isEmpty :: Array a -> Boolean isEmpty

    [] = true isEmpty _ = false Matches arrays with no elements
  15. Pattern Matching: arrays isEmpty :: Array a -> Boolean isEmpty

    [] = true isEmpty _ = false Matches arrays with no elements Matches any other array
  16. Pattern Matching: arrays > let takeFive [0, 1, a, b,

    _] = a * b takeFive _ = 0 First case matches all arrays of exactly 5 values, where the first 2 values are 0, 1
  17. Pattern Matching: arrays > let takeFive [0, 1, a, b,

    _] = a * b takeFive _ = 0 > takeFive [0, 1, 2, 3, 4] 6 First case matches all arrays of exactly 5 values, where the first 2 values are 0, 1
  18. Pattern Matching: arrays > let takeFive [0, 1, a, b,

    _] = a * b takeFive _ = 0 > takeFive [0, 1, 2, 3, 4] 6 > takeFive [1, 2, 3, 4, 5] 0 First case matches all arrays of exactly 5 values, where the first 2 values are 0, 1
  19. Pattern Matching: arrays > let takeFive [0, 1, a, b,

    _] = a * b takeFive _ = 0 > takeFive [0, 1, 2, 3, 4] 6 > takeFive [1, 2, 3, 4, 5] 0 > takeFive [] 0 First case matches all arrays of exactly 5 values, where the first 2 values are 0, 1
  20. • Patterns can only match arrays of fixed length •

    Use Data.List to match based on head/tail and other splits Pattern Matching: arrays
  21. Pattern Matching: records showPerson :: { first :: String, last

    :: String } -> String showPerson { first: x, last: y } = y <> ", " <> x
  22. Pattern Matching: records showPerson :: { first :: String, last

    :: String } -> String showPerson { first: x, last: y } = y <> ", " <> x Matches records that, at a minimum, contain first and last properties
  23. Pattern Matching: records showPerson :: { first :: String, last

    :: String } -> String showPerson { first: x, last: y } = y <> ", " <> x Matches records that, at a minimum, contain first and last properties Binds those properties to names
  24. Pattern Matching: nesting type Address = { street :: String,

    city :: String } type Person = { name :: String, address :: Address } livesInLA :: Person -> Boolean livesInLA { address: { city: "Los Angeles" } } = true livesInLA _ = false
  25. Pattern Matching: nesting type Address = { street :: String,

    city :: String } type Person = { name :: String, address :: Address } livesInLA :: Person -> Boolean livesInLA { address: { city: "Los Angeles" } } = true livesInLA _ = false Patterns may be arbitrarily nested
  26. Pattern Matching: named sortPair :: Array Int -> Array Int

    sortPair arr@[x, y] | x <= y = arr | otherwise = [y, x] sortPair arr = arr
  27. Pattern Matching: named sortPair :: Array Int -> Array Int

    sortPair arr@[x, y] | x <= y = arr | otherwise = [y, x] sortPair arr = arr Any pattern may be “named” by using the @ symbol
  28. Pattern Matching: named sortPair :: Array Int -> Array Int

    sortPair arr@[x, y] | x <= y = arr | otherwise = [y, x] sortPair arr = arr Any pattern may be “named” by using the @ symbol Matches arrays with two elements
  29. Pattern Matching: named sortPair :: Array Int -> Array Int

    sortPair arr@[x, y] | x <= y = arr | otherwise = [y, x] sortPair arr = arr Any pattern may be “named” by using the @ symbol Matches arrays with two elements Because of the named pattern, only this case allocates a new array in memory