Haskell 还有一个类型错误
正如我所理解的“x”和“y”的类型。是这样吗?它是正方形吗?需要整数。但是“x”和“y”的类型是什么?我想它们是Int。您已经声明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 )
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))|xcalc::(积分a=>a->[(a,a,a)]
计算a=[(x,y,(hipotenuse x y))|x