96160bd99871141ed775f6555aa3b6e4107324c2
[cc1516.git] / examples / parsing.spl
1 toInt(x) :: Char -> Int{
2 if(x == '0'){ return 0;
3 } else { if(x == '1'){ return 1;
4 } else { if(x == '2'){ return 2;
5 } else { if(x == '3'){ return 3;
6 } else { if(x == '4'){ return 4;
7 } else { if(x == '5'){ return 5;
8 } else { if(x == '6'){ return 6;
9 } else { if(x == '7'){ return 7;
10 } else { if(x == '8'){ return 8;
11 } else { if(x == '9'){ return 9;
12 } else { return -1; }}}}}}}}}}
13 }
14
15 isDigit(x) :: Char -> Bool {
16 return toInt(x) != -1;
17 }
18
19 strToInt(x) :: [Char] -> Int {
20 [Char] xs = x;
21 var i = 0;
22 var m = 1;
23 if(!isEmpty(x)){
24 if(xs.hd == '-'){
25 xs = xs.tl;
26 m = -1;
27 }
28
29 while(!isEmpty(xs)){
30 i = i*10 + toInt(xs.hd);
31 xs = xs.tl;
32 }
33 }
34 return i*m;
35 }
36
37 printList(p, l) {
38 print('[');
39 if(!isEmpty(l)){
40 p(l.hd);
41 l = l.tl;
42 }
43 while(!isEmpty(l)){
44 print(", ");
45 p(l.hd);
46 l = l.tl;
47 }
48 print("]\n");
49 }
50
51 main(){
52 var num = "-1234";
53
54 printList(print, 1 : 2 : []);
55 }