-
Notifications
You must be signed in to change notification settings - Fork 0
/
immu.c
67 lines (55 loc) · 1.31 KB
/
immu.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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
typedef struct __ll_node {
int value;
struct __ll_node* next;
} ll_node;
int head(ll_node* a) {
return a->value;
}
ll_node* tail(ll_node* a) {
return a->next;
}
ll_node* copy_ll(ll_node* list) {
if (list == NULL) return NULL;
ll_node* newNode = malloc(sizeof(ll_node));
newNode->value = head(list);
newNode->next = copy_ll(tail(list));
return newNode;
}
ll_node* newList(int value, ll_node* rest) {
ll_node* newHead = malloc(sizeof(ll_node));
newHead->value = value;
ll_node* restCopy = copy_ll(rest);
newHead->next = restCopy;
return newHead;
}
bool fizz(int a) {
return a % 3 == 0 || a % 5 == 0;
}
ll_node* filter(ll_node* list, bool (* filter_funct)(int a)) {
if (list == NULL) {
return NULL;
}
if (filter_funct(list->value)) {
return newList(list->value, filter(tail(list), filter_funct));
} else {
return filter(tail(list), filter_funct);
}
}
int sum(ll_node* a) {
if (a == NULL) return 0;
return a->value + sum(tail(a));
}
ll_node* range(int i) {
if (i == 1) {
return newList(0, NULL);
} else {
return newList(i - 1, range(i - 1));
}
}
int main() {
printf("%d\n", sum(filter(range(1000), &fizz)));
return 0;
}