[Haskell-cafe] Re: Non Empty List?

Luke Palmer lrpalmer at gmail.com
Fri Jun 5 18:36:12 EDT 2009


On Fri, Jun 5, 2009 at 4:13 PM, Jason Dagit <dagit at codersbase.com> wrote:

>
>
> 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:
> http://blog.sigfpe.com/2007/07/data-and-codata.html
>
> 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)
>

I am still not sure why so much attention is being given to the recursive
version.  I find the most elegant and useful to be this simple one:

  data NonEmpty a = NonEmpty a [a]

With the obligatory conversions:

toList (NonEmpty x xs) = x:xs
fromList [] = Nothing
fromList (x:xs) = Just (NonEmpty x xs)

Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090605/8b363951/attachment.html


More information about the Haskell-Cafe mailing list