fixed examples
[cc1516.git] / examples / printparse.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 toChar(x) :: Int -> Char{
16 if(x == 0){ return '0';
17 } else { if(x == 1){ return '1';
18 } else { if(x == 2){ return '2';
19 } else { if(x == 3){ return '3';
20 } else { if(x == 4){ return '4';
21 } else { if(x == 5){ return '5';
22 } else { if(x == 6){ return '6';
23 } else { if(x == 7){ return '7';
24 } else { if(x == 8){ return '8';
25 } else { if(x == 9){ return '9';
26 } else { return '0'; }}}}}}}}}}
27 }
28
29 isDigit(x) :: Char -> Bool {
30 return toInt(x) != -1;
31 }
32
33 strToInt(x) :: [Char] -> Int {
34 [Char] xs = x;
35 var i = 0;
36 var m = 1;
37 if(!isEmpty(x)){
38 if(xs.hd == '-'){
39 xs = xs.tl;
40 m = -1;
41 }
42
43 while(!isEmpty(xs)){
44 i = i*10 + toInt(xs.hd);
45 xs = xs.tl;
46 }
47 }
48 return i*m;
49 }
50
51 printList(p, l) :: (a -> Void) -> [a] -> Void {
52 print('[');
53 if(!isEmpty(l)){
54 p(l.hd);
55 l = l.tl;
56 }
57 while(!isEmpty(l)){
58 print(", ");
59 p(l.hd);
60 l = l.tl;
61 }
62 print("]\n");
63 }
64
65 printInt(x) {
66 [Char] l = [];
67 if(x < 0){
68 print('-');
69 x = -x;
70 }
71 while(x > 0){
72 l = toChar(x % 10) : l;
73 x = x / 10;
74 }
75 print(l);
76 }
77
78 printBool(b) :: Bool -> Void {
79 print(b);
80 }
81
82 main(){
83 var num = "-1234";
84
85 printList(printInt, [32, 4, strToInt(num)]);
86 printList(printBool, [True, False, True, True]);
87 }