From 01743ccd94207c3511195a4b974679c876411e72 Mon Sep 17 00:00:00 2001 From: pimjager Date: Thu, 2 Jun 2016 12:46:05 +0200 Subject: [PATCH] fixed examples --- examples/higher.spl | 87 ++++++++++++++++------------------------- examples/printparse.spl | 5 +-- 2 files changed, 35 insertions(+), 57 deletions(-) diff --git a/examples/higher.spl b/examples/higher.spl index 1df4a80..3e923d7 100644 --- a/examples/higher.spl +++ b/examples/higher.spl @@ -1,64 +1,43 @@ -//map(f, l){ -// if(isEmpty(l)){ -// return []; -// } else { -// return f(l.hd) : map(f, l.tl); -// } -//} -// -//foldr(f, acc, l){ -// if(isEmpty(l)){ -// return acc; -// } else { -// return foldr(f, f(acc, l.hd), l.tl); -// } -//} -// -//filter(f, l){ -// if(isEmpty(l)){ -// return []; -// } else { -// if(f(l.hd)){ -// return filter(f, l.tl); -// } else { -// return l.hd : filter(f, l.tl); -// } -// } -//} -// -//intList(x){ -// if(x <= 1){ -// return [x]; -// } else { -// return x : intList(x-1); -// } -//} - -plus3(x, y, z){ - return x + y; +map(f, l){ + if(isEmpty(l)){ + return []; + } else { + return f(l.hd) : map(f, l.tl); + } } -plus(x, y){ - return x+y; +foldr(f, acc, l){ + if(isEmpty(l)){ + return acc; + } else { + return foldr(f, f(acc, l.hd), l.tl); + } } -apply(f, x){ - return f(x); +filter(f, l){ + if(isEmpty(l)){ + return []; + } else { + if(f(l.hd)){ + return filter(f, l.tl); + } else { + return l.hd : filter(f, l.tl); + } + } } -ap2(f, g, x){ - return f(g(x)); +intList(x){ + if(x <= 1){ + return [x]; + } else { + return x : intList(x-1); + } } 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: "); -// print("sum of 0..12 but only the evens: ", -// foldr(\x y->x+y, 0, filter(\x->x%2 == 0, intList(12)))); - - var a = plus3; - var b = a(1); - var c = b(2); - var d = c(3); + var x = 5; + 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: ", + foldr(\x y->x+y, 0, filter(\x->x%2 == 0, intList(12)))); } diff --git a/examples/printparse.spl b/examples/printparse.spl index 607610f..68de2fb 100644 --- a/examples/printparse.spl +++ b/examples/printparse.spl @@ -48,8 +48,7 @@ strToInt(x) :: [Char] -> Int { return i*m; } -//printList(p, l) :: (a -> Void) -> [a] -> Void { -printList(p, l) { +printList(p, l) :: (a -> Void) -> [a] -> Void { print('['); if(!isEmpty(l)){ p(l.hd); @@ -84,5 +83,5 @@ main(){ var num = "-1234"; printList(printInt, [32, 4, strToInt(num)]); -// printList(printBool, [True, False, True, True]); + printList(printBool, [True, False, True, True]); } -- 2.20.1