[Haskell-cafe] How does one delare a 2D STUArray in Haskell?
noteed at gmail.com
Fri Sep 25 03:49:09 EDT 2009
2009/9/25 Casey Hawthorne <caseyh at istar.ca>:
> Well that makes sense, but for a learner, how is he/she supposed to
> know that 'i' could be '(i,i)' or for that matter a tuple of n of
> those i's?
> "STUArray s i e"
> Could you also have a tuple of states?
> Obviosly, 'e' could be a tuple, for instance (Int,Char)
Well, 'i' is just a type variable, just like 'a' and 'b' are type
variables in the function type
map :: (a -> b) -> [a] -> [b]
I guess you know that you can "map" a suitable function on a list of
pairs, right ?
Type variable mean you can use whatever type you want, including
pairs, lists, or whatever.
So you can have list of whaterver you want, and use whatever you want
as indices in arrays.... BUT!
But the whatever you want can be constrained a bit: here, the
constraint is that the type of the indices must be in Ix, this is what
the "Ix i =>" means.
For instance, we said we can put whatever you want in a list. But ask
GHCi what is the type of
inc = map (+1) :
Prelude> :t map (+1)
map (+1) :: (Num a) => [a] -> [a]
You see that you can use "inc" on list of whatever you want (the "a")
*provided* the "a" is in Num, the "Num a =>" part of the type
Now, you have to look if the type you want to use for your indices is in Ix.
Look at  and you see that
(Ix a, Ix b) => Ix ((,) a b)
is an Instance of Ix.
(The right part can be read as (a,b) instead of (,) a b).
So a pair is in Ix provided its elements are in Ix too.
More information about the Haskell-Cafe