On Wed, Mar 18, 2009 at 11:49 AM, Tom.Amundsen <tomamundsen@gmail.com> wrote:

Hi All,

I am new to Haskell. I just started reading "Real World Haskell" a few days
ago, so I apologize for being such a noob.

But, I am curious why I see a lot of code where people do pattern matching
via multiple function declarations instead of using the case ... of ...
construct? For example:

[code]
foldl' _    zero []     = zero
foldl' step zero (x:xs) =
     let new = step zero x
     in  new `seq` foldl' step new xs
[/code]

Well, in this particular case, note that you have two equations written.  These equations are true of foldl', and in fact foldl' is the least defined function satisfying these equations (see http://en.wikibooks.org/wiki/Haskell/Denotational_semantics for the underlying theory of "definedness").  It a very pretty idea.

However, this happy view of equations breaks down once you start using the "fall through" semantics, as in:

foo (x:y:xs) = x + y
foo xs = 0

In which the second equation does not always hold (foo [1,2,3] = 0 is false).  Because of this, I am beginning to prefer not writing functions using "fall through", although occasionally it is too damned convenient to pass up.

Luke