简体   繁体   English

无法将预期的类型“ IO b0”与实际类型“ [a0]”匹配

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

I'm new to haskell guys. 我是Haskell家伙的新手。 I'm trying to write a gcd executable file. 我正在尝试编写gcd可执行文件。

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. 我所知道的是, read帮助我将字符串转换为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. 以及IO操作putStrLn "GCD is: "String是导致-类型错误(在人们没有足够的经验之前)的原因。

From the context in that the line appears - in an IO -do-block - it must have type IO b for some b . 从上下文开始,该行出现-在IO -do-block中,它的某些b必须具有IO b类型。 But the type inferred from the application of (++) is [a] for some type a . 但是从(++)应用程序推断出的类型对于某些类型a[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 , 请注意,修复该问题之后,还需要将gcd'的结果转换为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. 与其使用putStrLn不将其在输出字符串后附加一个换行符) putStr ,不使用它。

putStr "Second Number: "
input2 <- getLine

In interactive mode (ghci), that works well. 在交互模式(ghci)中,效果很好。 stdout is not buffered there. stdout没有在那里缓冲。 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. 对于已编译的程序, stdout通常是行缓冲的,这意味着在输出换行符或缓冲区已满之前, stdout不会输出任何内容。

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). 但是至少后一种方法以前在Windows上不起作用(我不确定这是否是固定的,也不能绝对确定hFlush ing在Windows上可以工作)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将预期类型 &#39;(a0, b0, c0, Geometry -&gt; b)&#39; 与实际类型 &#39;Geometry -&gt; (Int, Int, Int, Int) 匹配 - Couldn't match expected type ‘(a0, b0, c0, Geometry -> b)’ with actual type ‘Geometry -> (Int, Int, Int, Int) 无法将预期类型“IO b0”与实际类型“Float”匹配 - Couldn't match expected type `IO b0' with actual type `Float' 无法将预期类型[a0]与实际类型IO()匹配 - couldn't match expected type [a0] with actual type IO () 无法将预期的类型“ IO()”与实际类型“ a0-&gt; m0 a0”匹配 - Couldn't match expected type `IO ()' with actual type `a0 -> m0 a0' Haskell:无法将预期类型&#39;(a,b0)&#39;与实际类型&#39;(a,b,h)&#39;相匹配 - Haskell: Couldn't match expected type ‘(a, b0)’ with actual type ‘(a, b, h)’ 无法将期望的类型&#39;([Char],b0)&#39;与实际类型&#39;[[Char]]&#39;相匹配 - Couldn't match expected type '([Char], b0)' with actual type '[[Char]]' 无法将预期的类型“ IO a0”与实际类型“ a1-&gt; IO()”匹配 - Couldn't match expected type ‘IO a0’ with actual type ‘a1 -> IO ()’ 有人可以解释此类型错误“无法将期望的类型&#39;IO()&#39;与实际类型&#39;a0-&gt; c0&#39;匹配” - could someone explain this type error “Couldn't match expected type `IO ()' with actual type `a0 -> c0' ” Haskell IO:无法将预期类型“IO a0”与实际类型匹配 - Haskell IO: Couldn't match expected type `IO a0' with actual type 无法将类型&#39;Int&#39;与&#39;(a0,b0,c0)&#39;匹配-quickBatch`的触发器有什么期望? - Couldn't match type ‘Int’ with ‘(a0, b0, c0)’ — What does the trigger of `quickBatch` expect?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM