[Haskell-cafe] Sparse records/ADTs

David Thomas davidleothomas at gmail.com
Thu Oct 25 02:30:10 CEST 2012


You've got a bunch of great answers, if there's no rhyme or reason to
which fields are missing.

If, on the other hand, they will tend to be present or absent in
groups, you could decompose your data-structure a bit, for fast
lookups, good space efficiency, and maybe even slightly more
interesting checks from the type system.


On Wed, Oct 24, 2012 at 3:08 AM, Jon Fairbairn
<jon.fairbairn at cl.cam.ac.uk> wrote:
>
> Is there a convenient way of handling a data structure with lots
> of fields of different types that may or may not be filled in?
>
> Something equivalent to
>
> data D = D {a::Maybe A, b::Maybe B, c::Maybe C, …}
>
> but with better space efficiency and a more convenient empty
> object.
>
> An easy alternative is
>
> data E = Ea A | Eb B | Ec C | …
> type R = [E]
>
> which has a straightforward empty object, but one then must
> define
>
>    getA e = listToMaybe [a | Ea a <- e]
>
> for each field, which is tedious (and O(n)). Obviously Templates
> would help, but is there an alternative I’ve missed?
>
> --
> Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list