有没有办法绕IO Double
?我正在寻找一个功能:
ownRound :: IO Double -> IO Double
使用这些单元测试:
ownRound 0.51 == 0.5
ownRound 0.49 == 0.5
ownRound 0.5 == 0.5
ownRound 0.7132 == 0.7
ownRound 0.39 == 0.4
答案 0 :(得分:3)
你要求的东西是不可能的。您编写的测试用例是针对该功能的
tensRound :: Double -> Double
但您编写的类型签名是函数
ownRound :: IO Double -> IO Double
如果您打算将测试用例编写为
ownRound (return 0.51) == return 0.5
ownRound (return 0.49) == return 0.5
等等,每个数字都包含在IO中,那么这些实现就可以了:
tensRound :: Double -> Double
tensRound d = fromInteger (round (d*10)) / 10
ownRound :: IO Double -> IO Double
ownRound = fmap tensRound