[Haskell-beginners] finding the cause of an error (here head: empty list)

David McBride toad3k at gmail.com
Sun Mar 31 19:17:11 CEST 2013

There is a safe package on hackage that has variations on head, headMay
which uses maybe, headNote which allows you to put a more descriptive error
should it fail, and headDef which allows you to specify a default, should
it fail.

But the most common way of dealing with it is not to use head except in
extremely simple cases.  You can grep some substantial codebases like yesod
and see maybe two or three actual heads in the code.  People tend to use
pattern matching on lists or case statements on the list.

On Sun, Mar 31, 2013 at 11:42 AM, Nathan Hüsken <nathan.huesken at posteo.de>wrote:

> Hey,
> I have written a program which, when executed produces:
>   Main: Prelude.head: empty list
> Now, I can go through my program and replace all "head" with
>   (\a -> trace ("<line-nr>: length=" ++ (show (length a))) a)
> Actually, I already did that, and by this I found the error.
> But I wonder if there would have been an easier way?
> Has anyone any debug advice how I could find out which call to "head"
> causes this without so much typing work?
> Thanks!
> Nathan
> ______________________________**_________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/**mailman/listinfo/beginners<http://www.haskell.org/mailman/listinfo/beginners>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130331/c0ec865f/attachment.htm>

More information about the Beginners mailing list