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
This commit is contained in:
committed by
Aditya Bhargava
parent
c5c2563d05
commit
6f78bdf3d7
25
06_breadth-first_search/Haskell/01_bfs.hs
Normal file
25
06_breadth-first_search/Haskell/01_bfs.hs
Normal file
@@ -0,0 +1,25 @@
|
||||
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"]) []))
|
||||
Reference in New Issue
Block a user