Skip to content

Instantly share code, notes, and snippets.

@ruicc
Last active August 29, 2015 14:01
Show Gist options
  • Save ruicc/5435acba4be89aed7d6a to your computer and use it in GitHub Desktop.
Save ruicc/5435acba4be89aed7d6a to your computer and use it in GitHub Desktop.
An Env Comonad example
import Control.Comonad.Env
data Bin a = Node a (Bin a) (Bin a) | Leaf a
deriving Show
bintree_ex :: Bin Int
bintree_ex = Node 5 (Leaf 3) (Node 8 (Leaf 7) (Leaf 10))
main :: IO ()
main = do
(_e, v) <- return $ runEnv $
(env bintree_ex ()) =>> selectW 3
print v
selectW :: Int -> Env (Bin Int) a -> Maybe Int
selectW n w =
extract $
w =>>
ask =>> \wbt ->
case extract wbt of
Leaf a
| a == n -> Just a
| otherwise -> Nothing
Node a l r
| n == a -> Just a
| n > a -> selectW n (env r ())
| otherwise -> selectW n (env l ())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment