Slide 1

Slide 1 text

Functions in PureScript CT Wu wu_ct wuct

Slide 2

Slide 2 text

Function Type → is an infix type constructor for functions String → Number A function with multiple → is a curried function String → String → String

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Lambda function \x y $-> x + y

Slide 5

Slide 5 text

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 $$>>>

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

You don’t need new syntaxes if you have infix operators! for_ (10 $.. 1) \n $-> log (show n $<> "$$...")

Slide 9

Slide 9 text

From and to infix operators 1 + 1 $-- equals (+) 1 1 add 1 1 $-- equals 1 `add` 1

Slide 10

Slide 10 text

“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]

Slide 11

Slide 11 text

Simple Pattern Matching fact $:: Int $-> Int fact 0 = 1 fact n = n * fact (n - 1)

Slide 12

Slide 12 text

Exercise fib $:: Int $-> Int https://en.wikipedia.org/wiki/Fibonacci_number

Slide 13

Slide 13 text

fib $:: Int $-> Int fib 0 = 1 fib 1 = 1 fib n = fib (n - 1) + fib (n - 2) https://en.wikipedia.org/wiki/Fibonacci_number

Slide 14

Slide 14 text

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)

Slide 15

Slide 15 text

Tail Recursion Optimization https://kangax.github.io/compat-table/es6/#test-proper_tail_calls_(tail_call_optimisation)

Slide 16

Slide 16 text

“if … then … else” is an expression fact'' $:: Int $-> Int fact'' n = if n $== 0 then 1 else n * fact (n - 1)

Slide 17

Slide 17 text

Case expressions fact $:: Int $-> Int fact n = case n of 0 $-> 1 _ $-> n * fact (n - 1)

Slide 18

Slide 18 text

Guards fact $:: Int $-> Int fact n | n $== 0 = 1 | otherwise = n * fact (n - 1)

Slide 19

Slide 19 text

“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)

Slide 20

Slide 20 text

Q & A

Slide 21

Slide 21 text

Learn once, apply everywhere. wu_ct wuct