Add cons, car, cdr

This commit is contained in:
madmaurice 2021-04-03 18:05:59 +02:00
parent 63af87b9ca
commit b316404b2b
2 changed files with 26 additions and 1 deletions

View file

@ -285,7 +285,20 @@ my %stdctx = (
$v = $cb->($v,$_) foreach (@copy);
return $v;
},
# TODO: this is tricky as it needs to recurse into sub lists
'cons' => sub {
my ($v, $list) = @_;
return [ $v, @$list ];
},
'car' => sub {
my ($list) = @_;
return $list->[0];
},
'cdr' => sub {
my ($list) = @_;
my @newlist = @$list;
shift @newlist; # drop first element
return \@newlist;
},
# Constants
't' => 1,

View file

@ -16,3 +16,15 @@
(expect "equal tests recursively 2"
(not (equal (list (list 1) (list 2)) (list (list 1) (list 3)))))
(expect "Length of empty list is 0"
(zerop (length (list))))
(expect "cons adds elements to the front"
(equal (list 1 2) (cons 1 (list 2))))
(let ((lst (list 5 1 2)))
(expect "car gives first element"
(= 5 (car lst)))
(expect "cdr gives rest of list"
(equal (list 1 2) (cdr lst))))