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 d开发者_开发技巧ebug.
I would use Debug.trace
. Something like this:
[trace ("comprehending " ++ show x) (x + 1) | x <- [1..10]]
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.
精彩评论