[Haskell-cafe] Access to list

Jed Brown jed at 59A2.org
Sun Jan 13 11:06:50 EST 2008


On 13 Jan 2008, frr149 at 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20080113/30d285c3/attachment.bin


More information about the Haskell-Cafe mailing list