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

PureScript Lunch-n-Learn Lesson 3

PureScript Lunch-n-Learn Lesson 3

Slides for the third lesson in my PureScript lunch-n-learn series. The topic is "Recursion, Maps, & Folds," and the material builds upon chapter 4 of "PureScript by Example." Given the foundational nature of this chapter, I've broken it down into multiple parts. This first installment focuses on simple recursion and map.

Jim Fitzpatrick

July 14, 2016
Tweet

More Decks by Jim Fitzpatrick

Other Decks in Programming

Transcript

  1. PureScript Lunch & Learn 3: Recursion, Maps & Folds (pt.

    1) github.com/jimf/purescript-lunchnlearn
  2. • Take a closer look at recursion and map •

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

    Example. Be sure to check it out: https://leanpub.com/purescript/read#leanpub-auto-recursion-maps-and-folds Note
  4. • Divide and conquer • Commonly used in functional programming

    • Frequently implemented with pattern matching • Remember: we don’t have for / while / et al constructs for looping Recursion
  5. • Divide and conquer • Commonly used in functional programming

    • Frequently implemented with pattern matching • Remember: we don’t have for / while / et al constructs for looping Recursion
  6. • Divide and conquer • Commonly used in functional programming

    • Frequently implemented with pattern matching • Remember: we don’t have for / while / et al constructs for looping Recursion
  7. • Divide and conquer • Commonly used in functional programming

    • Frequently implemented with pattern matching • Remember: we don’t have for / while / et al constructs for looping Recursion
  8. Example: factorial -- Factorial via recursion. fact :: Int ->

    Int fact n = if n == 0 then 1 else n * fact (n - 1)
  9. Example: factorial -- Factorial via recursion. fact :: Int ->

    Int fact 0 = 1 fact n = n * fact (n - 1)
  10. Example: factorial -- Factorial via recursion. fact :: Int ->

    Int fact 0 = 1 fact n = n * fact (n - 1) Pattern matching
  11. Example: factorial -- Factorial via recursion. fact :: Int ->

    Int fact 0 = 1 fact n = n * fact (n - 1) Pattern matching Function application has higher precedence than math operators
  12. Example: fibonnacci -- Fibonnacci via recursion. fib :: Int ->

    Int fib 0 = 1 fib 1 = 1 fib n = fib (n - 1) + fib (n - 2)
  13. • Applies a function over the value(s) of structure •

    Retains the original “shape” of the structure • Applies to much more than just arrays/lists • Formal type: ◦ forall a b f. (Functor f) => (a -> b) -> f a -> f b Maps
  14. • Applies a function over the value(s) of structure •

    Retains the original “shape” of the structure • Applies to much more than just arrays/lists • Formal type: ◦ forall a b f. (Functor f) => (a -> b) -> f a -> f b Maps
  15. • Applies a function over the value(s) of structure •

    Retains the original “shape” of the structure • Applies to much more than just arrays/lists • Formal type: ◦ forall a b f. (Functor f) => (a -> b) -> f a -> f b Maps
  16. • Applies a function over the value(s) of structure •

    Retains the original “shape” of the structure • Applies to much more than just arrays/lists • Formal type: ◦ forall a b f. (Functor f) => (a -> b) -> f a -> f b Maps
  17. • Anything that can be “mapped” over • Examples: Array,

    List, Maybe, etc. • It’s a little more complicated than this • Lets not get complicated. Functors (in 10 seconds)
  18. • Anything that can be “mapped” over • Examples: Array,

    List, Maybe, etc. • It’s a little more complicated than this • Lets not get complicated. Functors (in 10 seconds)
  19. • Anything that can be “mapped” over • Examples: Array,

    List, Maybe, etc. • It’s a little more complicated than this • Lets not get complicated. Functors (in 10 seconds)
  20. • Anything that can be “mapped” over • Examples: Array,

    List, Maybe, etc. • It’s a little more complicated than this • Lets not get complicated. Functors (in 10 seconds)
  21. Maps > import Prelude > map (\n -> n +

    1) [1, 2, 3, 4, 5] [2,3,4,5,6]
  22. Maps > import Prelude > map (\n -> n +

    1) [1, 2, 3, 4, 5] [2,3,4,5,6] > (\n -> n + 1) `map` [1, 2, 3, 4, 5] [2,3,4,5,6]
  23. Maps > import Prelude > map (\n -> n +

    1) [1, 2, 3, 4, 5] [2,3,4,5,6] > (\n -> n + 1) `map` [1, 2, 3, 4, 5] [2,3,4,5,6] > (\n -> n + 1) <$> [1, 2, 3, 4, 5] [2,3,4,5,6]
  24. Maps > import Prelude > map (\n -> n +

    1) [1, 2, 3, 4, 5] [2,3,4,5,6] > (\n -> n + 1) `map` [1, 2, 3, 4, 5] [2,3,4,5,6] > (\n -> n + 1) <$> [1, 2, 3, 4, 5] [2,3,4,5,6] > import Data.Maybe > (\n -> n + 1) <$> Just 42 (Just 43)
  25. Maps > import Prelude > map (\n -> n +

    1) [1, 2, 3, 4, 5] [2,3,4,5,6] > (\n -> n + 1) `map` [1, 2, 3, 4, 5] [2,3,4,5,6] > (\n -> n + 1) <$> [1, 2, 3, 4, 5] [2,3,4,5,6] > import Data.Maybe > (\n -> n + 1) <$> Just 42 (Just 43) > (\n -> n + 1) <$> Nothing Nothing
  26. Maps > import Prelude > map (\n -> n +

    1) [1, 2, 3, 4, 5] [2,3,4,5,6] > (\n -> n + 1) `map` [1, 2, 3, 4, 5] [2,3,4,5,6] > (\n -> n + 1) <$> [1, 2, 3, 4, 5] [2,3,4,5,6] > import Data.Maybe > (\n -> n + 1) <$> Just 42 (Just 43) > (\n -> n + 1) <$> Nothing Nothing > map (map (\n -> n + 1)) (Just [1, 2, 3, 4, 5]) (Just [2,3,4,5,6])