Mercurial > repos > tabletprog
annotate modules/list.tp @ 170:18598163e3ef
Add linked list implementation and cons operator
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 13 Aug 2013 21:58:03 -0700 |
parents | |
children | 158444b77c09 |
rev | line source |
---|---|
170
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 _empty <- #{ |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 length <- { 0 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 empty? <- { true } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 fold:with <- :acc :fun { acc } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 foldr:with <- :acc :fun { acc } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 map <- :fun { self } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 | <- :val { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 list node: val withTail: self |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 . <- :right { right } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 #{ |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 empty <- { _empty } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 node:withTail <- :_val :_tail { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 #{ |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 value <- { _val } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 tail <- { _tail } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 empty? <- { false } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 length <- { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 fold: 0 with: :acc val { acc + 1 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 fold:with <- :acc :fun { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 cur <- self |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 while: { not: (cur empty?)} do: { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 acc <- fun: acc (cur value) |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 cur <- cur tail |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 acc |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 foldr:with <- :acc fun { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 fun: (_tail foldr: acc with: fun) _val |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 map <- :fun { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 node: (fun: _val) withTail: (_tail map: fun) |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 | <- :val { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 node: val withTail: self |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 . <- :right { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 foldr: right with: :tail val { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 node: val withTail: tail |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 } |