author | Tony Garnock-Jones <tonyg@lshift.net> |
Tue, 18 Oct 2005 00:50:57 +1300 | |
changeset 23 | ca4c9dac5625 |
parent 20 | 9b9bd39d5e13 |
child 25 | 91504ba80408 |
permissions | -rw-r--r-- |
20
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
1 |
define map [ |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
2 |
(f Nil) : Nil |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
3 |
(f [Hd: h Tl: t]) : [Hd: f h Tl: map f t] |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
4 |
];; |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
5 |
|
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
6 |
(arg Hd) |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
7 |
(arg Tl) |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
8 |
|
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
9 |
"a pattern:" [(a b): _] |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
10 |
"-->" (arg a b) |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
11 |
|
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
12 |
define fold-left [ |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
13 |
(kons knil Nil) : knil |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
14 |
(kons knil [Hd: h Tl: t]) : fold-left kons (kons h knil) t |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
15 |
];; |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
16 |
|
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
17 |
map [x: x + 1] ([1, 2, 3] AsList);; |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
18 |
|
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
19 |
letrec (map = [(f Nil) : Nil |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
20 |
(f [Hd: h Tl: t]) : [Hd: f h Tl: map f t]], |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
21 |
toList = [[] : Nil |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
22 |
[h ; t]: [Hd: h Tl: (toList t)]]); |
23
ca4c9dac5625
Remove now-redundant parentheses.
Tony Garnock-Jones <tonyg@lshift.net>
parents:
20
diff
changeset
|
23 |
map [x: x] (toList [1 2 3]) |
ca4c9dac5625
Remove now-redundant parentheses.
Tony Garnock-Jones <tonyg@lshift.net>
parents:
20
diff
changeset
|
24 |
;; |
20
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
25 |
|
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
26 |
define map [ |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
27 |
(f Nil) : Nil |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
28 |
(f [Hd: h Tl: t]) : [Hd: f h Tl: map f t] |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
29 |
];; |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
30 |
|
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
31 |
map [x: x + 1] (toList [1 2 3]);; |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
32 |
|
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
33 |
[x: (Update: [] Set: x To: 123)] 'hi';; |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
34 |
|
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
35 |
define-behaviour Cst;; |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
36 |
define-method ({Cst cst} Convert) ( |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
37 |
Case: {cst} Of: ( |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
38 |
[[Adj: [l, r]]: [(l Convert) (r Convert)]], |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
39 |
[[Tuple: cs]: (let cs* = map [x: x Convert] cs in {Tuple cs*})], |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
40 |
[[Quote: v]: v], |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
41 |
[_: cst] |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
42 |
) |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
43 |
);; |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
44 |
|
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
45 |
"To lift something:" {lifted} <- val |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
46 |
"To drop something:" val <- {lifted} |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
47 |
|
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
48 |
define-method ({Tuple x} Length) ( |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
49 |
x Length;; |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
50 |
);; |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
51 |
|
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
52 |
define-method ({Tuple x} AsList) ( |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
53 |
(0 .. x Length) Map: [i: x ? i] |
9b9bd39d5e13
Lots of work. Simple evaluator.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff
changeset
|
54 |
);; |