Skip to content

Commit 6f78bdf

Browse files
bijoythomasegonSchiele
authored andcommitted
Adding Haskell examples (egonSchiele#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
1 parent c5c2563 commit 6f78bdf

File tree

13 files changed

+136
-0
lines changed

13 files changed

+136
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Data.Array
2+
3+
binarysearch :: Integer -> Array Integer Integer -> Integer -> Integer -> Maybe Integer
4+
binarysearch x arr low high
5+
| low > high = Nothing
6+
| mid > x = binarysearch x arr low (high - 1)
7+
| mid < x = binarysearch x arr (low + 1) high
8+
| otherwise = Just mid
9+
where
10+
mid = arr ! ((low + high) `div` 2)
11+
12+
13+
find :: Integer -> Array Integer Integer -> Maybe Integer
14+
find x arr = binarysearch x arr low high
15+
where p = bounds arr
16+
low = fst p
17+
high = snd p
18+
19+
-- Usage
20+
-- let arr = array (0, 4) [(i, i*i) | i <- [0..4]]
21+
-- find 4 arr
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
helper :: (Integer, [Integer]) -> Integer -> (Integer, [Integer])
3+
helper (min, xs) e
4+
| min <= e = (min, e : xs)
5+
| otherwise = (e, min : xs)
6+
7+
minandrest :: [Integer] -> (Integer, [Integer])
8+
minandrest (x:xs) = foldl helper (x, []) xs
9+
10+
selectionsort :: [Integer] -> [Integer]
11+
selectionsort [] = []
12+
selectionsort xs = min : selectionsort rest
13+
where (min, rest) = minandrest xs
14+
15+
-- Usage
16+
-- selectionsort [1,4,2,3,5,6,0,7,8,9,1,9,0]

03_recursion/Haskell/01_countdown.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
countdown :: Integer -> IO()
2+
countdown n
3+
| n < 0 = return ()
4+
| otherwise = do
5+
putStrLn (show n)
6+
countdown (n-1)
7+
8+
main = do
9+
countdown 5

03_recursion/Haskell/02_greet.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
greet2 name = putStrLn ("how are you, " ++ name ++ "?")
3+
4+
bye = putStrLn "ok bye!"
5+
6+
greet name = do
7+
putStrLn ("hello " ++ name)
8+
greet2 name
9+
putStrLn "getting ready to say bye..."
10+
bye
11+
12+
main = greet "adit"

03_recursion/Haskell/03_factorial.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
fact n
3+
| n <= 1 = 1
4+
| otherwise = n * (fact (n-1))
5+
6+
main = (putStrLn . show . fact) 5
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
sumarr [] = 0
3+
sumarr (x: xs) = x + sumarr xs
4+
5+
main = putStrLn (show (sumarr [1,2,3,4]))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
countarr [] = 0
3+
countarr (x:xs) = 1 + countarr xs
4+
5+
main = (putStrLn . show . countarr) [0, 1, 2, 3, 4, 5]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
maxarr (x: []) = x
3+
maxarr (x:xs) = if x > maxofrest then x else maxofrest
4+
where maxofrest = maxarr xs
5+
6+
main = (putStrLn . show . maxarr) [1, 5, 10, 25, 16, 1]

04_quicksort/Haskell/05_quicksort.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Data.List
2+
3+
quicksort [] = []
4+
quicksort (x: []) = [x]
5+
quicksort (x:xs) = (quicksort lessthan) ++ [x] ++ (quicksort greaterthan)
6+
where (lessthan, greaterthan) = partition (<= x) xs
7+
8+
main = (putStrLn . show . quicksort) [0,9,4,5,6,3,1,0,1]

0 commit comments

Comments
 (0)