1

簡単な単位変換器を作りたかったので、次のように書きました。

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 にまったく慣れていないので、このコードはおそらく間違った方法で書かれています。

4

1 に答える 1

4

結果を返そうとするのではなく、結果を印刷するだけです。

print (read numbr * value unit)

モナドをもっと勉強すればするほど明らかになる理由で、それを返すことはできません。代わりに I/O 関数から戻りたい場合は、

return (read numbr * value unit)
于 2013-01-05T01:28:50.553 に答える