Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell - Debug Print in if block

I want to debug my program by printing something

For example,

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

I want something like:

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

Is it possible to do in Haskell?

like image 510
JDL Wahaha Avatar asked Sep 23 '12 06:09

JDL Wahaha


1 Answers

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

debug = flip trace

You could then do

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

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

isPos n
  | n < 0     = False -- `debug` "negative"
  | otherwise = True  -- `debug` "positive"
like image 81
beta Avatar answered Dec 11 '22 09:12

beta