Haskell - Debug Printing in if block

I want to debug my program by typing something

For example,

isPos n | n<0 = False | otherwise = True 

I need something like:

 isPos n | n<0 = False AND print ("negative") | otherwise = True AND print ("positive") 

Can this be done in Haskell?

+8
debugging haskell
source share
2 answers

As the hammar said, use trace from the Debug.Trace module. The tip I found useful is to define the debug function:

 debug = flip trace 

Then you could do

 isPos n | n < 0 = False `debug` "negative" | otherwise = True `debug` "positive" 

The advantage of this is that it is easy to enable / disable debug printing during development. To remove debugging printing, just comment out the rest of the line:

 isPos n | n < 0 = False -- `debug` "negative" | otherwise = True -- `debug` "positive" 
+16
source share

Use Debug.Trace.trace .

 import Debug.Trace isPos n | n < 0 = trace "negative" False | otherwise = trace "positive" True 
+13
source share

All Articles