Skip to content

Instantly share code, notes, and snippets.

@edofic
Created June 25, 2014 16:23
Show Gist options
  • Save edofic/904785b50274a35bd742 to your computer and use it in GitHub Desktop.
Save edofic/904785b50274a35bd742 to your computer and use it in GitHub Desktop.
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
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