Trace output in pure function

Is it possible to print the trace log in a pure function, for example:

pure :: Int -> Int pure x = do <trace log> return x*x 

I know this is not "Haskell clean", but isn't there a useful hack in the GHC?

+4
source share
2 answers

For debugging you can use the Debug.Trace module.

 import Debug.Trace pure :: Int -> Int pure x = trace "log" (x * x) 

Please note that due to laziness, the output may in some cases be mixed with other output that you generate, therefore this is not recommended for registration in production code, but for simple debugging tasks this is usually normal.

+13
source

Of course, always unsafePerformIO . Not that it would be nice to use it here!

 import System.IO.Unsafe pure :: Int -> Int pure x = unsafePerformIO $ do print x return $ x*x 
+1
source

Source: https://habr.com/ru/post/1444528/


All Articles