Created
November 30, 2008 23:20
-
-
Save roag/30553 to your computer and use it in GitHub Desktop.
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
data Btree a = ND | Data a | Branch (Btree a) (Btree a) | |
deriving (Show,Eq) | |
data Dir = L | R | |
deriving (Show,Eq) | |
type Path = [Dir] | |
--- Part a) | |
extract :: Path -> Btree a -> Error a | |
-- function returns the data at the end of the path | |
extract p ND=Fail | |
extract [] (Data a)=Ok(a) | |
extract (L:p) (Branch t1 t2)=extract p t1 | |
extract (R:p) (Branch t1 t2)=extract p t2 | |
extract _ _= Fail | |
--- Part b) | |
add :: a -> Path -> Btree a -> Error (Btree a) | |
add d [] (Data a)=Fail | |
add d [] (Branch t1 t2)=Fail | |
add d [] (ND)= Ok(Data d) | |
add d (L:p) (Branch t1 t2)=add d p t1 | |
add d (R:p) (Branch t1 t2)=add d p t2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment