From caf7016620bf4bf742d902454d7fcbdebfeb27e0 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Sun, 29 May 2016 20:52:28 +0200 Subject: [PATCH] hoi --- examples/higher.spl | 16 +------------ examples/parsing.spl | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 examples/parsing.spl diff --git a/examples/higher.spl b/examples/higher.spl index 1237743..5e3429f 100644 --- a/examples/higher.spl +++ b/examples/higher.spl @@ -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 index 0000000..96160bd --- /dev/null +++ b/examples/parsing.spl @@ -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 : []); +} -- 2.20.1