Created
June 10, 2009 04:09
-
-
Save wilkes/127014 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{- | |
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