[Haskell-cafe] fast Array operations: foldl, drop

Henning Thielemann lemming at henning-thielemann.de
Fri Nov 30 06:24:44 EST 2007

On Fri, 30 Nov 2007, Ketil Malde wrote:

> Bryan O'Sullivan <bos at serpentine.com> writes:
> > For higher dimensions, there are enough options in terms of
> > traversal direction and what exactly e.g. a fold should fold over
> > (single elements? lower-dimensional slices?) that a sensible API
> > doesn't exactly leap out.
> How about a 'reduce' instead of 'foldl1'?  I think that if you require
> a commutative operator, the order doesn't matter (except for
> efficiency and possible rounding issues, I guess).

For what I have in mind the order of execution matters.

I also think now that slices for higher dimensional arrays are useful,
anyway. If you choose a subrange of indices in the most significant
dimension this would be possible without copying. It would be also
possible to 'reshape' (in MatLab terms) an array without copying, as long
as the number elements remain the same. So you could first transform an
array of arbitrary dimension to a two-dimensional one, say

reshape :: (j,j) -> Array i a -> Array j a

specialised to

reshape :: ((i,(j,k)), (i,(j,k))) -> Array (i,j,k) a -> Array (i,(j,k)) a

then you could slice with respect to the first (most significant)

slice :: (i,i) -> Array (i,j) a -> Array (i,j) a

{- | slice with respect to the first dimension and
     start indices of the slice with number of type 'k' -}
sliceRemap :: (i,i) -> k -> Array (i,j) a -> Array (k,j) a

More information about the Haskell-Cafe mailing list