Clean up HW2 a bit more.

This commit is contained in:
Danila Fedorin 2019-01-28 23:19:29 -08:00
parent d378cc4525
commit c46e7b67e3

View File

@ -73,7 +73,7 @@ mapTree = fmap
valueAt :: Path -> Tree a -> Maybe a valueAt :: Path -> Tree a -> Maybe a
valueAt _ End = Nothing valueAt _ End = Nothing
valueAt [] (Node a _ _) = Just a valueAt [] (Node a _ _) = Just a
valueAt (x:xs) (Node a l r) = valueAt xs $ if x == L then l else r valueAt (x:xs) (Node _ l r) = valueAt xs $ if x == L then l else r
-- | Find a path to a node that contains the given value. -- | Find a path to a node that contains the given value.
-- --
@ -94,10 +94,10 @@ valueAt (x:xs) (Node a l r) = valueAt xs $ if x == L then l else r
-- --
pathTo :: Eq a => a -> Tree a -> Maybe Path pathTo :: Eq a => a -> Tree a -> Maybe Path
pathTo v End = Nothing pathTo _ 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 = fmap (dir:) (pathTo v tree) pathHelper _ tree dir = fmap (dir:) (pathTo v tree)
orElse m1 m2 = if isJust m1 then m1 else m2 orElse m1 m2 = if isJust m1 then m1 else m2
isJust mx = mx /= Nothing isJust mx = mx /= Nothing