I will simplify your question since you do not provide types for some of my functions:
How do you write a function to get the minimum of a list, given the known lower bound of the minimum?
Such a function has the type:
minimum' :: (Ord a) => a -> [a] -> a
... where the first argument is the known lower bound (i.e. 1 choice), and the second argument is the search list.
We can define this function by composing two simpler functions. The first function simply lazily takes all the elements from the list until it reaches the bottom border or the end of the list:
chop :: (Ord a) => a -> [a] -> [a] chop minElem as = let (prefix, suffix) = span (> minElem) as in case suffix of [] -> prefix s:uffix -> prefix ++ [s]
Then we will compose it with a minimum:
minimum' minElem = minimum . chop minElem
This is a common pattern in functional programming: creating simple solutions to solve complex problems.
source share