Add cons, car, cdr
This commit is contained in:
parent
63af87b9ca
commit
b316404b2b
2 changed files with 26 additions and 1 deletions
|
@ -285,7 +285,20 @@ my %stdctx = (
|
||||||
$v = $cb->($v,$_) foreach (@copy);
|
$v = $cb->($v,$_) foreach (@copy);
|
||||||
return $v;
|
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
|
# Constants
|
||||||
't' => 1,
|
't' => 1,
|
||||||
|
|
12
t/lists.t
12
t/lists.t
|
@ -16,3 +16,15 @@
|
||||||
|
|
||||||
(expect "equal tests recursively 2"
|
(expect "equal tests recursively 2"
|
||||||
(not (equal (list (list 1) (list 2)) (list (list 1) (list 3)))))
|
(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))))
|
||||||
|
|
Loading…
Reference in a new issue