我想做一个简单的单位转换器,我写道:
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 很陌生,所以这段代码可能写错了。