无法使用-interactive-print在ghci

时间:2016-06-02 22:43:44

标签: 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

1 个答案:

答案 0 :(得分:2)

timePrint a = do
   s <- getTime
   putStrLn $ show a ++ "\n" ++ s

++想要一个字符串,而不是IO动作。

相关问题