Access to list

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Access to list

Fernando Rodriguez-6

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



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Access to list

jerzy.karczmarczuk
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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Access to list

Bugzilla from jed@59a2.org
In reply to this post by Fernando Rodriguez-6
On 13 Jan 2008, [hidden email] 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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

attachment0 (202 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Access to list

Tom Phoenix
In reply to this post by Fernando Rodriguez-6
On Jan 13, 2008 7:55 AM, Fernando Rodriguez <[hidden email]> wrote:

> If I define the follwoing functions:
>
> car (x:_) = x
> car [] = []

What's the type signature for that function?

Cheers!

--Tom Phoenix
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Access to list

Felipe Lessa
On Jan 13, 2008 2:07 PM, Tom Phoenix <[hidden email]> wrote:
> On Jan 13, 2008 7:55 AM, Fernando Rodriguez <[hidden email]> wrote:
>
> > If I define the follwoing functions:
> >
> > car (x:_) = x
> > car [] = []
>
> What's the type signature for that function?

car :: [[a]] -> [a]

--
Felipe.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Access to list

jerzy.karczmarczuk
In reply to this post by Bugzilla from jed@59a2.org
Jed Brown writes:

> On 13 Jan 2008, [hidden email] 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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe