Q:

Haskell convert type error

I'm writing a simple program in Haskell that lets user input exponent of the power of 2
power a = 2^a
main = do
   number <- readLn
   let x = (read number :: Int)
   power x

 I figured out that I need to convert String to Int but i still get following error: No instance for (Num (IO t0)) arising from a use of 'power'  How do I make it work?

A:

It helps when you write out the types.
power :: (Num a, Integral b) => b -> a
power a = 2^a

main :: IO ()
main = do
  number <- getLine
  let x = read number :: Int
  power x

But let's look what these things really return. power looks right, but main is trying to return a Num a, not an IO (). main is not meant to return values -- you should do something with them. Maybe you want to print the result? print :: Show a => a -> IO ()
main :: IO ()
main = do
  number <- getLine
  let x = read number :: Int
  print $ power x

