Created
June 25, 2014 16:23
-
-
Save edofic/904785b50274a35bd742 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
up = (1:) | |
down = (-1:) | |
done = [] | |
states = scanl (+) 0 | |
minMaxEnd commands = | |
let s = states commands | |
in (minimum s, maximum s, last s) | |
example = up.up.up.down.down.up $ done | |
exampleStates = states example |
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
import Control.Applicative | |
import Control.Monad.Free | |
data Command a = Up a | Down a deriving (Eq, Show, Functor) | |
type Sequence = Free Command () | |
up, down :: Sequence -> Sequence | |
up = Free . Up | |
down = Free . Down | |
done :: Sequence | |
done = Pure () | |
go = ($done) | |
example1 = up . up . down . up $ done | |
example2 = do | |
go up | |
go up | |
go down | |
go up | |
-- example1 == example2 | |
toList :: Num a => Sequence -> [a] | |
toList = iter f . fmap (const []) where | |
f (Up xs) = 1 : xs | |
f (Down xs) = -1 : xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment