简体   繁体   中英

Trace output in pure function

Is it somehow possible to print a trace log in the pure function like:

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

I know, it's not "Haskell clean" but isn't there any useful hack in GHC?

For debugging, you can use the Debug.Trace module.

import Debug.Trace

pure :: Int -> Int
pure x = trace "log" (x * x)

Note that due to laziness the output can in some cases get intermingled with other output you're generating, so this is not recommended for logging in production code, but for simple debugging tasks it's usually fine.

Of course, there's always unsafePerformIO . Not that it would be a good idea to use it here!

import System.IO.Unsafe

pure :: Int -> Int
pure x = unsafePerformIO $ do
  print x
  return $ x*x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM