mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
updated purescript example for compiler version >= 0.7.4.1
This commit is contained in:
parent
070e53dd85
commit
e44d7d47ec
@ -2,194 +2,207 @@
|
|||||||
language: purescript
|
language: purescript
|
||||||
contributors:
|
contributors:
|
||||||
- ["Fredrik Dyrkell", "http://www.lexicallyscoped.com"]
|
- ["Fredrik Dyrkell", "http://www.lexicallyscoped.com"]
|
||||||
|
- ["Thimoteus", "https://github.com/Thimoteus"]
|
||||||
---
|
---
|
||||||
|
|
||||||
PureScript is a small strongly, statically typed language compiling to Javascript.
|
PureScript is a small strongly, statically typed language compiling to Javascript.
|
||||||
|
|
||||||
* Learn more at [http://www.purescript.org/](http://www.purescript.org/)
|
* Learn more at [http://www.purescript.org/](http://www.purescript.org/)
|
||||||
* Documentation: [http://docs.purescript.org/en/latest/](http://docs.purescript.org/en/latest/)
|
* Documentation: [http://pursuit.purescript.org/](http://pursuit.purescript.org/)
|
||||||
* Book: Purescript by Example, [https://leanpub.com/purescript/](https://leanpub.com/purescript/)
|
* Book: Purescript by Example, [https://leanpub.com/purescript/](https://leanpub.com/purescript/)
|
||||||
|
|
||||||
```haskell
|
```purescript
|
||||||
|
|
||||||
--
|
--
|
||||||
-- 1. Primitive datatypes that corresponds to their Javascript
|
-- 1. Primitive datatypes that corresponds to their Javascript
|
||||||
-- equivalents at runtime.
|
-- equivalents at runtime.
|
||||||
|
|
||||||
|
> import Prelude
|
||||||
-- Numbers
|
-- Numbers
|
||||||
1 + 7*5 :: Number -- 36
|
> 1.0 + 7.2*5.5 :: Number -- 40.6
|
||||||
|
-- Ints
|
||||||
|
> 1 + 2*5 :: Int -- 11
|
||||||
-- Types are inferred, so the following works fine
|
-- Types are inferred, so the following works fine
|
||||||
9 / 2.5 + 4.4 -- 8
|
> 9.0/2.5 + 4.4 -- 8.0
|
||||||
|
-- But Ints and Numbers don't mix, so the following won't
|
||||||
|
> 5/2 + 2.5 -- Expression 2.5 does not have type Int
|
||||||
-- Hexadecimal literals
|
-- Hexadecimal literals
|
||||||
0xff + 1 -- 256
|
> 0xff + 1 -- 256
|
||||||
-- Unary negation
|
-- Unary negation
|
||||||
6 * -3 -- -18
|
> 6 * -3 -- -18
|
||||||
6 * negate 3 -- -18
|
> 6 * negate 3 -- -18
|
||||||
-- Modulus
|
-- Modulus, from purescript-math (Math)
|
||||||
3 % 2 -- 1
|
> 3.0 % 2.0 -- 1.0
|
||||||
4 % 2 -- 0
|
> 4.0 % 2.0 -- 0.0
|
||||||
-- Inspect the type of an expression in psci
|
-- Inspect the type of an expression in psci
|
||||||
:t 9 / 2.5 + 4.4 -- Prim.Number
|
> :t 9.5/2.5 + 4.4 -- Prim.Number
|
||||||
|
|
||||||
-- Booleans
|
-- Booleans
|
||||||
true :: Boolean -- true
|
> true :: Boolean -- true
|
||||||
false :: Boolean -- false
|
> false :: Boolean -- false
|
||||||
-- Negation
|
-- Negation
|
||||||
not true --false
|
> not true -- false
|
||||||
23 == 23 -- true
|
> 23 == 23 -- true
|
||||||
1 /= 4 -- true
|
> 1 /= 4 -- true
|
||||||
1 >= 4 -- false
|
> 1 >= 4 -- false
|
||||||
-- Comparisions < <= > >=
|
-- Comparisions < <= > >=
|
||||||
-- are defined in terms of compare
|
-- are defined in terms of compare
|
||||||
compare 1 2 -- LT
|
> compare 1 2 -- LT
|
||||||
compare 2 2 -- EQ
|
> compare 2 2 -- EQ
|
||||||
compare 3 2 -- GT
|
> compare 3 2 -- GT
|
||||||
-- Conjunction and Disjunction
|
-- Conjunction and Disjunction
|
||||||
true && (9 >= 19 || 1 < 2) -- true
|
> true && (9 >= 19 || 1 < 2) -- true
|
||||||
|
|
||||||
-- Strings
|
-- Strings
|
||||||
"Hellow" :: String -- "Hellow"
|
> "Hellow" :: String -- "Hellow"
|
||||||
-- Multiline string
|
-- Multiline string without newlines, to run in psci use the --multi-line-mode flag
|
||||||
"Hellow\
|
> "Hellow\
|
||||||
\orld" -- "Helloworld"
|
\orld" -- "Helloworld"
|
||||||
|
-- Multiline string with newlines
|
||||||
|
> """Hello
|
||||||
|
world""" -- "Hello\nworld"
|
||||||
-- Concatenate
|
-- Concatenate
|
||||||
"such " ++ "amaze" -- "such amaze"
|
> "such " ++ "amaze" -- "such amaze"
|
||||||
|
|
||||||
--
|
--
|
||||||
-- 2. Arrays are Javascript arrays, but must be homogeneous
|
-- 2. Arrays are Javascript arrays, but must be homogeneous
|
||||||
|
|
||||||
[1,1,2,3,5,8] :: [Number] -- [1,1,2,3,5,8]
|
> [1,1,2,3,5,8] :: Array Number -- [1,1,2,3,5,8]
|
||||||
[true, true, false] :: [Boolean] -- [true,true,false]
|
> [true, true, false] :: Array Boolean -- [true,true,false]
|
||||||
-- [1,2, true, "false"] won't work
|
-- [1,2, true, "false"] won't work
|
||||||
-- `Cannot unify Prim.Number with Prim.Boolean`
|
-- `Cannot unify Prim.Int with Prim.Boolean`
|
||||||
-- Cons (prepend)
|
-- Cons (prepend)
|
||||||
1 : [2,4,3] -- [1,2,4,3]
|
> 1 : [2,4,3] -- [1,2,4,3]
|
||||||
|
|
||||||
-- Requires purescript-arrays (Data.Array)
|
-- Requires purescript-arrays (Data.Array)
|
||||||
-- 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)
|
||||||
-- Random access - indexing
|
-- Random 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
|
||||||
drop 3 [5,4,3,2,1] -- [2,1]
|
> drop 3 [5,4,3,2,1] -- [2,1]
|
||||||
take 3 [5,4,3,2,1] -- [5,4,3]
|
> take 3 [5,4,3,2,1] -- [5,4,3]
|
||||||
append [1,2,3] [4,5,6] -- [1,2,3,4,5,6]
|
> 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.
|
||||||
let book = {title: "Foucault's pendulum", author: "Umberto Eco"}
|
-- In psci you have to write `let` in front of the function to get a
|
||||||
|
-- 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"
|
||||||
|
|
||||||
getTitle b = b.title
|
> let 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
|
||||||
|
> _.title book -- "Foucault's pendulum"
|
||||||
-- Update a record
|
-- Update a record
|
||||||
changeTitle b t = b {title = t}
|
> let changeTitle b t = b {title = t}
|
||||||
changeTitle book "Ill nome della rosa" -- {title: "Ill nome della
|
> getTitle (changeTitle book "Ill nome della rosa") -- "Ill nome della rosa"
|
||||||
-- rosa", author: "Umberto Eco"}
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- 4. Functions
|
-- 4. Functions
|
||||||
sumOfSquares x y = x*x+y*y
|
-- In psci's multiline mode
|
||||||
sumOfSquares 3 4 -- 25
|
> let sumOfSquares :: Int -> Int -> Int
|
||||||
-- In psci you have to write `let` in front of the function to get a
|
sumOfSquares x y = x*x + y*y
|
||||||
-- top level binding
|
> sumOfSquares 3 4 -- 25
|
||||||
mod x y = x % y
|
> let myMod x y = x % y
|
||||||
mod 3 2 -- 1
|
> myMod 3.0 2.0 -- 1.0
|
||||||
-- Infix application of function
|
-- Infix application of function
|
||||||
3 `mod` 2 -- 1
|
> 3 `mod` 2 -- 1
|
||||||
|
|
||||||
-- function application have higher precedence than all other
|
-- function application has higher precedence than all other
|
||||||
-- operators
|
-- operators
|
||||||
sumOfSquares 3 4 * sumOfSquares 4 5 -- 1025
|
> sumOfSquares 3 4 * sumOfSquares 4 5 -- 1025
|
||||||
|
|
||||||
-- Conditional
|
-- Conditional
|
||||||
abs' n = if n>=0 then n else -n
|
> let abs' n = if n>=0 then n else -n
|
||||||
abs' (-3) -- 3
|
> abs' (-3) -- 3
|
||||||
|
|
||||||
-- Guarded equations
|
-- Guarded equations
|
||||||
abs n | n >= 0 = n
|
> let abs'' n | n >= 0 = n
|
||||||
| otherwise = -n
|
| otherwise = -n
|
||||||
|
|
||||||
-- Pattern matching
|
-- Pattern matching
|
||||||
|
|
||||||
-- Note the type signature, input is an array of numbers The pattern
|
-- Note the type signature, input is a list of numbers. The pattern matching
|
||||||
-- matching destructures and binds the array into parts
|
-- destructures and binds the list into parts.
|
||||||
first :: [Number] -> Number
|
-- Requires purescript-lists (Data.List)
|
||||||
first (x:_) = x
|
> let first :: forall a. List a -> a
|
||||||
first [3,4,5] -- 3
|
first (Cons x _) = x
|
||||||
second :: [Number] -> Number
|
> first (toList [3,4,5]) -- 3
|
||||||
second (_:y:_) = y
|
> let second :: forall a. List a -> a
|
||||||
second [3,4,5] -- 4
|
second (Cons _ (Cons y _)) = y
|
||||||
sumTwo :: [Number] -> [Number]
|
> second (toList [3,4,5]) -- 4
|
||||||
sumTwo (x:y:rest) = (x+y) : rest
|
> let sumTwo :: List Int -> List Int
|
||||||
sumTwo [2,3,4,5,6] -- [5,4,5,6]
|
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 array is empty or just have one
|
-- sumTwo doesn't handle when the list is empty or there's only one element in
|
||||||
-- element in which case you get an error
|
-- which case you get an error.
|
||||||
sumTwo [1] -- Failed pattern match
|
sumTwo [1] -- Failed pattern match
|
||||||
|
|
||||||
-- Complementing patterns to match
|
-- Complementing patterns to match
|
||||||
-- Good ol' Fibonacci
|
-- Good ol' Fibonacci
|
||||||
fib 1 = 1
|
> let 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
|
||||||
isZero 0 = true
|
> let isZero 0 = true
|
||||||
isZero _ = false
|
isZero _ = false
|
||||||
|
|
||||||
-- Pattern matching on records
|
-- Pattern matching on records
|
||||||
ecoTitle {author = "Umberto Eco", title = t} = Just t
|
> let ecoTitle {author = "Umberto Eco", title = t} = Just t
|
||||||
ecoTitle _ = Nothing
|
ecoTitle _ = Nothing
|
||||||
|
|
||||||
ecoTitle book -- Just ("Foucault's pendulum")
|
> ecoTitle book -- 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 does not have property author
|
> 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
|
||||||
sqr = \x -> x*x
|
> let sqr = \x -> x*x
|
||||||
|
|
||||||
-- Currying
|
-- Currying
|
||||||
add x y = x + y -- is equivalent with
|
> let myAdd x y = x + y -- is equivalent with
|
||||||
add = \x -> (\y -> x+y)
|
> let myAdd' = \x -> \y -> x + y
|
||||||
add3 = add 3
|
> let add3 = myAdd 3
|
||||||
:t add3 -- Prim.Number -> Prim.Number
|
> :t add3 -- Prim.Int -> Prim.Int
|
||||||
|
|
||||||
-- Forward and backward function composition
|
-- Forward and backward function composition
|
||||||
-- drop 3 followed by taking 5
|
-- drop 3 followed by taking 5
|
||||||
(drop 3 >>> take 5) (1..20) -- [4,5,6,7,8]
|
> (drop 3 >>> take 5) (1..20) -- [4,5,6,7,8]
|
||||||
-- take 5 followed by dropping 3
|
-- take 5 followed by dropping 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
|
||||||
even x = x % 2 == 0
|
> let 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]
|
||||||
|
|
||||||
-- Requires purescript-foldable-traversabe (Data.Foldable)
|
-- Requires purescript-foldable-traversabe (Data.Foldable)
|
||||||
|
|
||||||
foldr (+) 0 (1..10) -- 55
|
> foldr (+) 0 (1..10) -- 55
|
||||||
sum (1..10) -- 55
|
> sum (1..10) -- 55
|
||||||
product (1..10) -- 3628800
|
> product (1..10) -- 3628800
|
||||||
|
|
||||||
-- Testing with predicate
|
-- Testing with predicate
|
||||||
any even [1,2,3] -- true
|
> any even [1,2,3] -- true
|
||||||
all even [1,2,3] -- false
|
> all even [1,2,3] -- false
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user