我想制作一个简单的单位转换器,我写道:
value :: String -> Float
value "mg" = 0.001
value "g" = 1
value "dag" = 10
value "kg" = 1000
value "t" = 1000000
main = do
putStrLn "enter the number: "
numbr <- getLine
putStrLn "enter the unit: "
unit <- getLine
(read numbr*(value unit))
但它给了我一个错误:
jedn.hs:16:16:
Couldn't match expected type `IO b0' with actual type `Float'
In the return type of a call of `value'
In the second argument of `(*)', namely `(value unit)'
In a stmt of a 'do' block: (read numbr * (value unit))
我认为问题在于将“dag”,“kg”等值更改为实际数字,但我该怎么写呢?
我对Haskell很新,所以这段代码可能写错了。
答案 0 :(得分:3)
您只需要打印结果而不是尝试返回结果。
print (read numbr * value unit)
当你更多地研究monad时,你无法将其归还。如果要从I / O函数返回,请使用
return (read numbr * value unit)