From: pimjager Date: Thu, 9 Jun 2016 12:32:14 +0000 (+0200) Subject: examples X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=b229d04579f2b07eb1d5e51e7c5a9743989d2246;p=cc1516.git examples --- diff --git a/examples/factorize.spl b/examples/factorize.spl new file mode 100644 index 0000000..0cecd10 --- /dev/null +++ b/examples/factorize.spl @@ -0,0 +1,63 @@ +reverse (list) :: [Int] -> [Int] { + var accu = []; + while (! isEmpty (list)) { + accu = list . hd : accu; + list = list . tl; + } + return accu; +} + +coPrimeWith (elem, list) :: Int -> [Int] -> Bool { + if (isEmpty(list)) { + return True; + } else { + if (elem % list.hd == 0) { + return False; + } else { + return coPrimeWith(elem, list.tl); + } + } +} + +factorize (n) :: Int -> [Int] { + var primes = []; + var i = 2; + while (i < n) { + if (n % i == 0 && coPrimeWith(i, primes)) { + print("Found prime: ", i); + primes = i : primes; + } + i = i + 1; + } + return primes; +} + +print_list_inner (list) :: [Int] -> Void { + if (isEmpty(list)) { + return; + } else { + print(list.hd); + if (!isEmpty(list.tl)) { + print(','); + print(' '); + } + return print_list_inner(list.tl); + } +} + +print_list (list) :: [Int] -> Void { + print('['); + print_list_inner(list); + print(']'); + print(' +'); + return; +} + +main (){ + //var number = 692963517; + var number = 12000; + var result = reverse(factorize(number)); + print_list(result); + return; +} \ No newline at end of file diff --git a/examples/higher.spl b/examples/higher.spl index 3e923d7..75d5d9b 100644 --- a/examples/higher.spl +++ b/examples/higher.spl @@ -34,8 +34,26 @@ intList(x){ } } +giveF(){ + return (\x->x+1); +} + + + main(){ var x = 5; + + var f = giveF(); + + var plus = \x y->x+y; + + var g = map(plus, [1,2,3]); + var is = map(\f->f(1), g); + + //print("is.hd", is.hd); + + print("f(4) = ", f(4)); + 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("sum of 0..12 but only the evens: ",