
The length command is too greedy. It wants to check the entire length of both lists, and only then compare. If you did this by hand, you would know to quit at the first nonequal element. To do this in Haskell, you should generally avoid "length". Lazier functions such as "and" stop as soon as they can: and (map isEqual xs) doesn't really map to every xs. The "and" keeps asking map to check the next element for equality, but stops asking when it gets a negative answer. length doesn't know to stop asking. Dan Alexteslin 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
Dan Weston wrote:
The real lesson here is that
1) no problem is "too easy" to cheat good software engineering practices. 2) no solution not accompanied by at least a QuickCheck result should be viewed with suspicion
I obviously have to go back and relearn these lessons.
Dan
Jason Dagit wrote:
On 7/19/07, Jason Dagit
wrote: On 7/19/07, Dan Weston
wrote: Oops, you got me. I didn't even look at the third line, I just took it from the previous post. My first instinct actually was to write:
allEqual x@(h:t) = and (zipWith (==) x t) I prefer,
allEqual [] = True allEqual xs = foldl1 (==) xs
But, unfortunately, it's not a one liner like yours (unless you allow allEqual [] = undefined). Oh and silly me, that only works for [Bool].
I'll hushup :)
Jason
but I don't think that zipWith is allowed in the question.
Dan
Antoine Latter wrote:
On 7/19/07, Dan Weston
wrote: > I would define: > > allEqual [] = True > allEqual [_] = True > allEqual (x1:x2:xs) = (x1 == x2) && allEqual xs What does this function do for "allEqual [1, 1, 2]" ? Antoine _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe