1
止損功能導致以下錯誤:我可以在IO monad中使用printf嗎?
Could not deduce (Text.Printf.PrintfType (m a0))
arising from the ambiguity check for `stopLoss'
from the context (Monad m,
Text.Printf.PrintfType (m b),
Text.Printf.PrintfType (m a))
bound by the inferred type for `stopLoss':
(Monad m, Text.Printf.PrintfType (m b),
Text.Printf.PrintfType (m a)) =>
Float -> Float -> Float -> m b
Possible fix:
add an instance declaration for (Text.Printf.PrintfType (m a0))
When checking that `stopLoss'
has the inferred type `forall (m :: * -> *) a b.
(Monad m, Text.Printf.PrintfType (m b),
Text.Printf.PrintfType (m a)) =>
Float -> Float -> Float -> m b'
Probable cause: the inferred type is ambiguous
功能:
stopLoss qty pb lossRate = do
let t = qty * pb * (1 + sxf)
printf "Stop Loss at: %.2f\n" ((pb - (t * lossRate)/qty) :: Float)
printf "Lost Money: %.2f\n" ((t * lossRate) :: Float)
任何建議表示讚賞!
我建議爲'stopLoss'提供一個類型簽名。 'printf'的返回類型不限於'IO()',編譯器不能推斷它。 – Artyom 2012-07-23 03:00:16
它通過添加類型簽名來解決。謝謝! – 2012-07-23 03:08:56
另外,我真的推薦'printf'上的'formatting'。類型的自由度要少得多。 http://hackage.haskell.org/package/formatting-3.1.0 – 2014-02-14 11:38:34