Skip to content

Instantly share code, notes, and snippets.

@wilkes
Created June 10, 2009 04:09
Show Gist options
  • Save wilkes/127014 to your computer and use it in GitHub Desktop.
Save wilkes/127014 to your computer and use it in GitHub Desktop.
{-
public void take(int v) {
if (currentMode == Mode.accumulating) {
int digits = (int)Math.pow(10, (int)Math.log10(v) + 1);
int x = stack.pop();
x *= digits;
x += v;
stack.push(x);
}
if (currentMode == Mode.replacing) {
stack.pop();
stack.push(v);
currentMode = Mode.accumulating;
}
if (currentMode == Mode.inserting) {
int top = stack.peek();
stack.push(top);
stack.push(v);
currentMode = Mode.accumulating;
}
}
-}
data CurrentMode = Accumulating
| Replacing
| Inserting
deriving Show
takeR :: Int -> (CurrentMode, [Int]) -> (CurrentMode, [Int])
takeR v (Inserting , []) = (Accumulating, [v])
takeR v (mode , []) = error $ "Empty stack not supported for mode: " ++ (show mode)
takeR v (mode , (x:stack)) = (Accumulating, newStack mode)
where
newStack Accumulating = (x * digits + v):stack
newStack Replacing = x:stack
newStack Inserting = v:x:stack
digits = 10 ^ (1 + (log10 $ realToFrac v))
log10 y = floor $ logBase 10 y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment