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

Functions in PureScript

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

wuct

August 09, 2018
Tweet

More Decks by wuct

Other Decks in Programming

Transcript

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

    String → Number A function with multiple → is a curried function String → String → String
  2. 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
  3. 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 $$>>>
  4. Exercise length (groupBy productCategory (filter isInStock products)) $-- equals length

    $ groupBy productCategory $ filter isInStock $ products $-- equals ? # ? # ? # ?
  5. 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
  6. You don’t need new syntaxes if you have infix operators!

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

    (+) 1 1 add 1 1 $-- equals 1 `add` 1
  8. “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]
  9. fib $:: Int $-> Int fib 0 = 1 fib

    1 = 1 fib n = fib (n - 1) + fib (n - 2) https://en.wikipedia.org/wiki/Fibonacci_number
  10. 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)
  11. “if … then … else” is an expression fact'' $::

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

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

    $== 0 = 1 | otherwise = n * fact (n - 1)
  14. “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)