为什么我收到此错误?这是我的代码,简单的也是!只是试图计算一级和二级方程。我在这里做错了什么?
我是新手,我真的需要做这项工作。
solveEcGrad1 :: Fractional -> Fractional-> Fractional
solveEcGrad1 a b = - b / a
solveEcGrad2 :: Floating a => a -> a -> a -> (a, a)
solveEcGrad2 a b c = let delta = sqrt (b ^ 2 - 4 * a * c)
in
((-b + delta) / 2 * a, (-b - delta) / 2 * a)
main::IO()
main = do
putStrLn("1.Ecuatie de grad unu:");
putStrLn("2.Ecuatie de grad doi:");
opt<-getLine;
if opt=="1" then do
putStr("a, din ax+b=0 este:");
f<-getLine;
nr<-return(read f:: Fractional );
putStr("b, din ax+b=0 este:");
f1<-getLine;
nr1<-return(read f1:: Fractional );
p<-return( solveEcGrad1 nr nr1);
putStr("rezultatul ecuatiei de grad 1 este:");
putStrLn (show p);
main;
else if opt=="2" then do
putStr("a, din ax^2+bx+c=0 este:");
e<-getLine;
n<-return(read e:: Floating);
putStr("b, din ax^2+bx+c=0 este:");
e1<-getLine;
n1<-return(read e1:: Floating);
putStr("c, din ax^2+bx+c=0 este:");
e2<-getLine;
n2<-return(read e2:: Floating);
p1<-return( solveEcGrad2 n n1 n2);
putStr("rezultatul ecuatiei de grad 2 este:");
putStrLn (show p1);
main;
else
putStrLn("Terminare program.");
答案 0 :(得分:4)
Fractional
不是类型的名称;它取而代之的是像Floating
这样的类型类。因此,solveEcGrad1
的签名需要更改为:
solveEcGrad1 :: Fractional a => a -> a -> a
同样,需要更改read f :: Fractional
和read e1 :: Floating
表达式,以使用实际的类型名称,而不是Fractional
和Floating
;最简单的选择是用Double
替换两个类型类名称。