Reader
This commit is contained in:
27
src/list.c
Normal file
27
src/list.c
Normal file
@ -0,0 +1,27 @@
|
||||
#include "list.h"
|
||||
|
||||
intptr_t list_length(LispVal *list) {
|
||||
assert(LISTP(list));
|
||||
LispVal *tortise = list;
|
||||
LispVal *hare = list;
|
||||
intptr_t length = 0;
|
||||
while (CONSP(tortise)) {
|
||||
tortise = XCDR_SAFE(tortise);
|
||||
hare = XCDR_SAFE(XCDR_SAFE(hare));
|
||||
if (!NILP(hare) && tortise == hare) {
|
||||
return -1;
|
||||
}
|
||||
++length;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
DEFUN(cons, "cons", (LispVal * car, LispVal *cdr), "(car cdr)",
|
||||
"Construct a new cons object from CAR and CDR.") {
|
||||
return CONS(car, cdr);
|
||||
}
|
||||
|
||||
DEFUN(length, "length", (LispVal * list), "(list)", "") {
|
||||
// TODO type check
|
||||
return MAKE_FIXNUM(list_length(list));
|
||||
}
|
||||
Reference in New Issue
Block a user