[Haskell-cafe] Things to avoid (Was: Top 20 ``things'' to know in Haskell)

Henning Thielemann lemming at henning-thielemann.de
Fri Feb 11 05:14:40 EST 2005

On Fri, 11 Feb 2005, Remi Turk wrote:

> 1) It's talking about the compiler having difficulty with some
>    warnings when using guards.


>        f x | odd x  = ...
>            | even x = ...
>    GHC does complain. I would also call it Bad Code,
>    but if it's what you mean, _this_ example should be in the
>    wiki.

Yes, your example is better.

> 2) foo xs | length xs == 1 = bar (head xs)
>    As already said in "Don't ask for the length of a list, if you
>    don't need it", this usage of length is bad in itself, and
>    doesn't really help the argument against patterns IMO.

I have seen it similarly in the example I give below at that page. So I
found it worth noting that some guards can nicely be replaced by simple
patterns. More examples are welcome. May be we should replace it by

foo xs | not (null xs) = bar (head xs)


foo (x:_) = bar x

This example might be useful, too:

foo x | x == 0 = blub
        x /= 0 = bla


foo 0 = blub
foo _ = bla

> 3) the pattern guards extension.
>    I have two objections against this one. First, I don't think
>    it's a good idea to talk about a non-standard extension like
>    pattern guards in a wiki about newbie-problems.

It was given to me as a good example why Guards are invaluable:

> P.P.S. Does a piece about "Avoid explicit lambda's" stand any
>        chance of not being removed?
>        (Basically about "\x y -> x + y" vs "(+)", and "when it
>        gets more complicated it probably deserves a name.")


More information about the Haskell-Cafe mailing list