-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprelude.roy
111 lines (85 loc) · 1.8 KB
/
prelude.roy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
let id a = a
let flip f a b = f b a
let not a =
if a then
false
else
true
let even a = (a % 2) == 0
let odd a = not (even a)
let succ n = n + 1
let pred n = n - 1
//let compose f g = λx → f (g x)
//let ∘ = compose
//let elem =
//let ∈ = elem
//let ∉ = not ∘ elem
//let negate n = -n
// List type
// data List a = Cons a (List a) | Nil
// let nil = Nil ()
// let map f l = match l
// case (Cons v r) = Cons (f v) (map f r)
// case Nil = nil
// let filter f l = match l
// case (Cons v r) = if (f v) then
// Cons v r
// else
// r
// case Nil = nil
// let head l = match l
// case (Cons v _) = v
// let tail l = match l
// case (Cons _ r) = r
// case Nil = nil
// let empty l = match l
// case (Cons a b) = false
// case Nil = true
// let length l = match l
// case (Cons _ r) = 1 + (length r)
// case Nil = 0
// let foldl f i l = match l
// case (Cons v r) = foldl f (f i v) r
// case Nil = i
// let replicate n v =
// if (n == 0) then
// nil
// else
// Cons v (replicate (n - 1) v)
// let take n l = match l
// case (Cons v r) = if n == 0 then
// nil
// else
// Cons v (take (n - 1) r)
// case Nil = nil
// Option type
data Option a = Some a | None
let maybe n f o = match o
case (Some s) = f s
case None = n
let optionMonad = {
return: λx →
Some x
bind: λx f → match x
case (Some a) = f a
case None = None
}
// Either type.
data Either a b = Left a | Right b
let rightMonad = {
return: λx → Right x
bind: λx f → match x
case (Left a) = Left a
case (Right a) = f a
}
let leftMonad = {
return: λx → Left x
bind: λx f → match x
case (Left a) = f a
case (Right a) = Right a
}
// Math constants.
let π: Number = Math.PI
let π2 = π * 2
// tauday.com
let τ = π2