Update various examples

* `let` no longer required in PSCi
* Properly emit output, `(Just 1)` vs `Just (1)`
* Remove `sumTwo` as it is too convoluted of an example
This commit is contained in:
Emiel van de Laar 2021-03-22 17:49:10 +01:00
parent 94704901df
commit 45281224fa

View File

@ -84,12 +84,12 @@ world""" -- "Hello\nworld"
-- and purescript-maybe (Data.Maybe) -- and purescript-maybe (Data.Maybe)
-- Safe access return Maybe a -- Safe access return Maybe a
head [1,2,3] -- Just (1) head [1,2,3] -- (Just 1)
tail [3,2,1] -- Just ([2,1]) tail [3,2,1] -- (Just [2,1])
init [1,2,3] -- Just ([1,2]) init [1,2,3] -- (Just [1,2])
last [3,2,1] -- Just (1) last [3,2,1] -- (Just 1)
-- Array access - indexing -- Array access - indexing
[3,4,5,6,7] !! 2 -- Just (5) [3,4,5,6,7] !! 2 -- (Just 5)
-- Range -- Range
1..5 -- [1,2,3,4,5] 1..5 -- [1,2,3,4,5]
length [2,2,2] -- 3 length [2,2,2] -- 3
@ -100,29 +100,28 @@ append [1,2,3] [4,5,6] -- [1,2,3,4,5,6]
-- --
-- 3. Records are JavaScript objects, with zero or more fields, which -- 3. Records are JavaScript objects, with zero or more fields, which
-- can have different types. -- can have different types.
-- In psci you have to write `let` in front of the function to get a book = {title: "Foucault's pendulum", author: "Umberto Eco"}
-- top level binding.
let book = {title: "Foucault's pendulum", author: "Umberto Eco"}
-- Access properties -- Access properties
book.title -- "Foucault's pendulum" book.title -- "Foucault's pendulum"
let getTitle b = b.title getTitle b = b.title
-- Works on all records with a title (but doesn't require any other field) -- Works on all records with a title (but doesn't require any other field)
getTitle book -- "Foucault's pendulum" getTitle book -- "Foucault's pendulum"
getTitle {title: "Weekend in Monaco", artist: "The Rippingtons"} -- "Weekend in Monaco" getTitle {title: "Weekend in Monaco", artist: "The Rippingtons"} -- "Weekend in Monaco"
-- Can use underscores as shorthand -- Can use underscores as shorthand
_.title book -- "Foucault's pendulum" _.title book -- "Foucault's pendulum"
-- Update a record -- Update a record
let changeTitle b t = b {title = t} changeTitle b t = b {title = t}
getTitle (changeTitle book "Ill nome della rosa") -- "Ill nome della rosa" getTitle (changeTitle book "Ill nome della rosa") -- "Ill nome della rosa"
-- --
-- 4. Functions -- 4. Functions
-- In psci's multiline mode -- In PSCi's paste mode
let sumOfSquares :: Int -> Int -> Int sumOfSquares :: Int -> Int -> Int
sumOfSquares x y = x*x + y*y sumOfSquares x y = x*x + y*y
sumOfSquares 3 4 -- 25 sumOfSquares 3 4 -- 25
let myMod x y = x % y
myMod x y = x % y
myMod 3.0 2.0 -- 1.0 myMod 3.0 2.0 -- 1.0
-- Infix application of function -- Infix application of function
3 `mod` 2 -- 1 3 `mod` 2 -- 1
@ -132,48 +131,47 @@ myMod 3.0 2.0 -- 1.0
sumOfSquares 3 4 * sumOfSquares 4 5 -- 1025 sumOfSquares 3 4 * sumOfSquares 4 5 -- 1025
-- Conditional -- Conditional
let abs' n = if n>=0 then n else -n abs' n = if n>=0 then n else -n
abs' (-3) -- 3 abs' (-3) -- 3
-- Guarded equations -- Guarded equations
let abs'' n | n >= 0 = n -- In PSCi's paste mode
| otherwise = -n abs'' n | n >= 0 = n
| otherwise = -n
-- Pattern matching -- Pattern matching
-- Note the type signature, input is a list of numbers. The pattern matching -- Note the type signature, input is a list of numbers. The pattern matching
-- destructures and binds the list into parts. -- destructures and binds the list into parts.
-- Requires purescript-lists (Data.List) -- Requires purescript-lists (Data.List) and purescript-maybe (Data.Maybe)
let first :: forall a. List a -> a first :: forall a. List a -> Maybe a
first (Cons x _) = x first (x : _) = Just x
first (toList [3,4,5]) -- 3 first Nil = Nothing
let second :: forall a. List a -> a first (fromFoldable [3,4,5]) -- (Just 3)
second (Cons _ (Cons y _)) = y
second (toList [3,4,5]) -- 4
let sumTwo :: List Int -> List Int
sumTwo (Cons x (Cons y rest)) = x + y : rest
fromList (sumTwo (toList [2,3,4,5,6])) :: Array Int -- [5,4,5,6]
-- sumTwo doesn't handle when the list is empty or there's only one element in second :: forall a. List a -> Maybe a
-- which case you get an error. second Nil = Nothing
sumTwo [1] -- Failed pattern match second (_ : Nil) = Nothing
second (_ : (y : _)) = Just y
second (fromFoldable [3,4,5]) -- (Just 4)
-- Complementing patterns to match -- Complementing patterns to match
-- Good ol' Fibonacci -- Good ol' Fibonacci
let fib 1 = 1 fib 1 = 1
fib 2 = 2 fib 2 = 2
fib x = fib (x-1) + fib (x-2) fib x = fib (x-1) + fib (x-2)
fib 10 -- 89 fib 10 -- 89
-- Use underscore to match any, where you don't care about the binding name -- Use underscore to match any, where you don't care about the binding name
let isZero 0 = true isZero 0 = true
isZero _ = false isZero _ = false
isZero 9 -- false
-- Pattern matching on records -- Pattern matching on records
let ecoTitle {author = "Umberto Eco", title = t} = Just t ecoTitle {author: "Umberto Eco", title: t} = Just t
ecoTitle _ = Nothing ecoTitle _ = Nothing
ecoTitle book -- Just ("Foucault's pendulum") ecoTitle {title: "Foucault's pendulum", author: "Umberto Eco"} -- (Just "Foucault's pendulum")
ecoTitle {title: "The Quantum Thief", author: "Hannu Rajaniemi"} -- Nothing ecoTitle {title: "The Quantum Thief", author: "Hannu Rajaniemi"} -- Nothing
-- ecoTitle requires both field to type check: -- ecoTitle requires both field to type check:
ecoTitle {title: "The Quantum Thief"} -- Object lacks required property "author" ecoTitle {title: "The Quantum Thief"} -- Object lacks required property "author"
@ -181,13 +179,13 @@ ecoTitle {title: "The Quantum Thief"} -- Object lacks required property "author"
-- Lambda expressions -- Lambda expressions
(\x -> x*x) 3 -- 9 (\x -> x*x) 3 -- 9
(\x y -> x*x + y*y) 4 5 -- 41 (\x y -> x*x + y*y) 4 5 -- 41
let sqr = \x -> x*x sqr = \x -> x*x
-- Currying -- Currying
let myAdd x y = x + y -- is equivalent with myAdd x y = x + y -- is equivalent with
let myAdd' = \x -> \y -> x + y myAdd' = \x -> \y -> x + y
let add3 = myAdd 3 add3 = myAdd 3
:t add3 -- Prim.Int -> Prim.Int :t add3 -- Int -> Int
-- Forward and backward function composition -- Forward and backward function composition
-- drop 3 followed by taking 5 -- drop 3 followed by taking 5
@ -196,7 +194,7 @@ let add3 = myAdd 3
(drop 3 <<< take 5) (1..20) -- [4,5] (drop 3 <<< take 5) (1..20) -- [4,5]
-- Operations using higher order functions -- Operations using higher order functions
let even x = x `mod` 2 == 0 even x = x `mod` 2 == 0
filter even (1..10) -- [2,4,6,8,10] filter even (1..10) -- [2,4,6,8,10]
map (\x -> x + 11) (1..5) -- [12,13,14,15,16] map (\x -> x + 11) (1..5) -- [12,13,14,15,16]