简体   繁体   中英

Couldn't match expected type `IO b0' with actual type `[a0]'

I'm new to haskell guys. I'm trying to write a gcd executable file.

ghc --make gcd

When I compile this code I'm getting the following error.

Couldn't match expected type `IO b0' with actual type `[a0]' 
In a stmt of a 'do' block:
  putStrLn "GCD is: " ++ gcd' num1 num2 ++ "TADA...."
In the expression:
  do { putStrLn "Hello,World. This is coming from Haskell";
       putStrLn "This is the GCD";
       putStrLn "Frist Number";
       input <- getLine;
       .... }
In an equation for `main':
    main
      = do { putStrLn "Hello,World. This is coming from Haskell";
             putStrLn "This is the GCD";
             putStrLn "Frist Number";
             .... }

I don't understand where my problem is... Here is my code.

gcd' :: (Integral a) => a -> a -> a
gcd' x y = gcd' (abs x) (abs y)
      where gcd' a 0  =  a
        gcd' a b  =  gcd' b (a `rem` b)

main = do
    putStrLn "Hello,World. This is coming from Haskell"
    putStrLn "This is the GCD"
    putStrLn "Frist Number"
    input <- getLine
    let num1 = (read input)
    putStrLn "Second Number"
    input2 <- getLine
    let num2 = read input2
    putStrLn "GCD is: " ++ gcd' num1 num2 ++ "TADA...."

All I know is that read helps me convert my string into an int.

First, you need parentheses,

putStrLn ("GCD is: " ++ gcd' num1 num2 ++ "TADA....")

or infix function application ($) :

putStrLn $ "GCD is: " ++ gcd' num1 num2 ++ "TADA...."

Without that, the line is parsed as

(putStrLn "GCD is: ") ++ gcd' num1 num2 ++ "TADA...."

and the concatenation of the IO-action putStrLn "GCD is: " with a String is what causes the - somewhat cryptic, before one has enough experience - type error.

From the context in that the line appears - in an IO -do-block - it must have type IO b for some b . But the type inferred from the application of (++) is [a] for some type a . These types cannot be matched, and that's what the compiler reports.

Note that after fixing that, you also need to convert the result of gcd' to a String ,

putStrLn $ "GCD is: " ++ show (gcd' num1 num2) ++ "TADA...."

or you'll see another type error.


From the comment

To make my program look nicer. Is there a way that the input area is right next to the statement instead of a line down?

In general, yes. Instead of using putStrLn which appends a newline to the output string, use putStr which doesn't.

putStr "Second Number: "
input2 <- getLine

In interactive mode (ghci), that works well. stdout is not buffered there. For compiled programmes, stdout is usually line-buffered, that means it will not output anything until a newline shall be output or the buffer is full.

So for a compiled programme, you need to explicitly flush the output buffer,

import System.IO -- for hFlush

putStr "Second Number: "
hFlush stdout
input2 <- getLine

or turn off buffering altogether

import System.IO

main = do
    hSetBuffering stdout NoBuffering
    ...

But at least the latter method used to not work on Windows (I'm not sure whether that's fixed, nor am I absolutely sure that hFlush ing works on Windows).

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