Merge branch 'master' of github.com:dopefishh/cc1516
[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(
41 "faculty of 5 is: ",
42 foldr(\x y->x*y, 1, intList(z)),
43 "sum of 1..5 is: ",
44 foldr(\x y->x+y, 0, intList(z)),
45 "filter evens from 0..12 is: ",
46 filter(\x->x % 2 == 0, intList(12)));
47 }