Last active
August 29, 2015 14:01
-
-
Save ruicc/5435acba4be89aed7d6a to your computer and use it in GitHub Desktop.
An Env Comonad example
This file contains 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.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