forked from joelvaneenwyk/language84
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.84
39 lines (34 loc) · 980 Bytes
/
sort.84
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
{
: list_insertion_on_heap
: list_insertion_on_stack
: list_insertion list_insertion_on_heap
}
Where
Define (list_insertion_on_heap compare input)
Define (insert sorted a)
Iterate {left right} From {'nil sorted}
Match right {
| 'nil (LIST.reverse [a & left])
| 'cons.{b right_tail}
Match (compare a b) {
| 'less (LIST.reverse_append left [a & right])
| _ (Continue [b & left] right_tail)
}
}
In
(LIST.reduce input 'nil insert)
Define (list_insertion_on_stack compare input)
Define (insert sorted a)
Unfold sorted
Match sorted {
| 'nil [a & 'nil]
| 'cons.{b sorted_tail}
Match (compare a b) {
| 'less [a & sorted]
| _ [b & (Fold sorted_tail)]
}
}
In
(LIST.reduce input 'nil insert)
Where
Let LIST Package "list"