Created
May 19, 2014 14:18
-
-
Save boxp/487db3440d2898efcf36 to your computer and use it in GitHub Desktop.
C言語でコンスセルとその他諸々
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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