withMulklib working the first time
[lazyeval.git] / lists.h
1 #ifndef __LISTS_H
2 #define __LISTS_H
3
4 #include <stdbool.h>
5
6 typedef int listElt;
7
8 typedef struct {
9   listElt car;
10   void* cdr; /* cons ** cdr */
11 } cons;
12
13 typedef cons** list;
14
15 list getNil () {
16   static cons nil1;
17   static cons * nil2 = &nil1;
18   return &nil2;
19 }
20
21 bool isNil (list c) {
22   return getNil () == c;
23 }
24
25 listElt car(list l) {
26   if (isNil(l)) {
27     printf("Called car of NIL!\n");
28     fsync(0);
29     exit(-1);
30   }
31   return (*l)->car;
32 }
33
34 list cdr (list l) {
35   if (isNil(l)) {
36     printf("Called cdr of NIL!\n");
37     fsync(0);
38     exit(-1);
39   }
40   list rt = (list) (*l)->cdr;
41   return rt;
42 }
43
44 listElt elt (list l, int eltnum) {
45   return (eltnum == 0) ? car(l) : elt (cdr(l), eltnum - 1);
46 }
47
48 int len (list l) {
49   if (isNil(l)) return 0;
50   return len(cdr(l));
51 }
52
53 list doCons (int car, list cdr, cons * newCons) {
54   newCons->cdr = (void*) cdr;
55   newCons->car = car;
56 }
57
58 #endif