-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/fasteffekt-list' into feature/fasteffekt
- Loading branch information
Showing
2 changed files
with
90 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import examples/fasteffekt/benchmark | ||
|
||
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) | ||
} | ||
|
||
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 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)) => | ||
val yChild : Option[Element] = yy.child; | ||
val zChild : Option[Element] = zz.child; | ||
val xChild : Option[Element] = xx.child; | ||
|
||
val xxx: Option[Element] = tail(xChild, y, z); | ||
val yyy: Option[Element] = tail(yChild, z, x); | ||
val zzz: Option[Element] = tail(zChild, x, y); | ||
tail( | ||
xxx, | ||
yyy, | ||
zzz | ||
) | ||
case _ => panic("oh no!"); | ||
} | ||
} else { | ||
z; | ||
} | ||
} | ||
|
||
def List(_: Int) = { | ||
def benchmark(): Int = { | ||
val result = | ||
tail( | ||
makeList(15), | ||
makeList(10), | ||
makeList(6) | ||
); | ||
length(result); | ||
} | ||
|
||
|
||
def verifyResult(result: Int): Boolean = { | ||
return result == 10; | ||
} | ||
|
||
return innerBenchmarkLoop(1){benchmark}{verifyResult}; | ||
} | ||
|
||
def main() = { | ||
List(0); | ||
} |