Almost working
[lisp-compiler-in-prolog.git] / tests.pl
1 /*******************************************************************
2 *
3 * A Lisp compiler, written in Prolog
4 *
5 * (tests.pl)
6 *
7 * (c) Neil Smith, 2001
8 *
9 * A few sample function definitions, mainly used by me as simple
10 * test cases for the compiler. I'm sure you can come up with
11 * something better...
12 *
13 *******************************************************************/
14
15
16
17 simple(x) <<== x.
18
19
20 lisp_append_2(l1, l2) <<==
21 cond( [[null(l1), l2],
22 [t, cons( first(l1),
23 lisp_append_2(rest(l1),
24 l2))]]).
25
26
27 lisp_error(x) <<== setq(y, 5).
28
29 lisp_let_simple <<==
30 let([bind(x, 10), bind(y, 20)],
31 x).
32
33 lisp_let <<==
34 let([bind(x, 3), bind(y, 5)],
35 [x, y]). % implicit progn here
36
37
38 % maps 'first' over a list of lists
39 mapfirst(l) <<==
40 mapcar(function(first), l).
41
42
43 <<== defvar(fred, 13).
44
45 <<== defvar(george).
46
47
48 reset_george(val) <<==
49 setq(george, val).
50
51
52 make_adder(x) <<==
53 function(lambda([y], plus(x, y))).
54
55
56 scale_list(xs, scale) <<==
57 let([bind(fred, function(lambda([num], times(scale, num))))],
58 mapcar(fred, xs)).
59
60
61 make_summer(total) <<==
62 function(lambda([n],
63 setq(total, plus(total, n)))).
64
65
66 sum_with_map(xs) <<==
67 let([bind(running_total, 0)],
68 let([bind(summer, function(lambda([n], setq(running_total,
69 plus(running_total, n)))))],
70 [ mapcar(summer, xs),
71 running_total ])).
72
73