From: pimjager Date: Thu, 26 May 2016 14:16:03 +0000 (+0200) Subject: fold tempTest X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=5e46faab0f5bc0510298717668c7d8c3d17257e8;p=cc1516.git fold tempTest --- diff --git a/examples/tempTest.spl b/examples/tempTest.spl index 23421f0..a62e6e2 100644 --- a/examples/tempTest.spl +++ b/examples/tempTest.spl @@ -1,17 +1,44 @@ -Let Int a = 4; +//Let Int a = 4;// -mapP1(xs) { - if(isEmpty(xs)) { +//mapP1(xs) { +// if(isEmpty(xs)) { +// return []; +// } else { +// return (xs.hd + 1) : mapP1(xs.tl); +// } +//} +//main() { +// [Int] x = []; +// [Int] y = []; +// Int z = a(); +// x = mapP1(x); +// y = mapP1(x); +// return a() + 5; +//} + +plus(x,y){ + return x+y; +} + +map(f, xs) { + if (isEmpty(xs)) { return []; } else { - return (xs.hd + 1) : mapP1(xs.tl); + return f(xs.hd) : map(f, xs.tl); + } +} + +foldr(f, acc, xs) { + if(isEmpty(xs)) { + return acc; + } else { + return foldr(f, f(xs.hd, acc), xs.tl); } } + main() { - [Int] x = []; - [Int] y = []; - Int z = a(); - x = mapP1(x); - y = mapP1(x); - return a() + 5; + var f = plus(1); + var z = map(f, 1:2:[]); + var x = foldr(plus, 0, 1:2:[]); + return; } \ No newline at end of file