[Haskell-cafe] YAHT Question about Exercise 4.8

Gwern Branwen gwern0 at gmail.com
Sat Jul 25 03:04:17 EDT 2009

Hash: SHA512

On Sat, Jul 25, 2009 at 2:55 AM, Futari wrote:
> Hi, I was trying to use the solution given, but I don't know how to use it...
> How do I create something that is of type List?
> data List a = Nil | Cons a (List a)
> >From the previous examples, I was thinking something like Cons 'a' 'b' 'c'
> would create a list ['a','b','c'] but it gave me errors... help please,
> thanks.

Think about what Cons 'a' 'b' 'c' means. You're trying to pass Cons
three arguments - Cons x y z. But your definition of Cons says it
takes *2* arguments. Clearly there's a contradiction here.

Now, your first argument to Cons can be anything; in this case, it's
'a', which is a Char. So the second argument must be List a, which has
exactly two alternatives: either Nil, or another Cons... eventually we
must bottom out at Nil.

The simplest example would be Cons 'a' Nil, which evaluates to
 Cons 'a' Nil
 it :: List Char

But we could substitute in another Cons, for Cons 'a' (Cons 'b' ...?).
We could terminate the list here with a Nil, but we'll go one more
deep: Cons 'a' (Cons 'b' (Cons 'c' Nil)), which will again evaluate
correctly to
it :: List Char

- --
Version: GnuPG v1.4.9 (GNU/Linux)


More information about the Haskell-Cafe mailing list