2015-04-22 89 views
0

有沒有辦法繞IO Double?我在尋找一個功能:Round IO Double到指定的位數 - Haskell

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

'fmap ::(a - > b) - >(IO a - > IO b)'是可能的。 – AJFarmar

+0

@AJFarmar,是的,我明白了。 – Denis

回答

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 
+0

@kgr,謝謝。有用! – Denis