此代碼是獲得2點之間的距離,但我有一個問題!Haskell錯誤沒有實例(浮動INT)
通過@EduardoLeon
rango2 :: Int -> [Int] -> [[Int]] -> [Int]
rango2 a b list = if (verif [(list!!a!!0),(list!!a!!1),(list!!a!!2)] (b)) then [1]
else [0]
verif :: [Int] -> [Int] -> Bool
verif a b = if ((distance a b) > a!!2) then True
else False
difference :: Num a => [a] -> [a] -> [a]
difference xs ys = zipWith (-) xs ys
dotProduct :: Num a => [a] -> [a] -> a
dotProduct xs ys = sum $ zipWith (*) xs ys
distance :: Floating a => [a] -> [a] -> a
distance xs ys = sqrt $ dotProduct zs zs
where
zs = difference xs ys
修訂版編輯:我不能改變詮釋爲浮動,因爲即時通訊做與清單業務,目前 拋出這個錯誤!
Proyecto.hs:71:18:
No instance for (Floating Int) arising from a use of `distance'
Possible fix: add an instance declaration for (Floating Int)
In the first argument of `(>)', namely `(distance a b)'
In the expression: ((distance a b) > a !! 2)
In the expression:
if ((distance a b) > a !! 2) then True else False
變化'rango2 ::的類型簽名(浮動一個,奧德一個)=>內部 - >並[a] - > [[a]] - > [a]'使其工作。在回答問題後,將問題轉換爲新的形式會混淆未來的讀者。 – Sibi
縮進在Haskell中很重要。 – pyon
你爲什麼要比較'a !! 2'的距離?在列表之外單獨存儲該值似乎是一個更好的主意。 – pyon