- # maximum.hs
- maximum' :: (Ord a) => [a] -> a
- maximum' [] = error "maximum of empty list!"
- maximum' [x] = x
- maximum' (x:xs) = max x (maximum' xs)
-
- ghci> maximum' [1, 3, 2, 7, 4]
- 7
- # replicate.hs
- replicate' :: Int -> a -> [a]
- replicate' n x
- | n <= 0 = []
- | otherwise = x : replicate' (n-1) x
-
- ghci> replicate' 3 'k'
- "kkk"
- #take.hs
- take' ::(Num i, Ord i) => i -> [a] -> [a]
- take' n _
- | n <= 0 = []
- take' _ [] = []
- take' n (x:xs) = x : take' (n-1) xs
-
- ghci> take' 3 "Hello"
- "Hel"
- # reverse.hs
- reverse' :: [a] -> [a]
- reverse' [] = []
- reverse' (x:xs) = reverse' xs ++ [x]
-
- ghci> reverse' "HelloWorld"
- "dlroWolleH"
- # repeat.hs
- repeat' :: [a] -> [a]
- repeat' x = x : repeat' x
- # zip.hs
- zip' :: [a] -> [b] -> [(a, b)]
- zip' _ [] = []
- zip' [] _ = []
- zip' (x:xs) (y:ys) = (x, y):zip' xs ys
-
- ghci> zip' [1, 2, 3] [4, 5]
- [(1,4),(2,5)]
- # elem.hs
- elem' :: (Eq a) => a -> [a] -> Bool
- elem' _ [] = False
- elem' a (x:xs)
- | a == x = True
- | otherwise = a `elem'` xs
-
- ghci> elem' 'e' "Hello World"
- True
- ghci> elem' 'z' "Hello World"
- False
- # quicksort.hs
- 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
-
- ghci> quicksort [1, 3, 2, 9, 7, 10, 13]
- [1,2,3,7,9,10,13]