簡単な単位変換器を作りたかったので、次のように書きました。
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 にまったく慣れていないので、このコードはおそらく間違った方法で書かれています。