Let's imagine a dummy subset of Brainf*ck:
+
increments the counter
-
decrements the counter
A simple program:
program = "++++--" -- should evaluate to 2
And a stateful evaluation function:
eval :: Char -> State Int Char
eval '+' = do x <- get
put (x + 1)
return 'I'
eval '-' = do x <- get
put (x - 1)
return 'D'
How would you evaluate the program?
(Looks like a fold
to me but can't get my head around it, and it doesn't feel like it's the way to do it properly...)
You can use traverse_
from Data.Foldable
:
import Data.Foldable (traverse_)
execState (traverse_ eval "++++--") 0