This commit is contained in:
2026-01-16 03:20:38 -08:00
parent e0d8693840
commit 94d5749d31
19 changed files with 1358 additions and 3 deletions

27
src/list.c Normal file
View 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));
}