Skip to content

Instantly share code, notes, and snippets.

@roag
Created November 30, 2008 23:20
Show Gist options
  • Save roag/30553 to your computer and use it in GitHub Desktop.
Save roag/30553 to your computer and use it in GitHub Desktop.
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