Files
grokking_algorithms/06_breadth-first_search/Haskell/01_bfs.hs
Bijoy Thomas 6f78bdf3d7 Adding Haskell examples (#17)
* Adding binary search example for Haskell

* Adding selection sort example in Haskell

* Adding Haskell examples for chapter 3

* Adding examples for chapter 4

* Adding examples for chapter 5

* Adding git ignore

* Add Haskell example for BFS
2017-06-11 19:12:48 -04:00

25 lines
620 B
Haskell

import qualified Data.HashMap.Strict as Map
import Control.Applicative
graph = Map.fromList [
("you", ["alice", "bob", "claire"]),
("bob", ["anuj", "peggy"]),
("alice", ["peggy"]),
("claire", ["mangoes", "jonny"]),
("anuj", []),
("peggy", []),
("mangoes", []),
("jonny", [])
]
connections name = Map.lookup name graph
bfs tosearch searched = case tosearch of
Just (x: xs)
| x == "mangoes" -> Just x
| x `elem` searched -> bfs (Just xs) searched
| otherwise -> bfs ((++) <$> Just xs <*> (connections x)) (x : searched)
_ -> Nothing
main = do
putStrLn (show (bfs (Just ["you"]) []))