[英]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.