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

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.DS_Store

View File

@@ -0,0 +1,21 @@
import Data.Array
binarysearch :: Integer -> Array Integer Integer -> Integer -> Integer -> Maybe Integer
binarysearch x arr low high
| low > high = Nothing
| mid > x = binarysearch x arr low (high - 1)
| mid < x = binarysearch x arr (low + 1) high
| otherwise = Just mid
where
mid = arr ! ((low + high) `div` 2)
find :: Integer -> Array Integer Integer -> Maybe Integer
find x arr = binarysearch x arr low high
where p = bounds arr
low = fst p
high = snd p
-- Usage
-- let arr = array (0, 4) [(i, i*i) | i <- [0..4]]
-- find 4 arr

View File

@@ -0,0 +1,16 @@
helper :: (Integer, [Integer]) -> Integer -> (Integer, [Integer])
helper (min, xs) e
| min <= e = (min, e : xs)
| otherwise = (e, min : xs)
minandrest :: [Integer] -> (Integer, [Integer])
minandrest (x:xs) = foldl helper (x, []) xs
selectionsort :: [Integer] -> [Integer]
selectionsort [] = []
selectionsort xs = min : selectionsort rest
where (min, rest) = minandrest xs
-- Usage
-- selectionsort [1,4,2,3,5,6,0,7,8,9,1,9,0]

View File

@@ -0,0 +1,9 @@
countdown :: Integer -> IO()
countdown n
| n < 0 = return ()
| otherwise = do
putStrLn (show n)
countdown (n-1)
main = do
countdown 5

View File

@@ -0,0 +1,12 @@
greet2 name = putStrLn ("how are you, " ++ name ++ "?")
bye = putStrLn "ok bye!"
greet name = do
putStrLn ("hello " ++ name)
greet2 name
putStrLn "getting ready to say bye..."
bye
main = greet "adit"

View File

@@ -0,0 +1,6 @@
fact n
| n <= 1 = 1
| otherwise = n * (fact (n-1))
main = (putStrLn . show . fact) 5

View File

@@ -0,0 +1,5 @@
sumarr [] = 0
sumarr (x: xs) = x + sumarr xs
main = putStrLn (show (sumarr [1,2,3,4]))

View File

@@ -0,0 +1,5 @@
countarr [] = 0
countarr (x:xs) = 1 + countarr xs
main = (putStrLn . show . countarr) [0, 1, 2, 3, 4, 5]

View File

@@ -0,0 +1,6 @@
maxarr (x: []) = x
maxarr (x:xs) = if x > maxofrest then x else maxofrest
where maxofrest = maxarr xs
main = (putStrLn . show . maxarr) [1, 5, 10, 25, 16, 1]

View File

@@ -0,0 +1,8 @@
import Data.List
quicksort [] = []
quicksort (x: []) = [x]
quicksort (x:xs) = (quicksort lessthan) ++ [x] ++ (quicksort greaterthan)
where (lessthan, greaterthan) = partition (<= x) xs
main = (putStrLn . show . quicksort) [0,9,4,5,6,3,1,0,1]

View File

@@ -0,0 +1,5 @@
import qualified Data.HashMap.Strict as Map
prices = Map.fromList [("Apple", 0.67), ("Milk", 1.49), ("Avocado", 1.55)]
main = putStrLn (show (Map.lookup "Apple" prices))

View File

@@ -0,0 +1,17 @@
import qualified Data.HashMap.Strict as Map
all_voters = Map.fromList[("tom", True), ("mike", True)]
check_voter name voters_map = case Map.lookup name voters_map of
Just _ -> ("kick them out!", voters_map)
otherwise -> ("let them vote!", Map.insert name True voters_map)
log_vote name voters_map = do
putStrLn str
return new_voters_map
where (str, new_voters_map) = check_voter name voters_map
main = do
try_mike <- log_vote "mike" all_voters
try_john <- log_vote "john" try_mike
return try_john

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