diff --git a/examples/fasteffekt/list.effekt b/examples/fasteffekt/list.effekt index f194ea3e6..2a2ac3ecc 100644 --- a/examples/fasteffekt/list.effekt +++ b/examples/fasteffekt/list.effekt @@ -1,9 +1,20 @@ +import examples/fasteffekt/benchmark + + // class Element { // constructor(v) { // this.val = v; // this.next = null; // } +record Element ( + value: Int, child: Option[Element] +) +def length(element: Option[Element]): Int = + element match { + case None() => return 0; + case Some(Element(value, child)) => return 1 + length(child) + } // length() { // if (this.next === null) { // return 1; @@ -12,62 +23,92 @@ // } // } -import examples/fasteffekt/benchmark +def isShorterThan(x: Option[Element], y: Option[Element]): Boolean = { + var xTail:Option[Element] = x; + var yTail:Option[Element] = y; + var run = true; + var bigger = false; + while (run) { + (xTail,yTail) match { + case (_, None()) => run = false; bigger = false; + case (None(), _) => run =false; bigger = true; + case (Some(xx),Some(yy)) => xTail = xx.child; yTail = yy.child; + } + } + return bigger; -def List() = { - println("run list benchmark"); +// while (yTail !== null) { +// if (xTail === null) { return true; } +// xTail = xTail.next; +// yTail = yTail.next; +// } +// return false; - def benchmark(): Int = { - var result = 0; - // arr = emptyArray[Int](6); - - // each(0,arr.size()){ i => - // put(arr,i, 1); - // } - // permute(6); - return count; +} + +def makeList(lenght: Int): Option[Element] = { + return if(lenght == 0) { + None() + } else { + Some(Element(0, makeList(lenght-1))) + } +} + + //no idea what that function does. + def tail(x: Option[Element], y: Option[Element], z: Option[Element]): Option[Element] = { + if (y.isShorterThan(x)) { + (x,y,z) match { + case (Some(xx),Some(yy),Some(zz)) => + //compiler KNOWS that xx,yy,zz are not None() => can access children. + + //all children have type Option[Element] + val xChild = xx.child; + val yChild = yy.child; + val zChild = zz.child; + + val xxx = tail(xChild, y, z); + val yyy = tail(yChild, z, x); + val zzz = tail(zChild, x, y); + tail( + xxx, + yyy, + zzz + ) + case _ => panic("oh no!"); + } + } else { + z; + } } - def makeList(lenght: int) = { +def List() = { + println("run list benchmark"); + def benchmark(): Int = { + val result = + tail( + makeList(15), + makeList(10), + makeList(6) + ); + length(result); } + + def verifyResult(result: Int): Boolean = { return result == 10; } - return 0; + return innerBenchmarkLoop(1){benchmark}{verifyResult}; } -// class List extends Benchmark { -// benchmark() { -// const result = this.tail( -// this.makeList(15), -// this.makeList(10), -// this.makeList(6) -// ); -// return result.length(); -// } -// makeList(length) { -// if (length === 0) { -// return null; -// } -// const e = new Element(length); -// e.next = this.makeList(length - 1); -// return e; -// } +def main() = { + List(); +} +// class List extends Benchmark { -// isShorterThan(x, y) { -// let xTail = x; -// let yTail = y; -// while (yTail !== null) { -// if (xTail === null) { return true; } -// xTail = xTail.next; -// yTail = yTail.next; -// } -// return false; -// } // tail(x, y, z) { // if (this.isShorterThan(y, x)) {