-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.rv
More file actions
69 lines (51 loc) · 1.22 KB
/
Copy pathsequence.rv
File metadata and controls
69 lines (51 loc) · 1.22 KB
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
import { ... } from "./core.rv"
import { ... } from "./numbers.rv"
import { isa? } from "./patterns.rv"
export {
Sequence, seq, first, rest, prepend, empty?, length, seqRange, repeat, sum
}
bundle Sequence { Prepend(xs, x), Empty() }
fn prepend(xs: Sequence, x) { Prepend(xs, x) }
fn seq() { Empty() }
fn seq(x, xs...) { prepend(seq(xs...), x) }
fn first(l: Empty) { abort("first: seq is empty") }
fn first(l: Prepend) { part(l, 2) }
@extend
fn rest(l: Empty) { abort("rest: seq is empty") }
@extend
fn rest(l: Prepend) { part(l, 1) }
@extend
fn empty?(_: Prepend) { false }
@extend
fn empty?(_: Empty) { true }
@extend
fn length(Prepend(xs, _)) { 1 + length(xs) }
@extend
fn length(Empty()) { 0 }
fn seqRange(start, end) {
if start > end {
seq()
} else {
prepend(seqRange(start + one(start), end), start)
}
}
fn repeat(n, x) { prepend(repeat(n - 1, x), x) }
fn repeat(0, x) { seq() }
fn sum(Empty(), total) { total }
fn sum(Prepend(xs, x), total) { sum(xs, total + x) }
fn sum(Prepend(xs, x)) { sum(xs, x) }
fn printList(Empty()) {
print(")")
}
fn printList(Prepend(xs, x)) {
print(x)
if !isa?(xs, Empty) {
print(", ")
}
printList(xs)
}
@extend
fn show(xs: Sequence) {
print("seq(")
printList(xs)
}