Haskell 还有一个类型错误

Haskell 还有一个类型错误,haskell,Haskell,正如我所理解的“x”和“y”的类型。是这样吗?它是正方形吗?需要整数。但是“x”和“y”的类型是什么?我想它们是Int。您已经声明sumOfSquare,hipotenuse,squareCheck和isItSquare在Int上运行 但是,您已经说过,calc可以使用任何类型的a,只要a是整数 像这样声明calc: Prelude> :load "some.hs" [1 of 1] Compiling Main ( some.hs, interpreted )

正如我所理解的“x”和“y”的类型。是这样吗?它是正方形吗?需要整数。但是“x”和“y”的类型是什么?我想它们是Int。

您已经声明
sumOfSquare
hipotenuse
squareCheck
isItSquare
Int
上运行

但是,您已经说过,
calc
可以使用任何类型的
a
,只要
a
整数

像这样声明
calc

Prelude> :load "some.hs"
[1 of 1] Compiling Main             ( some.hs, interpreted )

some.hs:16:74:
    Couldn't match expected type `Int' against inferred type `a'
      `a' is a rigid type variable bound by
          the type signature for `calc' at some.hs:15:18
    In the first argument of `isItSquare', namely `x'
    In the expression: (isItSquare x y)
    In a stmt of a list comprehension: (isItSquare x y)
Failed, modules loaded: none.
calc :: Int -> [(Int, Int, Int)]
…或更改所有其他功能,如下所示:

Prelude> :load "some.hs"
[1 of 1] Compiling Main             ( some.hs, interpreted )

some.hs:16:74:
    Couldn't match expected type `Int' against inferred type `a'
      `a' is a rigid type variable bound by
          the type signature for `calc' at some.hs:15:18
    In the first argument of `isItSquare', namely `x'
    In the expression: (isItSquare x y)
    In a stmt of a list comprehension: (isItSquare x y)
Failed, modules loaded: none.
calc :: Int -> [(Int, Int, Int)]

您已声明
sumOfSquare
hipotenuse
squareCheck
isItSquare
Int
上运行

但是,您已经说过,
calc
可以使用任何类型的
a
,只要
a
整数

像这样声明
calc

Prelude> :load "some.hs"
[1 of 1] Compiling Main             ( some.hs, interpreted )

some.hs:16:74:
    Couldn't match expected type `Int' against inferred type `a'
      `a' is a rigid type variable bound by
          the type signature for `calc' at some.hs:15:18
    In the first argument of `isItSquare', namely `x'
    In the expression: (isItSquare x y)
    In a stmt of a list comprehension: (isItSquare x y)
Failed, modules loaded: none.
calc :: Int -> [(Int, Int, Int)]
…或更改所有其他功能,如下所示:

Prelude> :load "some.hs"
[1 of 1] Compiling Main             ( some.hs, interpreted )

some.hs:16:74:
    Couldn't match expected type `Int' against inferred type `a'
      `a' is a rigid type variable bound by
          the type signature for `calc' at some.hs:15:18
    In the first argument of `isItSquare', namely `x'
    In the expression: (isItSquare x y)
    In a stmt of a list comprehension: (isItSquare x y)
Failed, modules loaded: none.
calc :: Int -> [(Int, Int, Int)]

你的类型太笼统了。您正在将
x
y
传递到
isItSquare
,这将增加
Int
s,但您不知道
x
y
Int
s。它们可以是,但也可以是
Integral
的任何其他实例。将签名更改为更具体的:

sumOfSquare :: (Integral a) => a -> a -> a
或者让助手函数处理更一般的类型:

calc :: Int -> [(Int, Int, Int)]

你的类型太笼统了。您正在将
x
y
传递到
isItSquare
,这将增加
Int
s,但您不知道
x
y
Int
s。它们可以是,但也可以是
Integral
的任何其他实例。将签名更改为更具体的:

sumOfSquare :: (Integral a) => a -> a -> a
或者让助手函数处理更一般的类型:

calc :: Int -> [(Int, Int, Int)]
calc::(积分a)=>a->[(a,a,a)]
计算a=[(x,y,(hipotenuse x y))|x
calc::(积分a=>a->[(a,a,a)]
计算a=[(x,y,(hipotenuse x y))|x