From b229d04579f2b07eb1d5e51e7c5a9743989d2246 Mon Sep 17 00:00:00 2001 From: pimjager Date: Thu, 9 Jun 2016 14:32:14 +0200 Subject: [PATCH] examples --- examples/factorize.spl | 63 ++++++++++++++++++++++++++++++++++++++++++ examples/higher.spl | 18 ++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 examples/factorize.spl 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: ", -- 2.20.1