Clean up HW2 code.
This commit is contained in:
parent
eb8754f62d
commit
d378cc4525
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user