如何打印IO字符串-“无法将类型'IO String'与'[Char]'匹配预期类型:字符串实际类型:IO字符串“

问题描述 投票:0回答:1

我希望此功能打印给定的IO String

day :: IO String -> IO ()
day p2 = do
  putStr "p2: "
  putStrLn p2

但是编译器说它需要[Char],但是据我所知它基本上与String相同,所以我的问题是我如何打印IO String] >>

这也是stack run输出的错误:

    • Couldn't match type ‘IO String’ with ‘[Char]’
      Expected type: String
        Actual type: IO String
    • In the first argument of ‘putStrLn’, namely ‘p2’
      In a stmt of a 'do' block: putStrLn p2
      In the expression: do putStrLn p2    • Couldn't match type ‘IO String’ with ‘[Char]’
      Expected type: String
        Actual type: IO String
    • In the first argument of ‘putStrLn’, namely ‘p2’
      In a stmt of a 'do' block: putStrLn p2
      In the expression: do putStrLn p2
   |
17 |   putStrLn p2
   |            ^^

我尝试执行putStr ("p2: " ++ p2)并使用print,但没有成功:(

我希望此函数打印给定的IO字符串日:: IO字符串-> IO()日p2 =做putStr“ p2:” putStrLn p2但是编译器说它需要[Char],但据我所知基本上...

haskell io stdout
1个回答
1
投票

编译器的错误消息实际上非常清楚。 putStrLn的参数必须是String(或[Char],这两种类型是彼此的同义词),但是您的p2不是String,而是IO String

© www.soinside.com 2019 - 2024. All rights reserved.