Haskell错误:“没有实例for(Enum [Int]) [英] Haskell Error: "No instance for (Enum [Int])
问题描述
我有以下代码:
betaRest :: Int - [Int] - > Int
betaRest n prevDigits | n == 0 =(length prevDigits)
|否则=(sum(mapRest(n-1))[0..9]))
betaFirst :: Int-> Int
betaFirst n | n == 0 = 0
|否则=(betaRest(n-1)[1..9])
错误,我不知道为什么。
1)没有从算术序列'0 .. 9'
2)没有从文字'0'产生的(Num [Int])实例
Haskell认为用..运算符做的事情是枚举吗?但是为什么下面4行的行没有错误(用[1..9])呢?
编辑:我想让代码这样做(程序上):
int betaRest ,int [] prevDigits){
if(n == 0)return prevDigits.length;
else {
sum = 0;
foreach prevDigit in prevDigits {
sum + = betaRest(n-1,[0..9]);
}
return sum;
}
}
int betaFirst(int n){
if(n == 0)return 0;
else return betaRest(n - 1,[1..9]);
}
因此,betaFirst(1)== 9和betaFirst(2)= = 90.是的,有人可能想建议一个公式来生成这个,但我要添加一个过滤器[0..9],从而减少范围。
<
betaRest
到映射
。地图是(a - > a) - > [a] - > [a] 所以对于 [Int]
列表你传递它它想要一个 Int - Int
函数。但部分应用 betaRest
是 [Int] - > [0..9]
其类型为<$ c $ c>(Enum t,Num t)=> [t]
,并将其转换为 enumFromTo 0 9
应用程序。所以编译器以另一种方式计算你的错误:如果你为列表定义了特殊的 Num
和 Enum
c $ c> [0..9]
成为int列表的列表,那么你的应用程序将是有意义的。
您要使用 inits
或 tails
函数。让我们知道你想要达到什么,以便我们可以帮助解决方案。
最小的修复将添加 prevDigits
作为 map
的参数,并使用lambda抽象来忽略未使用的 prevDigit
:
|否则= sum(map(\prevDigit - > betaRest(n-1)[0..9])prevDigits)
I have the following code:
betaRest :: Int -> [Int] -> Int
betaRest n prevDigits | n == 0 = (length prevDigits)
| otherwise = (sum (map (betaRest (n - 1)) [0..9]))
betaFirst :: Int -> Int
betaFirst n | n == 0 = 0
| otherwise = (betaRest (n - 1) [1..9])
It gives me the following errors, and I don't know why.
1) No instance for (Enum [Int]) arising from the arithmetic sequence '0 .. 9'
2) No instance for (Num [Int]) arising from the literal '0'
Does Haskell think that things made with the ".." operator are enumerations? But why isn't there an error for the line that's 4 lines below (with "[1..9]") it then?
Edit: What I want the code to do is like this (procedurally):
int betaRest(int n, int[] prevDigits) {
if (n == 0) return prevDigits.length;
else {
sum = 0;
foreach prevDigit in prevDigits {
sum += betaRest(n - 1, [0..9]);
}
return sum;
}
}
int betaFirst(int n) {
if (n == 0) return 0;
else return betaRest(n - 1, [1..9]);
}
Thus, betaFirst(1) == 9, and betaFirst(2) == 90. Yes, somebody may want to suggest a formula for generating this, but I'm going to add a filter of some sort to [0..9], thus reducing the range.
解决方案 You pass betaRest
to map
. Map is (a -> a) -> [a] -> [a]
so for [Int]
list you pass it it wants an Int -> Int
function. But partially applied betaRest
is [Int] -> Int
.
As for [0..9]
its type is (Enum t, Num t) => [t]
and it's translated into enumFromTo 0 9
application. So compiler figured your error the other way around: if you define special Num
and Enum
instances for lists, so [0..9]
becomes a list of lists of int, then your application will make sense.
But I think you want to use inits
or tails
function. Let us know what you want to achieve so we can help with solution.
A minimal fix to would be to add prevDigits
as an argument to map
and use a lambda abstraction to ignore unused prevDigit
:
| otherwise = sum (map (\prevDigit -> betaRest (n - 1) [0..9]) prevDigits)
这篇关于Haskell错误:“没有实例for(Enum [Int])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!