6ac619916e37548fb58eb02b40ee13a27c575b82
[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 printList(p, l) {
53 print('[');
54 if(!isEmpty(l)){
55 p(l.hd);
56 l = l.tl;
57 }
58 while(!isEmpty(l)){
59 print(", ");
60 p(l.hd);
61 l = l.tl;
62 }
63 print("]\n");
64 }
65
66 printInt(x) {
67 [Char] l = [];
68 if(x < 0){
69 print('-');
70 x = -x;
71 }
72 while(x > 0){
73 l = toChar(x % 10) : l;
74 x = x / 10;
75 }
76 print(l);
77 }
78
79 main(){
80 var num = "-1234";
81
82 printList(printInt, 32 : 4 : strToInt(num) : []);
83 }