-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.hs
More file actions
28 lines (24 loc) · 793 Bytes
/
recursion.hs
File metadata and controls
28 lines (24 loc) · 793 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-- recursive with matching (in the end we have simple max function)
maximum_ :: (Ord a) => [a] -> a
maximum_ [] = error "maximum of empty list!"
maximum_ [x] = x
maximum_ (x:xs) = max x (maximum_ xs)
-- reverse array
reverse_ :: [a] -> [a]
reverse_ [] = []
reverse_ (x:xs) = reverse_ xs ++ [x]
-- infinite repeat (add x as first element of returned array from repeat_)
repeat_ :: a -> [a]
repeat_ x = x:repeat_ x
-- zip
zip_ :: [a] -> [b] -> [(a, b)]
zip_ _ [] = []
zip_ [] _ = []
zip_ (x:xs) (y:ys) = (x,y):zip_ xs ys
-- quicksort (first element into center)
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
let smallerOrEqual = [a | a <- xs, a <= x]
larger = [a | a <- xs, a > x]
in quicksort smallerOrEqual ++ [x] ++ quicksort larger