[Haskell-beginners] better way to create Array defined on all
daniel.is.fischer at web.de
Thu Oct 16 10:26:30 EDT 2008
Am Mittwoch, 15. Oktober 2008 23:05 schrieb Larry Evans:
> I'd like to have an Array indices values
> (see http://haskell.org/onlinereport/array.html)
> defined on [minBound::indices ... maxBound::indices].
> The attached does this; however, it also allows
> duplicate definitions (e.g. in the attached, the
> value for Term index is defined twice).
> Is there a better way to do this? Maybe a
> way that doesn't use all the contexts?
I don't know of a better way to do it, your way is fine.
What do you mean by "contexts" here? Of course, Bounded and Ix are necessary
to define array_complete. If you want to perhaps leave some array entries
undefined, there's no problem with that, as long as you don't try to access
an undefined entry later.
> Also, is there a way to do it so that the value
> associated with an index is not redefined?
From GHC's user's guide, section 126.96.36.199:
GHC's implementation of array takes the value of an array slot from the last
(index,value) pair in the list, and does no checking for duplicates. The
reason for this is efficiency, pure and simple.
If you want to throw an error for duplicate definitions, you have to scan the
eqs list manually before passing it to array.
> In effect, I'm making a map, but using an Array
> as the implementation because, I guess, it would be
> a bit faster.
More information about the Beginners