demo
[cc1516.git] / examples / old / demo.spl
1 //Classical higher order functions:)
2 map(f, l){
3 if(isEmpty(l)){
4 return [];
5 } else {
6 return f(l.hd) : map(f, l.tl);
7 }
8 }
9
10 foldr(f, acc, l){
11 if(isEmpty(l)){
12 return acc;
13 } else {
14 return foldr(f, f(acc, l.hd), l.tl);
15 }
16 }
17
18 //Functions for operators
19 plus(x, y){ return x + y; }
20 times(x, y){ return x * y; }
21
22 intList(x){
23 [Int] l = [];
24 Int a = 1;
25 while(a <= x){
26 l = a : l;
27 a = a + 1;
28 }
29 return l;
30 }
31
32 faculty(x){
33 return foldr(times, 1, intList(x));
34 }
35
36 sum(x){
37 return foldr(plus, 0, intList(x));
38 }
39
40 main(){
41 print("faculty of 5 is: ", faculty(5));
42 print("sum of 1..5 is: ", sum(50));
43 }