2014-09-01 01:26:56 +04:00
|
|
|
---
|
2022-01-03 19:10:24 +03:00
|
|
|
language: PureScript
|
2017-08-25 11:16:44 +03:00
|
|
|
filename: purescript.purs
|
2014-09-01 01:26:56 +04:00
|
|
|
contributors:
|
|
|
|
- ["Fredrik Dyrkell", "http://www.lexicallyscoped.com"]
|
2015-10-13 02:18:22 +03:00
|
|
|
- ["Thimoteus", "https://github.com/Thimoteus"]
|
2014-09-01 01:26:56 +04:00
|
|
|
---
|
|
|
|
|
2021-03-22 14:01:34 +03:00
|
|
|
PureScript is a small strongly, statically typed language compiling to JavaScript.
|
2014-09-01 01:26:56 +04:00
|
|
|
|
2021-03-22 14:04:03 +03:00
|
|
|
* Learn more at [https://www.purescript.org/](https://www.purescript.org/)
|
|
|
|
* Documentation: [https://pursuit.purescript.org/](https://pursuit.purescript.org/)
|
|
|
|
* Book: Purescript by Example, [https://book.purescript.org/](https://book.purescript.org/)
|
2014-09-01 01:26:56 +04:00
|
|
|
|
2021-03-22 19:36:30 +03:00
|
|
|
All the noncommented lines of code can be run in the PSCi REPL, though some
|
|
|
|
will require "paste" mode (`:paste` followed by multiple lines, terminated by
|
|
|
|
^D).
|
2015-10-13 11:25:56 +03:00
|
|
|
|
|
|
|
```haskell
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
--
|
2021-03-22 14:01:34 +03:00
|
|
|
-- 1. Primitive datatypes that corresponds to their JavaScript
|
2014-09-01 01:26:56 +04:00
|
|
|
-- equivalents at runtime.
|
|
|
|
|
2015-10-13 11:25:56 +03:00
|
|
|
import Prelude
|
2014-09-01 01:26:56 +04:00
|
|
|
-- Numbers
|
2015-10-13 11:25:56 +03:00
|
|
|
1.0 + 7.2*5.5 :: Number -- 40.6
|
2015-10-13 02:18:22 +03:00
|
|
|
-- Ints
|
2015-10-13 11:25:56 +03:00
|
|
|
1 + 2*5 :: Int -- 11
|
2014-09-01 01:26:56 +04:00
|
|
|
-- Types are inferred, so the following works fine
|
2015-10-13 11:25:56 +03:00
|
|
|
9.0/2.5 + 4.4 -- 8.0
|
2015-10-13 02:18:22 +03:00
|
|
|
-- But Ints and Numbers don't mix, so the following won't
|
2015-10-13 11:25:56 +03:00
|
|
|
5/2 + 2.5 -- Expression 2.5 does not have type Int
|
2014-09-01 01:26:56 +04:00
|
|
|
-- Hexadecimal literals
|
2015-10-13 11:25:56 +03:00
|
|
|
0xff + 1 -- 256
|
2014-09-01 01:26:56 +04:00
|
|
|
-- Unary negation
|
2015-10-13 11:25:56 +03:00
|
|
|
6 * -3 -- -18
|
|
|
|
6 * negate 3 -- -18
|
2015-10-13 02:18:22 +03:00
|
|
|
-- Modulus, from purescript-math (Math)
|
2015-10-13 11:25:56 +03:00
|
|
|
3.0 % 2.0 -- 1.0
|
|
|
|
4.0 % 2.0 -- 0.0
|
2014-09-01 01:26:56 +04:00
|
|
|
-- Inspect the type of an expression in psci
|
2021-03-22 19:44:45 +03:00
|
|
|
:t 9.5/2.5 + 4.4 -- Number
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
-- Booleans
|
2015-10-13 11:25:56 +03:00
|
|
|
true :: Boolean -- true
|
|
|
|
false :: Boolean -- false
|
2014-09-01 01:26:56 +04:00
|
|
|
-- Negation
|
2015-10-13 11:25:56 +03:00
|
|
|
not true -- false
|
|
|
|
23 == 23 -- true
|
|
|
|
1 /= 4 -- true
|
|
|
|
1 >= 4 -- false
|
2016-02-28 01:38:22 +03:00
|
|
|
-- Comparisons < <= > >=
|
2014-09-01 01:26:56 +04:00
|
|
|
-- are defined in terms of compare
|
2015-10-13 11:25:56 +03:00
|
|
|
compare 1 2 -- LT
|
|
|
|
compare 2 2 -- EQ
|
|
|
|
compare 3 2 -- GT
|
2014-09-01 01:26:56 +04:00
|
|
|
-- Conjunction and Disjunction
|
2015-10-13 11:25:56 +03:00
|
|
|
true && (9 >= 19 || 1 < 2) -- true
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
-- Strings
|
2022-12-10 18:05:34 +03:00
|
|
|
"Hello" :: String -- "Hello"
|
2021-03-22 19:36:30 +03:00
|
|
|
-- Multiline string without newlines, to run in PSCi use "paste" mode.
|
2022-12-10 18:05:34 +03:00
|
|
|
"Hello\
|
2014-09-01 01:26:56 +04:00
|
|
|
\orld" -- "Helloworld"
|
2015-10-13 02:18:22 +03:00
|
|
|
-- Multiline string with newlines
|
2016-02-28 01:38:22 +03:00
|
|
|
"""Hello
|
2015-10-13 02:18:22 +03:00
|
|
|
world""" -- "Hello\nworld"
|
2014-09-01 01:26:56 +04:00
|
|
|
-- Concatenate
|
2017-03-20 19:20:21 +03:00
|
|
|
"such " <> "amaze" -- "such amaze"
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
--
|
2021-03-22 14:01:34 +03:00
|
|
|
-- 2. Arrays are JavaScript arrays, but must be homogeneous
|
2014-09-01 01:26:56 +04:00
|
|
|
|
2018-10-03 16:21:23 +03:00
|
|
|
[1,1,2,3,5,8] :: Array Int -- [1,1,2,3,5,8]
|
|
|
|
[1.2,2.0,3.14] :: Array Number -- [1.2,2.0,3.14]
|
2015-10-13 11:25:56 +03:00
|
|
|
[true, true, false] :: Array Boolean -- [true,true,false]
|
2014-09-01 01:26:56 +04:00
|
|
|
-- [1,2, true, "false"] won't work
|
2021-03-22 19:44:45 +03:00
|
|
|
-- `Cannot unify Int with Boolean`
|
2021-03-22 19:47:52 +03:00
|
|
|
|
|
|
|
-- Requires purescript-arrays (Data.Array)
|
2014-09-01 01:26:56 +04:00
|
|
|
-- Cons (prepend)
|
2015-10-13 11:25:56 +03:00
|
|
|
1 : [2,4,3] -- [1,2,4,3]
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
-- and purescript-maybe (Data.Maybe)
|
|
|
|
-- Safe access return Maybe a
|
2021-03-22 19:49:10 +03:00
|
|
|
head [1,2,3] -- (Just 1)
|
|
|
|
tail [3,2,1] -- (Just [2,1])
|
|
|
|
init [1,2,3] -- (Just [1,2])
|
|
|
|
last [3,2,1] -- (Just 1)
|
2016-12-14 21:59:16 +03:00
|
|
|
-- Array access - indexing
|
2021-03-22 19:49:10 +03:00
|
|
|
[3,4,5,6,7] !! 2 -- (Just 5)
|
2015-10-08 06:11:24 +03:00
|
|
|
-- Range
|
2015-10-13 11:25:56 +03:00
|
|
|
1..5 -- [1,2,3,4,5]
|
|
|
|
length [2,2,2] -- 3
|
|
|
|
drop 3 [5,4,3,2,1] -- [2,1]
|
|
|
|
take 3 [5,4,3,2,1] -- [5,4,3]
|
|
|
|
append [1,2,3] [4,5,6] -- [1,2,3,4,5,6]
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
--
|
2021-03-22 14:01:34 +03:00
|
|
|
-- 3. Records are JavaScript objects, with zero or more fields, which
|
2015-10-13 02:18:22 +03:00
|
|
|
-- can have different types.
|
2021-03-22 19:49:10 +03:00
|
|
|
book = {title: "Foucault's pendulum", author: "Umberto Eco"}
|
2014-09-01 01:26:56 +04:00
|
|
|
-- Access properties
|
2015-10-13 11:25:56 +03:00
|
|
|
book.title -- "Foucault's pendulum"
|
2014-09-01 01:26:56 +04:00
|
|
|
|
2021-03-22 19:49:10 +03:00
|
|
|
getTitle b = b.title
|
2014-09-01 01:26:56 +04:00
|
|
|
-- Works on all records with a title (but doesn't require any other field)
|
2015-10-13 11:25:56 +03:00
|
|
|
getTitle book -- "Foucault's pendulum"
|
|
|
|
getTitle {title: "Weekend in Monaco", artist: "The Rippingtons"} -- "Weekend in Monaco"
|
2015-10-13 02:18:22 +03:00
|
|
|
-- Can use underscores as shorthand
|
2015-10-13 11:25:56 +03:00
|
|
|
_.title book -- "Foucault's pendulum"
|
2014-09-01 01:26:56 +04:00
|
|
|
-- Update a record
|
2021-03-22 19:49:10 +03:00
|
|
|
changeTitle b t = b {title = t}
|
2015-10-13 11:25:56 +03:00
|
|
|
getTitle (changeTitle book "Ill nome della rosa") -- "Ill nome della rosa"
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
--
|
|
|
|
-- 4. Functions
|
2021-03-22 19:49:10 +03:00
|
|
|
-- In PSCi's paste mode
|
|
|
|
sumOfSquares :: Int -> Int -> Int
|
|
|
|
sumOfSquares x y = x*x + y*y
|
2015-10-13 11:25:56 +03:00
|
|
|
sumOfSquares 3 4 -- 25
|
2021-03-22 19:49:10 +03:00
|
|
|
|
|
|
|
myMod x y = x % y
|
2015-10-13 11:25:56 +03:00
|
|
|
myMod 3.0 2.0 -- 1.0
|
2014-09-01 01:26:56 +04:00
|
|
|
-- Infix application of function
|
2015-10-13 11:25:56 +03:00
|
|
|
3 `mod` 2 -- 1
|
2014-09-01 01:26:56 +04:00
|
|
|
|
2015-10-13 02:18:22 +03:00
|
|
|
-- function application has higher precedence than all other
|
2014-09-01 01:26:56 +04:00
|
|
|
-- operators
|
2015-10-13 11:25:56 +03:00
|
|
|
sumOfSquares 3 4 * sumOfSquares 4 5 -- 1025
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
-- Conditional
|
2021-03-22 19:49:10 +03:00
|
|
|
abs' n = if n>=0 then n else -n
|
2015-10-13 11:25:56 +03:00
|
|
|
abs' (-3) -- 3
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
-- Guarded equations
|
2021-03-22 19:49:10 +03:00
|
|
|
-- In PSCi's paste mode
|
|
|
|
abs'' n | n >= 0 = n
|
|
|
|
| otherwise = -n
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
-- Pattern matching
|
|
|
|
|
2015-10-13 02:18:22 +03:00
|
|
|
-- Note the type signature, input is a list of numbers. The pattern matching
|
|
|
|
-- destructures and binds the list into parts.
|
2021-03-22 19:49:10 +03:00
|
|
|
-- Requires purescript-lists (Data.List) and purescript-maybe (Data.Maybe)
|
|
|
|
first :: forall a. List a -> Maybe a
|
|
|
|
first (x : _) = Just x
|
|
|
|
first Nil = Nothing
|
|
|
|
first (fromFoldable [3,4,5]) -- (Just 3)
|
|
|
|
|
|
|
|
second :: forall a. List a -> Maybe a
|
|
|
|
second Nil = Nothing
|
|
|
|
second (_ : Nil) = Nothing
|
|
|
|
second (_ : (y : _)) = Just y
|
|
|
|
second (fromFoldable [3,4,5]) -- (Just 4)
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
-- Complementing patterns to match
|
|
|
|
-- Good ol' Fibonacci
|
2021-03-22 19:49:10 +03:00
|
|
|
fib 1 = 1
|
|
|
|
fib 2 = 2
|
|
|
|
fib x = fib (x-1) + fib (x-2)
|
2015-10-13 11:25:56 +03:00
|
|
|
fib 10 -- 89
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
-- Use underscore to match any, where you don't care about the binding name
|
2021-03-22 19:49:10 +03:00
|
|
|
isZero 0 = true
|
|
|
|
isZero _ = false
|
|
|
|
isZero 9 -- false
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
-- Pattern matching on records
|
2021-03-22 19:49:10 +03:00
|
|
|
ecoTitle {author: "Umberto Eco", title: t} = Just t
|
|
|
|
ecoTitle _ = Nothing
|
2014-09-01 01:26:56 +04:00
|
|
|
|
2021-03-22 19:49:10 +03:00
|
|
|
ecoTitle {title: "Foucault's pendulum", author: "Umberto Eco"} -- (Just "Foucault's pendulum")
|
2015-10-13 11:25:56 +03:00
|
|
|
ecoTitle {title: "The Quantum Thief", author: "Hannu Rajaniemi"} -- Nothing
|
2014-09-01 01:26:56 +04:00
|
|
|
-- ecoTitle requires both field to type check:
|
2015-10-13 11:25:56 +03:00
|
|
|
ecoTitle {title: "The Quantum Thief"} -- Object lacks required property "author"
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
-- Lambda expressions
|
2015-10-13 11:25:56 +03:00
|
|
|
(\x -> x*x) 3 -- 9
|
|
|
|
(\x y -> x*x + y*y) 4 5 -- 41
|
2021-03-22 19:49:10 +03:00
|
|
|
sqr = \x -> x*x
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
-- Currying
|
2021-03-22 19:49:10 +03:00
|
|
|
myAdd x y = x + y -- is equivalent with
|
|
|
|
myAdd' = \x -> \y -> x + y
|
|
|
|
add3 = myAdd 3
|
|
|
|
:t add3 -- Int -> Int
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
-- Forward and backward function composition
|
|
|
|
-- drop 3 followed by taking 5
|
2015-10-13 11:25:56 +03:00
|
|
|
(drop 3 >>> take 5) (1..20) -- [4,5,6,7,8]
|
2014-09-01 01:26:56 +04:00
|
|
|
-- take 5 followed by dropping 3
|
2015-10-13 11:25:56 +03:00
|
|
|
(drop 3 <<< take 5) (1..20) -- [4,5]
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
-- Operations using higher order functions
|
2021-03-22 19:49:10 +03:00
|
|
|
even x = x `mod` 2 == 0
|
2015-10-13 11:25:56 +03:00
|
|
|
filter even (1..10) -- [2,4,6,8,10]
|
|
|
|
map (\x -> x + 11) (1..5) -- [12,13,14,15,16]
|
2014-09-01 01:26:56 +04:00
|
|
|
|
2016-02-26 01:30:35 +03:00
|
|
|
-- Requires purescript-foldable-traversable (Data.Foldable)
|
2014-09-01 01:26:56 +04:00
|
|
|
|
2015-10-13 11:25:56 +03:00
|
|
|
foldr (+) 0 (1..10) -- 55
|
|
|
|
sum (1..10) -- 55
|
|
|
|
product (1..10) -- 3628800
|
2014-09-01 01:26:56 +04:00
|
|
|
|
2015-10-08 06:11:24 +03:00
|
|
|
-- Testing with predicate
|
2015-10-13 11:25:56 +03:00
|
|
|
any even [1,2,3] -- true
|
|
|
|
all even [1,2,3] -- false
|
2014-09-01 01:26:56 +04:00
|
|
|
|
|
|
|
```
|