更改内部值后,将重复执行IO操作

时间:2019-07-18 21:32:03

标签: io monads

我尝试打印一个值(可显示的Person类型),然后将返回类型从IO ()更改为IO Person

import qualified Data.Text as T

data Person = Person
  { firstName :: T.Text
  , lastName :: T.Text
  } deriving Show

writePerson :: Person -> IO Person
writePerson p = const p <$> print p

预期结果:

Person {firstName = "Maria", lastName = "do Rosario"}

实际结果:

Person {firstName = "Maria", lastName = "do Rosario"}
Person {firstName = "Maria", lastName = "do Rosario"}

1 个答案:

答案 0 :(得分:0)

您正在ghci中运行它。第一行是对print的调用的输出。第二行是解释器,它显示对writePerson的调用的返回值。它们是相同的,因为您将pconst用作参数。