I try to find a solution on an exercise of my own, with the following requirements :
- We need to move an object against a given sequence.
- A sequence is composed of actions.
Here are the possible actions : F, L, R
- F : move Forward
- L : rotate 90° to the left
- R : rotate 90° to the right
A sequence is then represented by a string, like this :
"FFLRLFF"
I want to parse the above sequence (and handle errors) and then bind each action to a function, like this :
parseAction :: Char -> Either String (a -> a)
parseAction 'F' = Right moveForward
parseAction 'L' = Right turnLeft
parseAction 'R' = Right turnRight
parseAction s = Left ("Unkown action : " ++ [s])
-- Implementation omitted
moveForward :: a -> a
turnLeft :: a -> a
turnRight :: a -> a
Now what I want is something with the following signature :
parseSequence :: String -> Either String [(a -> a)]
I want to parse a full sequence by using many times parseAction
function, and fails when that one returns Left. I'm stuck on how can I implement this function.
Do you have any ideas ?