Haskell未定义类型构造函数“Fractional”

时间:2014-06-04 20:21:48

标签: haskell types constructor

为什么我收到此错误?这是我的代码,简单的也是!只是试图计算一级和二级方程。我在这里做错了什么?

我是新手,我真的需要做这项工作。

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.");

1 个答案:

答案 0 :(得分:4)

Fractional不是类型的名称;它取而代之的是像Floating这样的类型类。因此,solveEcGrad1的签名需要更改为:

solveEcGrad1 :: Fractional a => a -> a -> a

同样,需要更改read f :: Fractionalread e1 :: Floating表达式,以使用实际的类型名称,而不是FractionalFloating;最简单的选择是用Double替换两个类型类名称。