musique/examples/church.mq
Robert Bendun ddf9cc8f8c New declaration syntax x := foo instead of var x = foo
Additionaly moving into multiparameter binary operations
2022-09-18 14:50:20 +02:00

35 lines
857 B
Plaintext

-- PAIR definition
pair := [x y | [z | z x y]];
car := [pair | pair [x y | x]];
cdr := [pair | pair [x y | y]];
x := pair 100 200;
say (car x);
say (cdr x);
-- LIST definition
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)];
for_each := [list iterator |
if (is_empty list) [ nil ] [
iterator (head list);
for_each (tail list) iterator ]];
map := [list iterator |
if (is_empty list) [ null ] [|
cons (iterator (head list)) (map (tail list) iterator) ]];
foldr := [list init folder |
if (is_empty list)
[ init ]
[ foldr (tail list) (folder (head list) init) folder ]];
range := [start stop | if (start >= stop) [cons start null] [cons start (range (start+1) stop)]];
xs := range 1 5;
say (foldr xs 1 [x y|x*y]);