Currently I do the following when I want to print a variable for debugging:
{-# LANGUAGE BangPatterns #-}
import Debug.Trace (trace)
someFunc :: Int -> Int
someFunc n = result where
result = n^2
!result_ = trace ("result: " ++ show result) ()
I’m wondering if there is an easier way to achieve the same thing. What I’m looking for is a function, e.g. debug, like this:
someFunc :: Int -> Int
someFunc n = result where
result = n^2
debug "result: " result
How can I define such a function, assuming result is an instance of Show? Also, I don’t want to enable the BangPatterns extension in every module I want to debug.
EDIT: Probably it’s not possible since I need at least one assignment.
I think the easiest way to make sure the debug function is evaluated is to put it inside the definition of result, like this:
import Debug.Trace (trace)
debug :: Show a => String -> a -> a
debug s a = trace (s ++ show a) a
someFunc :: Int -> Int
someFunc n = result where
result = debug "result: " (n^2)
If you really want to keep your debug function separate from your definition of result, I can’t think of a way to improve on what you have now.