From 6f78bdf3d7140ca50d3d026da707e7a7430c7db1 Mon Sep 17 00:00:00 2001 From: Bijoy Thomas Date: Sun, 11 Jun 2017 18:12:48 -0500 Subject: [PATCH] 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 --- .gitignore | 1 + .../Haskell/01_binarysearch.hs | 21 ++++++++++++++++ 02_selection_sort/Haskell/01_selectionsort.hs | 16 ++++++++++++ 03_recursion/Haskell/01_countdown.hs | 9 +++++++ 03_recursion/Haskell/02_greet.hs | 12 +++++++++ 03_recursion/Haskell/03_factorial.hs | 6 +++++ 04_quicksort/Haskell/01_02_loop_sum.hs | 5 ++++ 04_quicksort/Haskell/03_recursive_count.hs | 5 ++++ 04_quicksort/Haskell/04_recursive_max.hs | 6 +++++ 04_quicksort/Haskell/05_quicksort.hs | 8 ++++++ .../Haskell/01_price_of_groceries.hs | 5 ++++ 05_hash_tables/Haskell/02_check_voter.hs | 17 +++++++++++++ 06_breadth-first_search/Haskell/01_bfs.hs | 25 +++++++++++++++++++ 13 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100644 01_introduction_to_algorithms/Haskell/01_binarysearch.hs create mode 100644 02_selection_sort/Haskell/01_selectionsort.hs create mode 100644 03_recursion/Haskell/01_countdown.hs create mode 100644 03_recursion/Haskell/02_greet.hs create mode 100644 03_recursion/Haskell/03_factorial.hs create mode 100644 04_quicksort/Haskell/01_02_loop_sum.hs create mode 100644 04_quicksort/Haskell/03_recursive_count.hs create mode 100644 04_quicksort/Haskell/04_recursive_max.hs create mode 100644 04_quicksort/Haskell/05_quicksort.hs create mode 100644 05_hash_tables/Haskell/01_price_of_groceries.hs create mode 100644 05_hash_tables/Haskell/02_check_voter.hs create mode 100644 06_breadth-first_search/Haskell/01_bfs.hs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/01_introduction_to_algorithms/Haskell/01_binarysearch.hs b/01_introduction_to_algorithms/Haskell/01_binarysearch.hs new file mode 100644 index 0000000..923f209 --- /dev/null +++ b/01_introduction_to_algorithms/Haskell/01_binarysearch.hs @@ -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 \ No newline at end of file diff --git a/02_selection_sort/Haskell/01_selectionsort.hs b/02_selection_sort/Haskell/01_selectionsort.hs new file mode 100644 index 0000000..f5e301d --- /dev/null +++ b/02_selection_sort/Haskell/01_selectionsort.hs @@ -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] \ No newline at end of file diff --git a/03_recursion/Haskell/01_countdown.hs b/03_recursion/Haskell/01_countdown.hs new file mode 100644 index 0000000..bb22e27 --- /dev/null +++ b/03_recursion/Haskell/01_countdown.hs @@ -0,0 +1,9 @@ +countdown :: Integer -> IO() +countdown n + | n < 0 = return () + | otherwise = do + putStrLn (show n) + countdown (n-1) + +main = do + countdown 5 \ No newline at end of file diff --git a/03_recursion/Haskell/02_greet.hs b/03_recursion/Haskell/02_greet.hs new file mode 100644 index 0000000..0f29e6e --- /dev/null +++ b/03_recursion/Haskell/02_greet.hs @@ -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" \ No newline at end of file diff --git a/03_recursion/Haskell/03_factorial.hs b/03_recursion/Haskell/03_factorial.hs new file mode 100644 index 0000000..e3f9c90 --- /dev/null +++ b/03_recursion/Haskell/03_factorial.hs @@ -0,0 +1,6 @@ + +fact n + | n <= 1 = 1 + | otherwise = n * (fact (n-1)) + +main = (putStrLn . show . fact) 5 \ No newline at end of file diff --git a/04_quicksort/Haskell/01_02_loop_sum.hs b/04_quicksort/Haskell/01_02_loop_sum.hs new file mode 100644 index 0000000..ca6e1ee --- /dev/null +++ b/04_quicksort/Haskell/01_02_loop_sum.hs @@ -0,0 +1,5 @@ + +sumarr [] = 0 +sumarr (x: xs) = x + sumarr xs + +main = putStrLn (show (sumarr [1,2,3,4])) \ No newline at end of file diff --git a/04_quicksort/Haskell/03_recursive_count.hs b/04_quicksort/Haskell/03_recursive_count.hs new file mode 100644 index 0000000..e6c51ba --- /dev/null +++ b/04_quicksort/Haskell/03_recursive_count.hs @@ -0,0 +1,5 @@ + +countarr [] = 0 +countarr (x:xs) = 1 + countarr xs + +main = (putStrLn . show . countarr) [0, 1, 2, 3, 4, 5] \ No newline at end of file diff --git a/04_quicksort/Haskell/04_recursive_max.hs b/04_quicksort/Haskell/04_recursive_max.hs new file mode 100644 index 0000000..65e2011 --- /dev/null +++ b/04_quicksort/Haskell/04_recursive_max.hs @@ -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] \ No newline at end of file diff --git a/04_quicksort/Haskell/05_quicksort.hs b/04_quicksort/Haskell/05_quicksort.hs new file mode 100644 index 0000000..26e1e4a --- /dev/null +++ b/04_quicksort/Haskell/05_quicksort.hs @@ -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] \ No newline at end of file diff --git a/05_hash_tables/Haskell/01_price_of_groceries.hs b/05_hash_tables/Haskell/01_price_of_groceries.hs new file mode 100644 index 0000000..9aaf4e7 --- /dev/null +++ b/05_hash_tables/Haskell/01_price_of_groceries.hs @@ -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)) \ No newline at end of file diff --git a/05_hash_tables/Haskell/02_check_voter.hs b/05_hash_tables/Haskell/02_check_voter.hs new file mode 100644 index 0000000..98003e5 --- /dev/null +++ b/05_hash_tables/Haskell/02_check_voter.hs @@ -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 \ No newline at end of file diff --git a/06_breadth-first_search/Haskell/01_bfs.hs b/06_breadth-first_search/Haskell/01_bfs.hs new file mode 100644 index 0000000..20a9305 --- /dev/null +++ b/06_breadth-first_search/Haskell/01_bfs.hs @@ -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"]) [])) \ No newline at end of file