hoi
authorMart Lubbers <mart@martlubbers.net>
Sun, 29 May 2016 18:52:28 +0000 (20:52 +0200)
committerMart Lubbers <mart@martlubbers.net>
Sun, 29 May 2016 18:52:28 +0000 (20:52 +0200)
examples/higher.spl
examples/parsing.spl [new file with mode: 0644]

index 1237743..5e3429f 100644 (file)
@@ -36,23 +36,9 @@ intList(x){
        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))));
 }
diff --git a/examples/parsing.spl b/examples/parsing.spl
new file mode 100644 (file)
index 0000000..96160bd
--- /dev/null
@@ -0,0 +1,55 @@
+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 : []);
+}