finding the cause of an error (here head: empty list)

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

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
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@haskell.org http://www.haskell.org/**mailman/listinfo/beginnershttp://www.haskell.org/mailman/listinfo/beginners

On Mon, Apr 1, 2013 at 12:17 AM, David McBride
People tend to use pattern matching on lists or case statements on the list.
Yes, and activate -Wall to detect places where the pattern matching isn't covering all cases. Modulo the actual exception, head is equivalent to head (x:xs) = x Notice the absence of a "head []" definition. -- Kim-Ee

I asked more or less the same question in august 2012.
as I have gained more experience with Haskell, the problem has become less
important and I think it will be the same with you. But there is a better
solution than Maybe and others, haskell can give you a stack trace if you
compile with profiling enabled:
http://marc.info/?t=135220245600002&r=1&w=4
I now make sure to compile all my libraries with profiling enabled, just in
case I may need it...
emmanuel
On Sun, Mar 31, 2013 at 5:42 PM, Nathan Hüsken
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@haskell.org http://www.haskell.org/**mailman/listinfo/beginnershttp://www.haskell.org/mailman/listinfo/beginners
participants (4)
-
David McBride
-
Emmanuel Touzery
-
Kim-Ee Yeoh
-
Nathan Hüsken