简体   繁体   中英

Couldn't match expected type `IO b0' with actual type `Float'

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM