fixed 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 if(x <= 1){
31 return [x];
32 } else {
33 return x : intList(x-1);
34 }
35 }
36
37 main(){
38 var x = 5;
39 print("faculty of 5 is: ", foldr(\x y->x*y, 1, intList(5)));
40 print("sum of 1..5 is: ", foldr(\x y->x+y, 0, intList(5)));
41 print("sum of 0..12 but only the evens: ",
42 foldr(\x y->x+y, 0, filter(\x->x%2 == 0, intList(12))));
43 }