Clean up HW2 code.

This commit is contained in:
Danila Fedorin 2019-01-22 13:48:57 -08:00
parent eb8754f62d
commit d378cc4525
1 changed files with 5 additions and 5 deletions

View File

@ -32,7 +32,7 @@ instance Functor Tree where
-- | Map a function over a tree. Applies the given function to every label -- | Map a function over a tree. Applies the given function to every label
-- in the tree, preserving the tree's structure. -- in the tree, preserving the tree's structure.
-- --
-- >>> mapTree odd End -- >>> mapTree odd End
-- End -- End
-- --
@ -92,12 +92,12 @@ valueAt (x:xs) (Node a l r) = valueAt xs $ if x == L then l else r
-- >>> pathTo 10 ex -- >>> pathTo 10 ex
-- Nothing -- Nothing
-- --
pathTo :: Eq a => a -> Tree a -> Maybe Path pathTo :: Eq a => a -> Tree a -> Maybe Path
pathTo v End = Nothing pathTo v End = Nothing
pathTo v (Node a l r) = orElse currentNode $ orElse (pathHelper v l L) $ pathHelper v r R pathTo v (Node a l r) = orElse currentNode $ orElse (pathHelper v l L) $ pathHelper v r R
where where
currentNode = if a == v then Just [] else Nothing currentNode = if a == v then Just [] else Nothing
pathHelper v tree dir = (pathTo v tree) >>= (Just . (dir:)) pathHelper v tree dir = fmap (dir:) (pathTo v tree)
orElse m1 m2 = case m1 of orElse m1 m2 = if isJust m1 then m1 else m2
Just _ -> m1 isJust mx = mx /= Nothing
Nothing -> m2