I wrote the following split
function. It takes a list
, an element
and an Ordering (LT, GT, EQ)
. For LT
, the list will filter out all items that are >= the element argument.
split' :: (Ord a) => [a] -> a -> Ordering -> [a]
split' [] _ _ = []
split' (x:xs) a LT
| x < a = x : split' xs a LT
| otherwise = split' xs a LT
split' (x:xs) a GT
| x > a = x : split' xs a GT
| otherwise = split' xs a GT
-- non-exhaustive, unacceptable
Example:
*Main> split' [1,2,3] 2 LT
[1]
Note that this function can result in an exception if the Ordering
argument is EQ
. Of course this is terrible.
However, please review the above code, offering suggestions on how to improve it.