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

Functions in PureScript

672ba495b3eadfdb0daee60ed354bdff?s=47 wuct
August 09, 2018

Functions in PureScript

Including:

- basic pattern matching
- function application and composition
- where and let bindings
- infix operators and extended infix operators
- guard syntax
- “if ... then ... else” and “case” expressions

672ba495b3eadfdb0daee60ed354bdff?s=128

wuct

August 09, 2018
Tweet

Transcript

  1. Functions in PureScript CT Wu wu_ct wuct

  2. Function Type → is an infix type constructor for functions

    String → Number A function with multiple → is a curried function String → String → String
  3. Universal Quantifiers ∀ (forall) is the universal quantifier and lowercase

    letters stand for type variables ∀ a. a → a 㱺 expresses constraints on type variables ∀ a. Monoid a ⇒ a $-> a $-> a
  4. Lambda function \x y $-> x + y

  5. Infix Operators apply $:: forall a b. (a $-> b)

    $-> a $-> b apply f x = f x infixr 0 apply as $ applyFlipped $:: forall a b. a $-> (a $-> b) $-> b applyFlipped x f = f x infixr 1 applyFlipped as # compose $:: forall a b c. (b $-> c) $-> (a $-> b) $-> a $-> c compose f g x = f (g x) infixr 9 compose as $$<<< composeFlipped $:: forall a b c. (a $-> b) $-> (b $-> c) $-> a $-> c composeFlipped f g = compose g f infixr 9 compose as $$>>>
  6. Exercise length (groupBy productCategory (filter isInStock products)) $-- equals length

    $ groupBy productCategory $ filter isInStock $ products $-- equals ? # ? # ? # ?
  7. length (groupBy productCategory (filter isInStock products)) $-- equals length $

    groupBy productCategory $ filter isInStock $ products $-- equals products # filter isInStock # groupBy productCategory # length $-- equals length $$<<< groupBy productCategory $$<<< filter isInStock $ products $-- equals products # filter isInStock $$>>> groupBy productCategory $$>>> length
  8. You don’t need new syntaxes if you have infix operators!

    for_ (10 $.. 1) \n $-> log (show n $<> "$$...")
  9. From and to infix operators 1 + 1 $-- equals

    (+) 1 1 add 1 1 $-- equals 1 `add` 1
  10. “Extended Infix Operators” are useful for higher-order functions zipWith add

    [1, 2, 3] [4, 5, 6] $-- equals [1, 2, 3] `zipWith add` [4, 5, 6]
  11. Simple Pattern Matching fact $:: Int $-> Int fact 0

    = 1 fact n = n * fact (n - 1)
  12. Exercise fib $:: Int $-> Int https://en.wikipedia.org/wiki/Fibonacci_number

  13. fib $:: Int $-> Int fib 0 = 1 fib

    1 = 1 fib n = fib (n - 1) + fib (n - 2) https://en.wikipedia.org/wiki/Fibonacci_number
  14. Tail Recursion Optimization $-- no tail recursion optimization fact $::

    Int $-> Int fact 0 = 1 fact n = n * fact (n - 1) $-- tail recursion optimization fact' $:: Int $-> Int $-> Int fact' 0 acc = acc fact' n acc = fact’ (n - 1) (acc * n)
  15. Tail Recursion Optimization https://kangax.github.io/compat-table/es6/#test-proper_tail_calls_(tail_call_optimisation)

  16. “if … then … else” is an expression fact'' $::

    Int $-> Int fact'' n = if n $== 0 then 1 else n * fact (n - 1)
  17. Case expressions fact $:: Int $-> Int fact n =

    case n of 0 $-> 1 _ $-> n * fact (n - 1)
  18. Guards fact $:: Int $-> Int fact n | n

    $== 0 = 1 | otherwise = n * fact (n - 1)
  19. “let” and “where” fact $:: Int $-> Int fact 0

    = 1 fact n = let acc = fact (n - 1) in n * acc fact $:: Int $-> Int fact 0 = 1 fact n = n * acc where acc = fact (n - 1)
  20. Q & A

  21. Learn once, apply everywhere. wu_ct wuct