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

Demystifying functional programming and what th...

Demystifying functional programming and what that means for learning & teaching

VIDEO: https://www.youtube.com/watch?v=2CWEg64qZnY (Sydney)

Did you ever wonder: Is functional programming hard? Do you have to be a math whiz? What about the jargon? This talk has answers.

Functional programming is sometimes perceived to be unapproachable, with unfamiliar jargon, obscure concepts, and bewildering theories. This seems counter to its main aim, namely to simplify programming and to make programming more widely accessible. In this talk, I like to argue that there is nothing inherently unapproachable or complex in functional programming, at least not beyond the complexity inherent in programming in general. Instead, we need to critically analyse our teaching strategies and ensure that they are appropriate for a broad range of developers. In my experience, the most common pitfalls are (1) to start with abstract concepts instead of with concrete examples and (2) confusing the historic development of a concept with a pedagogically appropriate teaching strategy. A good example of the latter problem is any attempt to explain the use of functors and monads in functional programming by appeal to category theory. Explaining an unfamiliar idea with an even more alien idea is generally a futile endeavour.

We avoid the first problem by leading with concrete examples, which we use to infer recurring patterns of computation and to motivate more abstract language features — for example, by demonstrating how higher-order functions facilitate the removal of duplicate code. We avoid the second problem by focusing on the concrete computational reasons for using a particular concept or language feature; that is, we place the why before the how. For instance, in sample code that requires maintaining shared state, a state transformer monad helps us to remove error-prone plumbing code.

Nevertheless, we have to acknowledge that moving from imperative, object-oriented programming to functional programming requires more effort than learning yet another object-oriented language. The key here is to clearly distinguish new concepts from known ideas that are just presented differently. Some concepts simply have different names (such as structs versus product types), some have different syntax (such as functional application without parenthesis in Haskell), and some are expressed differently (such as while loops versus tail recursive functions). In all cases, we can help learners by establishing a correspondence between the known and the superficially new.

Putting all of this together, teaching and learning functional programming is surprisingly straight forward. Still, we can do even better. Given the importance of working from examples and for students to experiment by quickly exploring a design space, ideas from live programming tighten the feedback loop and provide a distinct improvement for teaching over the classic REPL (read-eval-print loop) introduced with Lisp. I will demonstrate these improvements using Haskell playgrounds in the Haskell for Mac IDE, but the same applies to Swift playgrounds in Apple’s Xcode IDE and the Swift Playgrounds iPad app.

The material presented in this talk is informed by the experience that Gabriele Keller and I accumulated over a decade of teaching Haskell in a variety of courses at UNSW (University of New South Wales) to thousands of students spanning from absolute beginners to experienced developers in postgraduate courses. We experimented with a variety of approaches and performed student surveys to refine our approach over time. We wrote a textbook providing an introduction to computing for first years students and more recently an online Haskell tutorial including screencasts that feature live coding.

This talk was presented at four YOW! Nights in March 2018 in Sydney, Melbourne, Perth & Brisbane: http://nights.yowconference.com.au/archive-2018/yow-night-2018-sydney-manuel-chakravarty-mar-6/

Manuel Chakravarty

March 06, 2018
Tweet

More Decks by Manuel Chakravarty

Other Decks in Programming

Transcript

  1. Manuel M T Chakravarty (Applicative & Tweag I/O) Gabriele Keller

    (UNSW Sydney) Demystifying Functional Programming And what that means for learning & teaching mchakravarty TacticalGrace justtesting.org haskellformac.com
  2. “How do we teach FP to the early majority as

    opposed to early adopters (and innovators).”
  3. “Liberation from the tyranny of syntax: focus on concepts instead

    of language constructs.” —Felleisen et al, The Structure and Interpretation of the Computer Science Curriculum
  4. Beware of Historical Explanations “A monad is a monoid in

    the category of endofunctors.” Category Theory Formal Semantics Functional Programming
  5. Beware of Historical Explanations “A monad is a monoid in

    the category of endofunctors.” Category Theory Formal Semantics Functional Programming We need explanations that can stand on their own
  6. Examples First Why? Example #1 Example #2 Example #n ộ

    Problem statement Example solution ộ
  7. Examples First Why? Example #1 Example #2 Example #n ộ

    Problem statement General concept Example solution ộ
  8. Math Later “If math is useful to a functional programmer,

    functional programming must be able to motivate math.”
  9. Math Later “If math is useful to a functional programmer,

    functional programming must be able to motivate math.”
  10. But the Jargon is Here to Stay Burrito Warm fuzzy

    thing Workflow Computation builder Kleisli triple
  11. But the Jargon is Here to Stay Burrito Warm fuzzy

    thing Workflow Computation builder Kleisli triple Monad
  12. Examples first Generalise & abstract only after demonstrating a need

    Teach design patterns FP abstractions are used in various idiomatic ways
  13. Examples first Generalise & abstract only after demonstrating a need

    Teach design patterns FP abstractions are used in various idiomatic ways Tight feedback loop Examples, stepwise evaluation, playgrounds
  14. Examples first Generalise & abstract only after demonstrating a need

    Teach design patterns FP abstractions are used in various idiomatic ways Tight feedback loop Examples, stepwise evaluation, playgrounds Visualisation can be an effective tool Pure computations lend themselves to visualisation
  15. Examples first Generalise & abstract only after demonstrating a need

    Teach design patterns FP abstractions are used in various idiomatic ways Tight feedback loop Examples, stepwise evaluation, playgrounds Visualisation can be an effective tool Pure computations lend themselves to visualisation
  16. The Trouble with Recursion fix :: (a -> a) ->

    a fix f = let x = f x in x Simple. Elegant. Powerful.
  17. The Trouble with Recursion fix :: (a -> a) ->

    a fix f = let x = f x in x Simple. Elegant. Powerful. And utterly confusing…
  18. How does it work? How can I use it to

    solve a given problem?
  19. First Recursion Example natSum n 㱺 n + (n-1) +

    ⋯ + 2 + 1 + 0 natSum 0 = 0 natSum 1 = 1 + 0 natSum 2 = 2 + 1 + 0 natSum 3 = 3 + 2 + 1 + 0 natSum 4 = 4 + 3 + 2 + 1 + 0 natSum 5 = 5 + 4 + 3 + 2 + 1 + 0 ⋮
  20. First Recursion Example natSum n 㱺 n + (n-1) +

    ⋯ + 2 + 1 + 0 natSum 0 = 0 natSum 1 = 1 + 0 natSum 2 = 2 + 1 + 0 natSum 3 = 3 + 2 + 1 + 0 natSum 4 = 4 + 3 + 2 + 1 + 0 natSum 5 = 5 + 4 + 3 + 2 + 1 + 0 ⋮ we can reuse natSum 4
  21. First Recursion Example natSum n 㱺 n + (n-1) +

    ⋯ + 2 + 1 + 0 natSum 0 = 0 natSum 1 = 1 + natSum 0 natSum 2 = 2 + natSum 1 natSum 3 = 3 + natSum 2 natSum 4 = 4 + natSum 3 natSum 5 = 5 + natSum 4 ⋮
  22. First Recursion Example natSum n 㱺 n + (n-1) +

    ⋯ + 2 + 1 + 0 natSum 0 = 0 natSum 1 = 1 + natSum 0 natSum 2 = 2 + natSum 1 natSum 3 = 3 + natSum 2 natSum 4 = 4 + natSum 3 natSum 5 = 5 + natSum 4 ⋮ from here on all the same with different numbers
  23. First Recursion Example natSum n 㱺 n + (n-1) +

    ⋯ + 2 + 1 + 0 natSum 0 = 0 natSum n = n + natSum (n - 1)
  24. First Recursion Example natSum n 㱺 n + (n-1) +

    ⋯ + 2 + 1 + 0 natSum 0 = 0 natSum n = n + natSum (n - 1) use variable instead of unbound sequence
  25. natSum 0 = 0 natSum n = n + natSum

    (n - 1) natSum 5  㱺 5 + natSum (5 - 1)
  26. natSum 0 = 0 natSum n = n + natSum

    (n - 1) natSum 5  㱺 5 + natSum (5 - 1)        㱺 5 + natSum 4
  27. natSum 0 = 0 natSum n = n + natSum

    (n - 1) natSum 5  㱺 5 + natSum (5 - 1)        㱺 5 + natSum 4        㱺 5 + (4 + natSum (4 - 1))
  28. natSum 0 = 0 natSum n = n + natSum

    (n - 1) natSum 5  㱺 5 + natSum (5 - 1)        㱺 5 + natSum 4        㱺 5 + (4 + natSum (4 - 1))        㱺 5 + (4 + natSum 3)
  29. natSum 0 = 0 natSum n = n + natSum

    (n - 1) natSum 5  㱺 5 + natSum (5 - 1)        㱺 5 + natSum 4        㱺 5 + (4 + natSum (4 - 1))        㱺 5 + (4 + natSum 3)        㱺 5 + (4 + (3 + natSum (3 - 1)))
  30. natSum 0 = 0 natSum n = n + natSum

    (n - 1) natSum 5  㱺 5 + natSum (5 - 1)        㱺 5 + natSum 4        㱺 5 + (4 + natSum (4 - 1))        㱺 5 + (4 + natSum 3)        㱺 5 + (4 + (3 + natSum (3 - 1)))        㱺 5 + (4 + (3 + natSum 2))
  31. natSum 0 = 0 natSum n = n + natSum

    (n - 1) natSum 5  㱺 5 + natSum (5 - 1)        㱺 5 + natSum 4        㱺 5 + (4 + natSum (4 - 1))        㱺 5 + (4 + natSum 3)        㱺 5 + (4 + (3 + natSum (3 - 1)))        㱺 5 + (4 + (3 + natSum 2))        㱺 5 + (4 + (3 + (2 + natSum (2 - 1))))
  32. natSum 0 = 0 natSum n = n + natSum

    (n - 1) natSum 5  㱺 5 + natSum (5 - 1)        㱺 5 + natSum 4        㱺 5 + (4 + natSum (4 - 1))        㱺 5 + (4 + natSum 3)        㱺 5 + (4 + (3 + natSum (3 - 1)))        㱺 5 + (4 + (3 + natSum 2))        㱺 5 + (4 + (3 + (2 + natSum (2 - 1))))        㱺 5 + (4 + (3 + (2 + natSum 1)))
  33. natSum 0 = 0 natSum n = n + natSum

    (n - 1) natSum 5  㱺 5 + natSum (5 - 1)        㱺 5 + natSum 4        㱺 5 + (4 + natSum (4 - 1))        㱺 5 + (4 + natSum 3)        㱺 5 + (4 + (3 + natSum (3 - 1)))        㱺 5 + (4 + (3 + natSum 2))        㱺 5 + (4 + (3 + (2 + natSum (2 - 1))))        㱺 5 + (4 + (3 + (2 + natSum 1)))        㱺 5 + (4 + (3 + (2 + (1 + natSum (1 - 1)))))
  34. natSum 0 = 0 natSum n = n + natSum

    (n - 1) natSum 5  㱺 5 + natSum (5 - 1)        㱺 5 + natSum 4        㱺 5 + (4 + natSum (4 - 1))        㱺 5 + (4 + natSum 3)        㱺 5 + (4 + (3 + natSum (3 - 1)))        㱺 5 + (4 + (3 + natSum 2))        㱺 5 + (4 + (3 + (2 + natSum (2 - 1))))        㱺 5 + (4 + (3 + (2 + natSum 1)))        㱺 5 + (4 + (3 + (2 + (1 + natSum (1 - 1)))))        㱺 5 + (4 + (3 + (2 + (1 + natSum 0))))
  35. natSum 0 = 0 natSum n = n + natSum

    (n - 1) natSum 5  㱺 5 + natSum (5 - 1)        㱺 5 + natSum 4        㱺 5 + (4 + natSum (4 - 1))        㱺 5 + (4 + natSum 3)        㱺 5 + (4 + (3 + natSum (3 - 1)))        㱺 5 + (4 + (3 + natSum 2))        㱺 5 + (4 + (3 + (2 + natSum (2 - 1))))        㱺 5 + (4 + (3 + (2 + natSum 1)))        㱺 5 + (4 + (3 + (2 + (1 + natSum (1 - 1)))))        㱺 5 + (4 + (3 + (2 + (1 + natSum 0))))        㱺 5 + (4 + (3 + (2 + (1 + 0))))
  36. natSum 0 = 0 natSum n = n + natSum

    (n - 1) natSum 5  㱺 5 + natSum (5 - 1)        㱺 5 + natSum 4        㱺 5 + (4 + natSum (4 - 1))        㱺 5 + (4 + natSum 3)        㱺 5 + (4 + (3 + natSum (3 - 1)))        㱺 5 + (4 + (3 + natSum 2))        㱺 5 + (4 + (3 + (2 + natSum (2 - 1))))        㱺 5 + (4 + (3 + (2 + natSum 1)))        㱺 5 + (4 + (3 + (2 + (1 + natSum (1 - 1)))))        㱺 5 + (4 + (3 + (2 + (1 + natSum 0))))        㱺 5 + (4 + (3 + (2 + (1 + 0))))        㱺 15 natSum 5  㱺 5 + natSum (5 - 1)        㱺 5 + natSum 4        㱺 5 + (4 + natSum (4 - 1))        㱺 5 + (4 + natSum 3)        㱺 5 + (4 + (3 + natSum (3 - 1)))        㱺 5 + (4 + (3 + natSum 2))        㱺 5 + (4 + (3 + (2 + natSum (2 - 1))))        㱺 5 + (4 + (3 + (2 + natSum 1)))        㱺 5 + (4 + (3 + (2 + (1 + natSum (1 - 1)))))        㱺 5 + (4 + (3 + (2 + (1 + natSum 0))))        㱺 5 + (4 + (3 + (2 + (1 + 0))))        㱺 15
  37. Example-Driven Development Illustrates the concept (here recursion) Provides a concrete

    problem solving strategy (to get students started on their own code)
  38. Example-Driven Development Illustrates the concept (here recursion) Provides a concrete

    problem solving strategy (to get students started on their own code) Recursion
  39. Example-Driven Development Illustrates the concept (here recursion) Provides a concrete

    problem solving strategy (to get students started on their own code) Recursion Higher-order functions
  40. Example-Driven Development Illustrates the concept (here recursion) Provides a concrete

    problem solving strategy (to get students started on their own code) Recursion Higher-order functions Parametric polymorphism
  41. Example-Driven Development Illustrates the concept (here recursion) Provides a concrete

    problem solving strategy (to get students started on their own code) Recursion Higher-order functions Parametric polymorphism Type classes
  42. Example-Driven Development Illustrates the concept (here recursion) Provides a concrete

    problem solving strategy (to get students started on their own code) Recursion Higher-order functions Parametric polymorphism Type classes Categorial structures
  43. Example-Driven Development Illustrates the concept (here recursion) Provides a concrete

    problem solving strategy (to get students started on their own code) Recursion Higher-order functions Parametric polymorphism Type classes Categorial structures and more
  44. Examples first Generalise & abstract only after demonstrating a need

    Teach design patterns FP abstractions are used in various idiomatic ways Tight feedback loop Examples, stepwise evaluation, playgrounds Visualisation can be an effective tool Pure computations lend themselves to visualisation
  45. allSquares :: Num a => [a] -> [a] allSquares []

    = [] allSquares (x : xs) = x * x : allSquares xs
  46. allSquares :: Num a => [a] -> [a] allSquares []

    = [] allSquares (x : xs) = x * x : allSquares xs allToUpper :: String -> String allToUpper [] = [] allToUpper (chr : restString) = toUpper chr : allToUpper restString
  47. allSquares :: Num a => [a] -> [a] allSquares []

    = [] allSquares (x : xs) = x * x : allSquares xs allToUpper :: String -> String allToUpper [] = [] allToUpper (chr : restString) = toUpper chr : allToUpper restString
  48. allSquares :: Num a => [a] -> [a] allSquares []

    = [] allSquares (x : xs) = x * x : allSquares xs allToUpper :: String -> String allToUpper [] = [] allToUpper (chr : restString) = toUpper chr : allToUpper restString recursiveFunction [] = [] recursiveFunction (x : xs) = doSomethingWith x : recursiveFunction xs
  49. Computational Design Patterns Illustrates a computational idiom (here map-like recursion)

    Provides learners with concrete guidance on how to get started
  50. Examples first Generalise & abstract only after demonstrating a need

    Teach design patterns FP abstractions are used in various idiomatic ways Tight feedback loop Examples, stepwise evaluation, playgrounds Visualisation can be an effective tool Pure computations lend themselves to visualisation
  51. Examples first Generalise & abstract only after demonstrating a need

    Teach design patterns FP abstractions are used in various idiomatic ways Tight feedback loop Examples, stepwise evaluation, playgrounds Visualisation can be an effective tool Pure computations lend themselves to visualisation
  52. “Doesn’t static typing hinder experimentation as you have to get

    everything right, before you can run it?”
  53. natSum 0 = 0 natSum 1 = 1 + 0

    natSum 2 = 2 + 1 + 0 ⋮ Examples first mchakravarty TacticalGrace justtesting.org haskellformac.com
  54. natSum 0 = 0 natSum 1 = 1 + 0

    natSum 2 = 2 + 1 + 0 ⋮ Examples first recursiveFunction [] = [] recursiveFunction (x : xs) = doSomethingWith x : recursiveFunction xs Teach design patterns mchakravarty TacticalGrace justtesting.org haskellformac.com
  55. natSum 0 = 0 natSum 1 = 1 + 0

    natSum 2 = 2 + 1 + 0 ⋮ Examples first recursiveFunction [] = [] recursiveFunction (x : xs) = doSomethingWith x : recursiveFunction xs Teach design patterns Tight feedback loop & visualisation mchakravarty TacticalGrace justtesting.org haskellformac.com
  56. natSum 0 = 0 natSum 1 = 1 + 0

    natSum 2 = 2 + 1 + 0 ⋮ Examples first recursiveFunction [] = [] recursiveFunction (x : xs) = doSomethingWith x : recursiveFunction xs Teach design patterns Tight feedback loop & visualisation http://learninghaskell.com mchakravarty TacticalGrace justtesting.org haskellformac.com
  57. Image Attribution https://pixabay.com/photo-1246043/ https://commons.wikimedia.org/wiki/File:Diffusion_of_ideas.svg https://web.archive.org/web/20140722064822/http://haskell.cs.yale.edu/people/paul-hudak/ https://commons.wikimedia.org/wiki/File:Robert_Harper.jpg https://commons.wikimedia.org/wiki/File:Rich_Hickey.jpg https://commons.wikimedia.org/wiki/File:Alan_Kay_(3097597186).jpg https://commons.wikimedia.org/wiki/File:Edsger_Wybe_Dijkstra.jpg https://harc.github.io/seymour-live2017/

    https://pixabay.com/photo-2536159/ https://pixabay.com/photo-1218797/ https://pixabay.com/photo-3195378/ https://pixabay.com/photo-2245832/ https://commons.wikimedia.org/wiki/File:Eugenio_Moggi.jpg https://commons.wikimedia.org/wiki/File:Wadler2.JPG https://commons.wikimedia.org/wiki/File:Handcuffs01_2008-07-27.jpg https://commons.wikimedia.org/wiki/File:Set_square_Geodreieck.svg https://www.flickr.com/photos/provisions/7986149891/