Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trace haskell list comprehension?

Is there any easy way to trace the evaluation of a List-comprehension in Haskell? They are nicely compact, but that can also make them difficult to debug.

like image 409
guthrie Avatar asked Aug 28 '26 05:08

guthrie


2 Answers

I would use Debug.trace. Something like this:

[trace ("comprehending " ++ show x) (x + 1) | x <- [1..10]]
like image 77
Dan Avatar answered Aug 30 '26 20:08

Dan


List comprehension is rather concise, and usually easy to comprehend. If you are confused why a particular element doesn't show up in the result you should be able to test it by hand. Same thing if a element is showing up that you don't expect. I've never needed any more debugging than GHCi, but if that answer doesn't satisfy you...

List comprehension is just a short-hand for the List monad. If you expand the list comprehension into do notation and add explicit trace statements (or use the GHCi debugger`) you should quickly be able to discover what is wrong.

like image 31
Thomas M. DuBuisson Avatar answered Aug 30 '26 21:08

Thomas M. DuBuisson