haskell - 无法使用 -interactive-print 在 ghci 中的每行之后打印时间戳

标签 haskell ghci

我试图在 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/

相关文章:

list - Haskell:从列表中一一打印 Int

Haskell 命令行参数处理库

haskell - 从 ghci session 中的子目录导入(从 yesod 中的测试导入模块)

haskell - 在 ghci 中跟踪历史

haskell - 在 monad 变压器堆栈中使用 WeightedSample 时为 "No instance for MonadRandom"

list - 指定位置可以采用的元素的情况下,生成所有可能列表的列表的函数

haskell - GHC : partially compile Haskell code?

haskell - 为什么我不能用 Haskell 端口号中相同值的变量替换某个值?

haskell - 在 Haskell 中设置提示

haskell - 使用 Haskell 中纸牌游戏的数据模型避免不可能的状态