Function
> meetupPlace m = m.category <> " meetup @" <> m.place
> m = { category: "PureScript", place: "Taiwan" }
> meetupPlace m
"PureScript meetup @Taiwan"
15
Slide 16
Slide 16 text
Types
type Meetup = { category :: String
, place :: String }
16
Slide 17
Slide 17 text
Kinds
> :kind Number
Type
> import Data.List
> :kind List
Type -> Type
> :kind List String
Type
Type Constructor
17
Slide 18
Slide 18 text
Type Signature
Function Name
Function Parameter Types
add :: Number -> Number -> Number
add x y = x + y
Return Type
18
Slide 19
Slide 19 text
Curried Function
addMeetup :: Meetup -> Meetups -> Meetups
addMeetup m a = Cons m a
> :type addMeetup meetup
List
{ category :: String
, place :: String
}
-> List
{ category :: String
, place :: String
}
19
map (Cont.)
> :type map
forall a b f. Functor f => (a -> b) -> f a -> f b
forall a b. (a -> b) -> Array a -> Array b
> show <$> [1, 2, 3, 4, 5]
["1","2","3","4","5"]
30
Slide 31
Slide 31 text
range
infix 8 range as ..
> 1 .. 5
[1, 2, 3, 4, 5]
> show <$> (1 .. 5)
["1","2","3","4","5"]
31
`do` notation
> :paste
… do
… i <- 1 ..3
… pure i
…^D
[1,2,3]
40
Slide 41
Slide 41 text
`do` notation (Cont.)
> :paste
… do
… i <- 1 ..3
… j <- i ..3
… pure [i,j]
…^D
[[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]]
41
Slide 42
Slide 42 text
`do` notation (Cont.)
Let’s refactor the `factor` function with `do` notation.
factors :: Int -> Array (Array Int)
factors n = filter (\xs -> product xs == n) $ do
i <- 1 .. n
j <- i .. n
pure [i, j]
`pure` will be introduced in later Chapter; we can just
understand it easily as returning the corresponding
Type. In this example, we can also write `[[i,j]]`.
42
Slide 43
Slide 43 text
`do` notation (Cont.)
Let’s refactor the `factor` function with `do` notation.
import Control.MonadZero (guard)
factors :: Int -> Array (Array Int)
factors n = do
i <- 1 .. n
j <- i .. n
guard $ i * j == n
pure [i, j]
Those code below `guard` will only be
executed when its result is `true`
43
Slide 44
Slide 44 text
Folds
> import Data.Array (foldl, foldr)
> :type foldl
forall a b. (b -> a -> b) -> b -> Array a -> b
> :type foldr
forall a b. (a -> b -> b) -> b -> Array a -> b
44
Slide 45
Slide 45 text
Folds (Cont.)
> foldl (\acc n -> acc <> show n) "" [1,2,3,4,5]
"12345"
> foldr (\n acc -> acc <> show n) "" [1,2,3,4,5]
"54321"
45
Slide 46
Slide 46 text
Tail Recursion
46
Slide 47
Slide 47 text
Tail Recursion (Cont.)
> f 0 = 0
> f n = 1 + f (n - 1)
> f 10
10
> f 100000
RangeError: Maximum call stack size exceeded
47
Slide 48
Slide 48 text
Tail Recursion (Cont.)
fib :: Int -> Int
fib = fib' 0 1
where
fib' :: Int -> Int -> Int -> Int
fib' acc nx 0 = (acc + nx)
fib' acc nx x = fib' (acc + nx) acc (x - 1)
48
Slide 49
Slide 49 text
Tail Recursion in JS
49
Ref: ES6 compatible table
Slide 50
Slide 50 text
Q & A
50
Slide 51
Slide 51 text
References
• PureScript Tutorial 1 by Ray Shih
• Getting Started with PureScript by Michael Ficarra
• Why the PureScript community uses Bower
• Update from Bower to Psc-Package
• Why You Should Use PureScript
51