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
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.DS_Store
|
||||||
21
01_introduction_to_algorithms/Haskell/01_binarysearch.hs
Normal file
21
01_introduction_to_algorithms/Haskell/01_binarysearch.hs
Normal 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
|
||||||
16
02_selection_sort/Haskell/01_selectionsort.hs
Normal file
16
02_selection_sort/Haskell/01_selectionsort.hs
Normal 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]
|
||||||
9
03_recursion/Haskell/01_countdown.hs
Normal file
9
03_recursion/Haskell/01_countdown.hs
Normal 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
|
||||||
12
03_recursion/Haskell/02_greet.hs
Normal file
12
03_recursion/Haskell/02_greet.hs
Normal 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"
|
||||||
6
03_recursion/Haskell/03_factorial.hs
Normal file
6
03_recursion/Haskell/03_factorial.hs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
fact n
|
||||||
|
| n <= 1 = 1
|
||||||
|
| otherwise = n * (fact (n-1))
|
||||||
|
|
||||||
|
main = (putStrLn . show . fact) 5
|
||||||
5
04_quicksort/Haskell/01_02_loop_sum.hs
Normal file
5
04_quicksort/Haskell/01_02_loop_sum.hs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
sumarr [] = 0
|
||||||
|
sumarr (x: xs) = x + sumarr xs
|
||||||
|
|
||||||
|
main = putStrLn (show (sumarr [1,2,3,4]))
|
||||||
5
04_quicksort/Haskell/03_recursive_count.hs
Normal file
5
04_quicksort/Haskell/03_recursive_count.hs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
countarr [] = 0
|
||||||
|
countarr (x:xs) = 1 + countarr xs
|
||||||
|
|
||||||
|
main = (putStrLn . show . countarr) [0, 1, 2, 3, 4, 5]
|
||||||
6
04_quicksort/Haskell/04_recursive_max.hs
Normal file
6
04_quicksort/Haskell/04_recursive_max.hs
Normal 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]
|
||||||
8
04_quicksort/Haskell/05_quicksort.hs
Normal file
8
04_quicksort/Haskell/05_quicksort.hs
Normal 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]
|
||||||
5
05_hash_tables/Haskell/01_price_of_groceries.hs
Normal file
5
05_hash_tables/Haskell/01_price_of_groceries.hs
Normal 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))
|
||||||
17
05_hash_tables/Haskell/02_check_voter.hs
Normal file
17
05_hash_tables/Haskell/02_check_voter.hs
Normal 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
|
||||||
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