Skip to content

Instantly share code, notes, and snippets.

@boxp
Created May 19, 2014 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boxp/487db3440d2898efcf36 to your computer and use it in GitHub Desktop.
Save boxp/487db3440d2898efcf36 to your computer and use it in GitHub Desktop.
C言語でコンスセルとその他諸々
#include<stdio.h>
#include<stdlib.h>
typedef struct cell {
int car;
struct cell *cdr;
} _cell;
struct cell *cons(int car, struct cell *cdr) {
struct cell *new;
new = malloc(sizeof(_cell));
new->car = car;
new->cdr = cdr;
return new;
};
struct cell *range(int start, int end) {
int tmp = end-1;
struct cell *res;
res = cons(tmp, NULL);
while(start!=tmp) {
tmp--;
res = cons(tmp, res);
}
return res;
};
void pprint(struct cell *coll) {
if(coll != NULL) {
printf("(%d ", coll->car);
pprint(coll->cdr);
printf(")");
} else {
printf("nil");
return 0;
}
}
int main(void) {
printf("range(1,40) => ");
pprint(range(1,40));
printf("\ncons(1, cons(2, NULL)) => ");
pprint(cons(1, cons(2, NULL)));
printf("\n");
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment