kowainik / slist

♾️ Sized list
https://kowainik.github.io/projects/slist
Mozilla Public License 2.0
46 stars 6 forks source link

NonEmpty Slist #44

Open vrom911 opened 3 years ago

vrom911 commented 3 years ago

Create another data structure similar to NonEmpty implemented through the ordinary list. Something like this:

data SNonEmpty a = a :| Slist a
chshersh commented 3 years ago

@vrom911 Awesome idea! I think the Slist.NonEmpty module would be a great place for this data type 🙂

The :| constructor is taken by the NonEmpty, unfortunatately. But maybe we don't need fancy operators. I see two options: introduce something similar to Cons for ordinary lists, e.g. "non-empty" cons:

data SNonEmpty a = NCons a (Slist a)

Or maybe even use a record:

data SNonEmpty a = SNonEmpty
    { sNonEmptyHead :: a
    , sNonEmptyTail :: Slist a
    }

But I think we want to provide ergonomic pattern-matching, as probably most of the time we want to pattern-match like this:

case snelist of
    NCons x xs -> ...