
Hi, If I define the follwoing functions: car (x:_) = x car [] = [] cdr (_:xs) = xs cdr [] = [] and try to apply them to some list, such as car [1,2,3] I get this odd error: <interactive>:1:9: No instance for (Num [a]) arising from the literal `3' at <interactive>:1:9 Possible fix: add an instance declaration for (Num [a]) In the expression: 3 In the first argument of `car', namely `[1, 2, 3]' In the expression: car [1, 2, 3] What am I doing wrong this time? :-P Thanks

Fernando Rodriguez writes:
car (x:_) = x car [] = [] ... and try to apply them to some list, such as
car [1,2,3]
I get this odd error: No instance for (Num [a]) arising from the literal `3' ...
The error is really a bit cryptic (who cares, Nums or whatever...) but the error is here. Your 'car' picks the first element, or the empty LIST. So, the first element of the argument also must be a list, not a number, otherwise the type-checker yells. Jerzy Karczmarczuk

On 13 Jan 2008, frr149@easyjob.net wrote:
If I define the follwoing functions:
car (x:_) = x car [] = []
This won't typecheck. It helps to add a type signature car :: [a] -> a The first element of an empty list is undefined, so you can do what Prelude.head does and write: car [] = undefined
cdr (_:xs) = xs cdr [] = []
This is entirely valid, but may not be what you want. cdr [] = undefined -- like Prelude.tail Jed

Jed Brown writes:
On 13 Jan 2008, frr149@easyjob.net wrote:
If I define the follwoing functions:
car (x:_) = x car [] = []
This won't typecheck. It helps to add a type signature car :: [a] -> a
Good will, wrong diagnosis. This WILL check. car :: forall a. [[a]] -> [a] J. Karczmarczuk
participants (5)
-
Felipe Lessa
-
Fernando Rodriguez
-
Jed Brown
-
jerzy.karczmarczuk@info.unicaen.fr
-
Tom Phoenix