【发布时间】:2016-09-13 02:23:32
【问题描述】:
我正在编写一个函数,但我找不到合适的解决方案。我该如何解决这个问题:
findLoot val [] = 0
findLoot val ((x:y:[]):xs) | val == 0 = 0.0
| val < y = ((val*x)/y)
| val > y = ((div val y)*x) + (findLoot (mod val y) xs)
错误是
interactive:33:1:
No instance for (Fractional a0) arising from a use of ‘it’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance HasResolution a => Fractional (Fixed a)
-- Defined in ‘Data.Fixed’
instance Integral a => Fractional (Ratio a)
-- Defined in ‘GHC.Real’
instance Fractional Double -- Defined in ‘GHC.Float’
...plus one other
In the first argument of ‘print’, namely ‘it’
In a stmt of an interactive GHCi command: print it
输入集是
Input 1:-
val = 50
((x:y:[]):xs) = [[120, 30], [100,50],[60, 20]]
Output 1:-
180.0000
Input 2:-
val = 10
((x:y:[]):xs) = [[500,30]]
Output 2:-
166.6667
【问题讨论】:
-
看起来像可怕的单态限制(谷歌这个词) - 它试图确定你的数字的类型,但不能决定一个。
-
@Bergi 有没有办法修改这个?我想不出合适的解决方案。
-
添加显式类型
findLoot :: Fractional a => a -> [a] -> a -
@Bergi 添加此内容后,我在加载时遇到错误无法将预期类型“a”与实际类型“[a]”匹配,“a”是受约束的刚性类型变量findLoot :: Fractional a => a -> [a] -> a 的类型签名
-
同时使用
div和/看起来不对。你的号码是什么类型的?您可能只需要/(浮点)或只需要div(整数除法)。
标签: haskell