
Hi! While trying to implement "words", we ran into the question of how to build lists of lists. The trouble boils down to this: test = []:[] is no problem, just as Prelude> []:[]:[]:[] [[],[],[]] works fine. Now trying to put the two together _is_ a problem: testlist 1 = []:[] testlist n = []:(testlist n-1) No instance for (Num [[a]]) arising from a use of `testlist' at <interactive>:1:0-9 Possible fix: add an instance declaration for (Num [[a]]) In the expression: testlist 2 In the definition of `it': it = testlist 2 Can someone please explain, what is going on here? Regards, Torsten

Am Dienstag 08 Dezember 2009 23:34:30 schrieb Torsten Otto:
Hi!
While trying to implement "words", we ran into the question of how to build lists of lists. The trouble boils down to this:
test = []:[]
is no problem, just as
Prelude> []:[]:[]:[] [[],[],[]]
works fine. Now trying to put the two together _is_ a problem:
testlist 1 = []:[] testlist n = []:(testlist n-1)
No instance for (Num [[a]]) arising from a use of `testlist' at <interactive>:1:0-9 Possible fix: add an instance declaration for (Num [[a]]) In the expression: testlist 2 In the definition of `it': it = testlist 2
Can someone please explain, what is going on here?
Yes. Function application has higher precedence than aritthmetic operations, so the RHS of testlist n is parsed as [] : ( (testlist n) - 1) So, by the equation for testlist 1, the compiler knows that testlist :: (Num n) => n -> [[a]] In the second equation, you try to subtract 1 from a value of type [[a]], that means you need a Num instance for that type. What you want is testlist n = []:testlist (n-1) HTH, Daniel

Thank you! I'll go hide in a dark corner... Torsten Am 08.12.2009 um 23:43 schrieb Daniel Fischer:
Am Dienstag 08 Dezember 2009 23:34:30 schrieb Torsten Otto:
Hi!
While trying to implement "words", we ran into the question of how to build lists of lists. The trouble boils down to this:
test = []:[]
is no problem, just as
Prelude> []:[]:[]:[] [[],[],[]]
works fine. Now trying to put the two together _is_ a problem:
testlist 1 = []:[] testlist n = []:(testlist n-1)
No instance for (Num [[a]]) arising from a use of `testlist' at <interactive>:1:0-9 Possible fix: add an instance declaration for (Num [[a]]) In the expression: testlist 2 In the definition of `it': it = testlist 2
Can someone please explain, what is going on here?
Yes. Function application has higher precedence than aritthmetic operations, so the RHS of testlist n is parsed as
[] : ( (testlist n) - 1)
So, by the equation for testlist 1, the compiler knows that
testlist :: (Num n) => n -> [[a]]
In the second equation, you try to subtract 1 from a value of type [[a]], that means you need a Num instance for that type.
What you want is
testlist n = []:testlist (n-1)
HTH, Daniel

Excerpts from Edward Z. Yang's message of Tue Dec 08 17:45:19 -0500 2009:
Excerpts from Torsten Otto's message of Tue Dec 08 17:34:30 -0500 2009:
testlist 1 = []:[] testlist n = []:(testlist n-1)
Works for me on GHC 6.10.4. What compiler are you using?
Ah, it compiles, but refuses to run. Please ignore me. :-) Edward
participants (3)
-
Daniel Fischer
-
Edward Z. Yang
-
Torsten Otto