Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. import Data.Maybe
  2. data Tree a = Empty | Node a [Tree a]
  3.  
  4. depthFirstSearch _ Empty = Nothing
  5. depthFirstSearch needle (Node val children) =
  6. if needle == val then Just needle
  7. else let results = dropWhile isNothing $
  8. map (depthFirstSearch needle) children
  9. in if null results then Nothing else head results
  10.  
  11. main = do
  12. needle <- getLine
  13. let tree = Node "a" [Node "b" [Node "d" [Empty], Node "e" [Empty], Node "f" [Empty]], Node "c" [Node "g" [Node "h" [Empty]]]]
  14. putStrLn $ show $ depthFirstSearch needle tree
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement