You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.1 KiB

-- Example: maximum in list
maximum :: (Ord a) -> [a] -> a
maximum [] = error "maximum of empty list"
maximum [x] = x
maximum (x:xs) = max x (maximum xs)
-- Another example, replicate
replicate (Num i, Ord i) => i -> a -> [a]
replicate n x
| n <= 0 = []
| otherwise = x:(replicate (n-1) x)
-- Now, take
take :: (Num i, Ord i) => i -> [a] -> [a]
take n _
| n <= 0 = [] -- if n is 0 or less, empty list
-- if guard is non-exhaustive, matching falls through to next pattern
take _ [] = []
take n (x:xs) = x:(take (n-1) xs)
reverse :: [a] -> [a]
reverse [] = []
reverse (x:xs) = (reverse xs) ++ [x]
zip :: [a] -> [b] -> [(a,b)]
zip _ [] = []
zip [] _ = []
zip (x:xs) (y:ys) = (x,y):(zip xs, ys)
elem :: (Eq a) => a -> [a] -> Bool
elem _ [] = False
elem a (x:xs)
| a == x = True
| otherwise = a `elem` xs
-- Double recursion!
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) = let smallerSorted = quicksort [ a | a <- xs, a <= x ] in
let biggerSorted = quicksort [ a | a <- xs, a > x ] in
smallerSorted ++ [x] ++ biggerSorted