-
Notifications
You must be signed in to change notification settings - Fork 1
/
playground.txt
68 lines (52 loc) · 1008 Bytes
/
playground.txt
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
module Sequence = struct
type v
type v seq
val hd? : v seq -> bool
val tl? : v seq -> bool
val hd : v seq -> v
val tl : v seq -> v seq
end
struct SequenceCtor : Sequence = {
nil : v seq
empty : v seq
cons : v -> v seq -> v seq
push : v -> v seq -> v seq
add : v -> v seq -> v seq
}
struct
map :: (a -> b) -> a seq -> b seq
val map f seq = match seq with
| hd x ->
map f xs =
if hd? xs then f . hd xs
else if tl? xs then f
select
project
reduce
struct List =
type a list
type [a] list = [] | a :: a list
hd? : a list -> bool
tl? : a list -> bool
hd : a list -> a
tl : a list -> a list
end struct
iter f xs = {
if hd? xs { f . hd xs }
if tl? xs { iter f . tl xs}
}
iter f = match
| [] -> ()
| x :: xs -> f x; iter f xs
fold f accum xs = match
| [] -> accum
| x :: xs -> fold (f accum x) xs
fold f accum xs = {
if hd? xs { }
}
type fib : Int32 -> Int32
val fib n =
if n <= 0 then 0
else if n = 1 then 1
else fib (n-1) + fib (n-2)
end