
Hello! The function trace is supposed to write debug messages to console. However, when I trying to run the following program import Debug.Trace main = do putStrLn "xxx" return (trace "yyy" ()) putStrLn "zzz" only xxx and zzz is displayed. yyy is missing. Why trace is not working? PS. GHC-6.4.2, Debian Linux on x86. With best regards, Alexander.

Hi, Either one of these will work: main = do putStrLn "xxx" x <- return (trace "yyy" ()) x `seq` putStrLn "zzz" main = do putStrLn "xxx" trace "yyy" (return ()) putStrLn "zzz" This works fine, the problem is that trace is defined to output the first parameter before returning the second. In the case of return () the () is never evaluated, hence the item is never generated, and the trace is not fired. Adding a seq to the result gets the trace going. This just shows how fragile trace is! However, if you use it in a real situation, rather than just a play one like this, it will probably work for you. Thanks Neil

Alexander Vodomerov
main = do putStrLn "xxx" return (trace "yyy" ()) putStrLn "zzz"
only xxx and zzz is displayed. yyy is missing.
This is because you never demanded the value of (trace "yyy" ()), so it was never computed. The joys of laziness! To force its evaluation try main = do putStrLn "xxx" () <- return (trace "yyy" ()) putStrLn "zzz" Regards, Malcolm
participants (3)
-
Alexander Vodomerov
-
Malcolm Wallace
-
Neil Mitchell