[Haskell-cafe] Producing MinimumValue

Colin DeVilbiss cdevilbiss at gmail.com
Thu Jul 19 16:43:12 EDT 2007


On 7/19/07, Alexteslin <alexteslin at yahoo.co.uk> wrote:
> What wrong with my original solution?
>
> allEqual2 :: [Int] -> Bool
> allEqual2 xs = length xs == length (filter isEqual xs)
> 	where
> 	isEqual n = (head xs) == n
>
> It looks simpler to me

I believe it's correct, but the use of "length" and "filter" seems forced.

For example, for a very long list that has the second element
mismatch, the versions with explicit recursion (or fold variants) will
terminate with False as soon as a mismatch is found; the "length"
version will not terminate until both have been fully traversed.

If you're allowed to use everything, perhaps the clearest would be:

allEqual3 [] = True
allEqual3 (x:xs) = all (== x) xs

(with "all" built on top of a fold.)

Colin


More information about the Haskell-Cafe mailing list