Created
May 25, 2014 00:27
-
-
Save laser/3a37ffbfd73ec21ba399 to your computer and use it in GitHub Desktop.
debugging the div' function using guards
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
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.
@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
Can't you also just do
?