[Haskell] modern language design, stone age tools

John Meacham john at repetae.net
Wed Jun 23 18:54:35 EDT 2004


On Wed, Jun 23, 2004 at 06:52:32PM +0100, MR K P SCHUPKE wrote:
> >So how do you debug problems like "Prelude.head: empty list"
> >in large programs?
> for precisely this reason - and the fact that I dont like bindings
> that can fail... 

Note that pattern matching rather than deconstruction functions have a
number of benefits, not just relating to error messages, consider two
functions which use the head of their argument.

f xs = ... head xs ... 
g (x:_) = ... x ...

now, g is superior to f in several ways, 

1) the error message generated if the pattern fails will have the file
name, line number and name of function

2) everything but the head of xs may be garbage collected right away
since there is no reference to the rest of the list!  This can cure some
nasty space leaks and is vital in certain cases. 

3) even the simplest compiler will realize g is stirct in its first
argument and take advantage of the numerous optimizations that entails. 


A good compiler may figure out 2 and 3 with f, but it can't always, and why
take the chance it won't? All of these benefits apply to deconstructing
more complicated types too, so using pattern matching for deconstruction
is just a good habit to get into. 

        John

-- 
John Meacham - ⑆repetae.net⑆john⑈ 


More information about the Haskell mailing list