Skip to content

Instantly share code, notes, and snippets.

@laser
Created May 25, 2014 00:27
Show Gist options
  • Save laser/3a37ffbfd73ec21ba399 to your computer and use it in GitHub Desktop.
Save laser/3a37ffbfd73ec21ba399 to your computer and use it in GitHub Desktop.
debugging the div' function using guards
import Debug.Trace
div' :: Integer -> Integer -> Integer
div' x y
| trace ("x is: " ++ (show x) ++ " and y is: " ++ (show y)) False = undefined
| otherwise = x `div` y
-- guards are evaluated from top to bottom until a match is found, so we fire off
-- our call to trace (which returns False) and move on to the next guard (which is
-- our real implementation).
@mstksg
Copy link

mstksg commented Jul 16, 2014

Can't you also just do

div' x y = "blah" `trace` div x y

?

@erantapaa
Copy link

Another technique is to use a where clause to simplify creation of the message:

div' x y | trace msg False = undefined where msg = ...
div' x y = ... original definition...

To disable tracing just comment out the first line. Also have a look at Debug.NoTrace to globally disable tracing.

@laser
Copy link
Author

laser commented Aug 25, 2014

@erantapaa I really enjoy that syntax. Thanks for the tip!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment