Skip to content

Commit

Permalink
refactor towers, get rid of obsolete pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
IR0NSIGHT committed Feb 12, 2024
1 parent 84db43a commit 4f8671c
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/effekt/benchmark/towers.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import src/effekt/benchmark
import immutable/list
import src/effekt/cliRunner

//each list represents one smallerDisk.
type SingleDisk = Int
type StackOfDisks = List[SingleDisk]
type TowerSetup = Array[StackOfDisks]
Expand All @@ -12,29 +11,25 @@ def Towers() = {
var piles: TowerSetup = emptyArray[StackOfDisks](0);
var movesDone: Int = 0;

def pushDisk(piles: TowerSetup, smallerDisk: SingleDisk, pileIdx: Int): StackOfDisks = {
val currentTopDisk: StackOfDisks = piles.unsafeGet(pileIdx);
currentTopDisk match {
case Cons(largerSize,_) =>
if (smallerDisk >= largerSize) {
panic("Cannot put a big smallerDisk on a smaller one:"++show(piles));
def pushDisk(piles: TowerSetup, newTopDiskOnStack: SingleDisk, pileIdx: Int): Unit = {
val stack: StackOfDisks = piles.unsafeGet(pileIdx);
stack match {
case Cons(topDiskOnStack,_) =>
if (newTopDiskOnStack >= topDiskOnStack) {
panic("Cannot put a big disk onto a smaller one:"++show(piles));
}
}

//push current top disk one down, put smaller on top
val replacer = smallerDisk match { //smallerDisk.next = currentTopDisk
case smallerSize =>
Cons(smallerSize, currentTopDisk)
}
put(piles,pileIdx, replacer);
return currentTopDisk
val updatedStack = Cons(newTopDiskOnStack, stack)
put(piles,pileIdx, updatedStack);
}

def popDiskFrom(pileIdx: Int): SingleDisk = {
val currentTopDisk = piles.unsafeGet(pileIdx);

currentTopDisk match {
case Nil() => panic("Attempting to remove a smallerDisk from an empty pileIdx");
case Nil() => panic("Attempting to remove a newTopDiskOnStack from an empty pileIdx");
case Cons(size, next) =>
put(piles, pileIdx, next)
return size
Expand Down

0 comments on commit 4f8671c

Please sign in to comment.