return l;
}
-printIntList(l) :: [Int] -> Void{
- print('[');
- if(!isEmpty(l)){
- print(l.hd);
- l = l.tl;
- }
- while(!isEmpty(l)){
- print(", ", l.hd);
- l = l.tl;
- }
- print("]\n");
-}
-
main(){
print("faculty of 5 is: ", foldr(\x y->x*y, 1, intList(5)));
print("sum of 1..5 is: ", foldr(\x y->x+y, 0, intList(5)));
print("filter evens from 0..12 is: ");
- printIntList(filter(\x->x%2 == 0, intList(12)));
- printIntList(1:2:3:[]);
+ print("sum of 0..12 but only the evens: ", foldr(\x y->x+y, 0, filter(\x->x%2 == 0, intList(12))));
}
--- /dev/null
+toInt(x) :: Char -> Int{
+ if(x == '0'){ return 0;
+ } else { if(x == '1'){ return 1;
+ } else { if(x == '2'){ return 2;
+ } else { if(x == '3'){ return 3;
+ } else { if(x == '4'){ return 4;
+ } else { if(x == '5'){ return 5;
+ } else { if(x == '6'){ return 6;
+ } else { if(x == '7'){ return 7;
+ } else { if(x == '8'){ return 8;
+ } else { if(x == '9'){ return 9;
+ } else { return -1; }}}}}}}}}}
+}
+
+isDigit(x) :: Char -> Bool {
+ return toInt(x) != -1;
+}
+
+strToInt(x) :: [Char] -> Int {
+ [Char] xs = x;
+ var i = 0;
+ var m = 1;
+ if(!isEmpty(x)){
+ if(xs.hd == '-'){
+ xs = xs.tl;
+ m = -1;
+ }
+
+ while(!isEmpty(xs)){
+ i = i*10 + toInt(xs.hd);
+ xs = xs.tl;
+ }
+ }
+ return i*m;
+}
+
+printList(p, l) {
+ print('[');
+ if(!isEmpty(l)){
+ p(l.hd);
+ l = l.tl;
+ }
+ while(!isEmpty(l)){
+ print(", ");
+ p(l.hd);
+ l = l.tl;
+ }
+ print("]\n");
+}
+
+main(){
+ var num = "-1234";
+
+ printList(print, 1 : 2 : []);
+}