Weghalen unresolved labels en update examples
[cc1516.git] / examples / higher.spl
1 map(f, l){
2 if(isEmpty(l)){
3 return [];
4 } else {
5 return f(l.hd) : map(f, l.tl);
6 }
7 }
8
9 foldr(f, acc, l){
10 if(isEmpty(l)){
11 return acc;
12 } else {
13 return foldr(f, f(acc, l.hd), l.tl);
14 }
15 }
16
17 filter(f, l){
18 if(isEmpty(l)){
19 return [];
20 } else {
21 if(f(l.hd)){
22 return filter(f, l.tl);
23 } else {
24 return l.hd : filter(f, l.tl);
25 }
26 }
27 }
28
29 intList(x){
30 [Int] l = [];
31 Int a = 1;
32 while(a <= x){
33 l = a : l;
34 a = a + 1;
35 }
36 return l;
37 }
38
39 main(){
40 print("faculty of 5 is: ", foldr(\x y->x*y, 1, intList(5)));
41 print("sum of 1..5 is: ", foldr(\x y->x+y, 0, intList(5)));
42 print("filter evens from 0..12 is: ");
43 print("sum of 0..12 but only the evens: ",
44 foldr(\x y->x+y, 0, filter(\x->x%2 == 0, intList(12))));
45 }