[Haskell-cafe] Re: Non Empty List?

Ketil Malde ketil at malde.org
Fri Jun 5 05:56:02 EDT 2009


GüŸnther Schmidt <gue.schmidt at web.de> writes:

> I need a data structure as in my example without the [] being possible
> to be empty.

Well, a list can by definition be empty, so this is clearly
impossible.  The best you can do is to hide the constructors and have
"smart" constructor functions that guarantee not to construct your
data structure with an empty list component.

I'm puzzled why you need to use a list here though.

> The idea behind this is that an "a" can "pocket" siblings, but only
> one level deep and that an "a's" list of "pocketed/swallowed" siblings
> must not be empty, because otherwise it would automatically be an
> "Single a".

>>     data Container a = Container a [a]

If you cannot use this directly, then perhaps you can use it as a
replacement for normal lists that cannot be non-empty?

  chead (Container x _) = x
  ctail (Container _ []) = error "Can't take tail of singleton list"
  ctail (Container _ (x:xs) = x:xs

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants


More information about the Haskell-Cafe mailing list