-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
98 lines (80 loc) · 2.01 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// CSS341, Fall 2011, Lecture 11
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
typedef struct List list_t;
struct List {
void * head;
list_t * tail;
};
list_t * makelist (void * x, list_t * xs) {
list_t * ans = (list_t *)malloc(sizeof(list_t));
ans->head = x;
ans->tail = xs;
return ans;
}
list_t * map(void* (*f)(void*,void*), void* env, list_t * xs) {
if(xs==NULL)
return NULL;
return makelist(f(env,xs->head), map(f,env,xs->tail));
}
list_t * filter(bool (*f)(void*,void*), void* env, list_t * xs) {
if(xs==NULL)
return NULL;
if(f(env,xs->head))
return makelist(xs->head, filter(f,env,xs->tail));
return filter(f,env,xs->tail);
}
int length(list_t* xs) {
int ans = 0;
while(xs != NULL) {
++ans;
xs = xs->tail;
}
return ans;
}
//signed integer type that is big enough to hold a pointer
// awful type casts to match what map expects
void* doubleInt(void* ignore, void* i) {
return (void*)(((intptr_t)i)*2);
}
// assumes list holds intptr_t fields
list_t * doubleAll(list_t * xs) {
return map(doubleInt, NULL, xs);
}
// awful type casts to match what filter expects
bool isN(void* n, void* i) {
return ((intptr_t)n)==((intptr_t)i);
}
// assumes list hold intptr_t fields
int countNs(list_t * xs, intptr_t n) {
return length(filter(isN, (void*)n, xs));
}
void* dec(void* ignore, void* i) {
return (void*)(((intptr_t)i)-1);
}
list_t * decAll(list_t * xs) {
return map(dec, NULL, xs);
}
void main()
{
//testing link list
/* list_t* x=NULL;*/
/* list_t * ans = (list_t *)malloc(sizeof(list_t));*/
/* ans->head = 1;*/
/* ans->tail = x;*/
/* x=ans;
/*printf("%d",x->head);*/
// creats a list named z
list_t* y= makelist(9,NULL);
list_t* z= makelist(10,y);
int len= length(z);
printf("\nlength of list : %d\n",len);
printf("our list : [%d,",z->head);
printf(" %d]\n",z->tail->head);
list_t* l= decAll(z); // mapping dec fucntion to list z
printf("mapped list : [%d,",l->head);
printf(" %d]",l->tail->head);
printf("\n");
return 0;
}