From d378cc45259379f3f51d8ccd5a1df8a5699a70ce Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 22 Jan 2019 13:48:57 -0800 Subject: [PATCH] Clean up HW2 code. --- HW2.fedorind.hs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/HW2.fedorind.hs b/HW2.fedorind.hs index bfc4dcb..b303f29 100644 --- a/HW2.fedorind.hs +++ b/HW2.fedorind.hs @@ -32,7 +32,7 @@ instance Functor Tree where -- | Map a function over a tree. Applies the given function to every label -- in the tree, preserving the tree's structure. --- +-- -- >>> mapTree odd 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 -- Nothing -- + pathTo :: Eq a => a -> Tree a -> Maybe Path pathTo v End = Nothing pathTo v (Node a l r) = orElse currentNode $ orElse (pathHelper v l L) $ pathHelper v r R where currentNode = if a == v then Just [] else Nothing - pathHelper v tree dir = (pathTo v tree) >>= (Just . (dir:)) - orElse m1 m2 = case m1 of - Just _ -> m1 - Nothing -> m2 + pathHelper v tree dir = fmap (dir:) (pathTo v tree) + orElse m1 m2 = if isJust m1 then m1 else m2 + isJust mx = mx /= Nothing