[Haskell-cafe] Re: Non-Overlapping Patterns

Achim Schneider barsoap at web.de
Mon May 5 11:42:36 EDT 2008


PR Stanley <prstanley at ntlworld.com> wrote:

> Hi
> isZero :: Int -> Bool
> isZero 0 = True
> isZero n | n /= 0 = False
> 
> The order in which the above equations appear makes no difference to 
> the application of isZero. Does the Haskell interpreter rewrite 
> patterns into one single definition using some sort of switch or if 
> construct? Why does an equation without a guard have to be placed 
> after the more specific cases?To put it another way, why doesn't the 
> interpreter identify the more specific cases and put them before the 
> general ones.
> Cheers
> Paul
>
For completeness' sake:

isZero 0 = True
isZero _ = False

works, and, contrary to your example, requires an order to
have a well defined meaning.

It's equivalent[1] to

isZero n = case n of 
	0 -> True
	_ -> False

, and yours to

isZero n = case n of
	0 -> True
	n -> if n /= 0
		then False
		else undefined

I'll leave the last question unanswered, just try to write such a beast.


PS: I prefer

isZero = (==) 0


[1] which doesn't mean that one is reduced to the other. It just means
they're semantically identical.

-- 
(c) this sig last receiving data processing entity. Inspect headers for
past copyright information. All rights reserved. Unauthorised copying,
hiring, renting, public performance and/or broadcasting of this
signature prohibited. 



More information about the Haskell-Cafe mailing list