forked from joelvaneenwyk/language84
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.84
More file actions
84 lines (69 loc) · 1.35 KB
/
queue.84
File metadata and controls
84 lines (69 loc) · 1.35 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{
: empty
: new
: is_empty
: push
: pop
: pop_all
: append
: concat
}
Where
Define (concat queues)
(LIST.reduce queues empty append)
Where
Define (pop_all queue)
Unfold queue
Match (pop queue) {
| 'nothing 'nil
| 'just.{item queue} [item & (Fold queue)]
}
Define (push_all queue items)
(LIST.reduce items queue push)
Define (append a b)
Iterate {a b} From {a b}
Match (pop b) {
| 'nothing a
| 'just.{item b} (Continue (push a item) b)
}
Where
Define (push queue item)
{
: incoming [item & queue.incoming]
: outgoing queue.outgoing
}
Define (pop queue)
Unfold queue
Match queue.outgoing {
| 'cons.{item outgoing}
Let queue
{
: incoming queue.incoming
: outgoing
}
In
'just.{item queue}
| 'nil
Match queue.incoming {
| 'nil 'nothing
| _ (Fold (new (LIST.reverse queue.incoming)))
}
}
Where
Define (new init)
{
: incoming 'nil
: outgoing init
}
Let empty
{
: incoming 'nil
: outgoing 'nil
}
Define (is_empty queue)
Match queue.incoming {
| 'nil Match queue.outgoing { | 'nil True | 'cons._ False }
| 'cons._ False
}
Where
Let LIST Package "list"