[Haskell-cafe] Haskell Style - Pattern Matching with case vs.
divisortheory at gmail.com
Wed Mar 18 14:30:06 EDT 2009
On Wed, Mar 18, 2009 at 12:49 PM, Tom.Amundsen <tomamundsen at 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:
> foldl' _ zero  = zero
> foldl' step zero (x:xs) =
> let new = step zero x
> in new `seq` foldl' step new xs
> instead of this, which I prefer:
> foldl' f acc xs = case xs of
>  -> acc
> (x:xs) -> let new = f acc x
> in new `seq` foldl' f new xs
This is just my opinion, but pure functions are often compared to
mathematical functions because they're similar in the way that neither
depend on outside elements, only the inputs. Consider a mathematical
"piecewise function" (http://en.wikipedia.org/wiki/Piecewise_function).
These are typically defined as:
f(x) = (*definition 1*) if condition1
f(x) = (*definition 2*) if condition2
(Normally written with a large curly brace, but the idea is still the
same). To me the notation here matches up nicely with the way of separating
cases by defining them as multiple functions in Haskell because the choice
of notation itself emphasizes that the function can be split into multiple
completely separate computations depending on the format of the input.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe