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
betaResttomap. Map is(a -> a) -> [a] -> [a]so for[Int]list you pass it it wants anInt -> Intfunction. But partially appliedbetaRestis[Int] -> Int.As for
[0..9]its type is(Enum t, Num t) => [t]and it’s translated intoenumFromTo 0 9application. So compiler figured your error the other way around: if you define specialNumandEnuminstances 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
initsortailsfunction. Let us know what you want to achieve so we can help with solution.A minimal fix to would be to add
prevDigitsas an argument tomapand use a lambda abstraction to ignore unusedprevDigit: