Reputation:
I wanted to make a simple unit converter, I wrote:
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))
but it's giving me an error:
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))
I believe that the problem is with changing values like "dag", "kg" to actual numbers, but how should I write it right?
I'm quite new to Haskell, so this code is probably written the wrong way.
Upvotes: 1
Views: 1789
Reputation: 60503
You just need to print the result rather than trying to return it.
print (read numbr * value unit)
You can't return it for reasons that will become clearer as you study monads more. If you want to return from an I/O function instead, use
return (read numbr * value unit)
Upvotes: 4