withMulklib working the first time
[lazyeval.git] / lists.h
diff --git a/lists.h b/lists.h
new file mode 100644 (file)
index 0000000..9f9b781
--- /dev/null
+++ b/lists.h
@@ -0,0 +1,58 @@
+#ifndef __LISTS_H
+#define __LISTS_H
+
+#include <stdbool.h>
+
+typedef int listElt;
+
+typedef struct {
+  listElt car;
+  void* cdr; /* cons ** cdr */
+} cons;
+
+typedef cons** list;
+
+list getNil () {
+  static cons nil1;
+  static cons * nil2 = &nil1;
+  return &nil2;
+}
+
+bool isNil (list c) {
+  return getNil () == c;
+}
+
+listElt car(list l) {
+  if (isNil(l)) {
+    printf("Called car of NIL!\n");
+    fsync(0);
+    exit(-1);
+  }
+  return (*l)->car;
+}
+
+list cdr (list l) {
+  if (isNil(l)) {
+    printf("Called cdr of NIL!\n");
+    fsync(0);
+    exit(-1);
+  }
+  list rt = (list) (*l)->cdr;
+  return rt;
+}
+
+listElt elt (list l, int eltnum) {
+  return (eltnum == 0) ? car(l) : elt (cdr(l), eltnum - 1);
+}
+
+int len (list l) {
+  if (isNil(l)) return 0;
+  return len(cdr(l));
+}
+
+list doCons (int car, list cdr, cons * newCons) {
+  newCons->cdr = (void*) cdr;
+  newCons->car = car;
+}
+
+#endif