2010-12-02 60 views
1
sumOfSquare :: Int -> Int -> Int 
sumOfSquare a b = a * a + b * b 

hipotenuse :: Int -> Int -> Int 
hipotenuse a b = truncate(sqrt(x)) 
      where x = fromIntegral(sumOfSquare a b) 

squareCheck :: Int -> Bool 
squareCheck n = truncate(sqrt(x)) * truncate(sqrt(x)) == n 
     where x = fromIntegral n 

isItSquare :: Int -> Int -> Bool 
isItSquare a b = squareCheck (sumOfSquare a b) 

calc :: (Integral a) => a -> [(a, a, a)] 
calc a = [(x, y, (hipotenuse x y)) | x <- [1..a], y <-[1..a], (isItSquare x y)] 

錯誤消息:而另一一種類型的錯誤

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. 

據我所知的 'x' 和 'y' 的類型。這樣對嗎?它是否正方要求Int。但是什麼是'x'和'y'?我認爲他們是詮釋。

+0

哪一行是第16行? – luqui 2010-12-02 10:46:05

回答

4

你的類型太籠統。您正在通過xyisItSquare,這是Int s,但您不知道xyInt s。他們可能是,但他們也可以是Integral的任何其他實例。無論是簽名更改爲更爲具體:

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

或你的輔助功能上更普遍的工種:

squareCheck :: (Integral a) => a -> Bool 
... 
3

您已聲明sumOfSquare,hipotenuse,squareCheckisItSquareInt上操作。

但是,您所說的calc可以使用任何類型的a,只要aIntegral

要麼宣佈calc這樣的:

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

...或者改變您的其他所有功能,像這樣:

sumOfSquare :: (Integral a) => a -> a -> a 
2
calc :: (Integral a) => a -> [(a, a, a)] 
calc a = [(x, y, (hipotenuse x y)) | x <- [1..a], y <-[1..a], (isItSquare x y)] 

a具有類型a(簽名明確地這麼說的,那是什麼「是由calc的類型簽名綁定的剛性類型變量」的意思),並且x取自列表[1..a],所以它也具有類型a(並且與相同)。

相關問題