2022-05-22 00:34:02 +02:00
|
|
|
-- PAIR definition
|
2022-09-18 14:50:20 +02:00
|
|
|
pair := [x y | [z | z x y]];
|
|
|
|
car := [pair | pair [x y | x]];
|
|
|
|
cdr := [pair | pair [x y | y]];
|
2022-05-22 00:34:02 +02:00
|
|
|
|
2022-09-18 14:50:20 +02:00
|
|
|
x := pair 100 200;
|
2022-05-22 00:34:02 +02:00
|
|
|
say (car x);
|
|
|
|
say (cdr x);
|
|
|
|
|
|
|
|
-- LIST definition
|
2022-09-18 14:50:20 +02:00
|
|
|
null := pair true true;
|
|
|
|
is_empty := car;
|
|
|
|
head := [list | car (cdr list)];
|
|
|
|
tail := [list | cdr (cdr list)];
|
|
|
|
cons := [head tail | pair false (pair head tail)];
|
2022-05-22 00:34:02 +02:00
|
|
|
|
2022-09-18 14:50:20 +02:00
|
|
|
for_each := [list iterator |
|
2022-05-22 05:29:41 +02:00
|
|
|
if (is_empty list) [ nil ] [
|
2022-05-22 00:34:02 +02:00
|
|
|
iterator (head list);
|
|
|
|
for_each (tail list) iterator ]];
|
|
|
|
|
2022-09-18 14:50:20 +02:00
|
|
|
map := [list iterator |
|
2022-05-22 05:29:41 +02:00
|
|
|
if (is_empty list) [ null ] [|
|
2022-05-22 00:34:02 +02:00
|
|
|
cons (iterator (head list)) (map (tail list) iterator) ]];
|
|
|
|
|
2022-09-18 14:50:20 +02:00
|
|
|
foldr := [list init folder |
|
2022-05-22 00:34:02 +02:00
|
|
|
if (is_empty list)
|
2022-05-22 05:29:41 +02:00
|
|
|
[ init ]
|
|
|
|
[ foldr (tail list) (folder (head list) init) folder ]];
|
2022-05-22 00:34:02 +02:00
|
|
|
|
2022-09-18 14:50:20 +02:00
|
|
|
range := [start stop | if (start >= stop) [cons start null] [cons start (range (start+1) stop)]];
|
2022-05-22 00:34:02 +02:00
|
|
|
|
2022-09-18 14:50:20 +02:00
|
|
|
xs := range 1 5;
|
2022-05-22 00:34:02 +02:00
|
|
|
say (foldr xs 1 [x y|x*y]);
|