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:
Bijoy Thomas
2017-06-11 18:12:48 -05:00
committed by Aditya Bhargava
parent c5c2563d05
commit 6f78bdf3d7
13 changed files with 136 additions and 0 deletions

View 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"]) []))