我试图在 ghci
中计算每一行后打印当前时间。到目前为止,这是我的代码:
module TimePrint (timePrint) where
import System.IO
import Data.Time
getTime :: IO String
getTime = do
now <- getCurrentTime
return (formatTime defaultTimeLocale "%T" now)
timePrint :: Show a => a -> IO ()
timePrint a = putStrLn $ show a ++ "\n" ++ getTime
我正在运行ghci
,如下所示:ghci -interactive-print=TimePrint.timePrint TimePrint
我收到的错误是这样的:
TimePrint.hs:12:44:
Couldn't match expected type ‘[Char]’ with actual type ‘IO String’
In the second argument of ‘(++)’, namely ‘getTime’
In the second argument of ‘(++)’, namely ‘"\n" ++ getTime’
Failed, modules loaded: none.
我认为这与 timePrint 不在 IO monad 中这一事实有关,但除此之外我没有任何线索。有任何想法吗?我希望 ghci 输出的每一行看起来像这样:
Prelude> 1+2
3
11:59:20
Prelude> 3+4
7
12:00:16
等等
最佳答案
timePrint a = do
s <- getTime
putStrLn $ show a ++ "\n" ++ s
++
想要一个字符串,而不是 IO 操作。
关于haskell - 无法使用 -interactive-print 在 ghci 中的每行之后打印时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37603307/