[Haskell-cafe] Re: Non Empty List?

Jason Dagit dagit at codersbase.com
Fri Jun 5 18:13:08 EDT 2009

On Fri, Jun 5, 2009 at 2:58 PM, MH <mhamro at gmail.com> wrote:

> I actually meant
> data Container a = Many a(Container a)
> but here is what I don't understand (fyi, I am a beginner) how can you
> construct  this container? I can do

I think I saw the above definition described as a coalgebra or codata:

Basically, that list has to always be infinite.

> let a = Many "somestring" - and I will get back a function but I can not do
> let a = Many 'a' "somestring"  - because the second param is not
> (Container a) type.
> let a = Many 'a' (Container ['a','a']) - doesn't work either because
> Container is a type not a constructor (right or I am missing
> something?).
> So I have two questions:
> 1. When I do
> let b = Many "somestring" , I get
> :t b
> b :: Container[Char] -> Container[Char]
> what is it and why I am allowed to pass just one parameter to Many
> (and how can I use it)?
> 2. How can you construct that container?
> data Container a = Many a(Container a)
> let a = ????

Check that link above for answers to some of your questions.

I think that basically you want a different type:
data NonEmptyList a = JustOne a | MoreThanOne a (NonEmptyList a)

This is just the normal list definition with a small change, if you think of
the normal list as being defined in this (pseudo) code:
data [a] = [] | a : [a]

I've just made it so that the "empty" list has at least one.

I hope that helps,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090605/7483a157/attachment.html

More information about the Haskell-Cafe mailing list