[Haskell-cafe] Cyclic indexing (Was: naturally, length :: a -> Int)

Henning Thielemann lemming at henning-thielemann.de
Sun Mar 14 08:23:04 UTC 2021

On Thu, 4 Mar 2021, Ben Franksen wrote:

> If we do embrace that viewpoint, then I'd say we should go all the way
> and interpret indices modulo (non-negative) structure size! This makes
> (safe) indexing total (for non-empty structures) and allows things like
> xs !! (-1) == last xs as in Perl and some other languages. Unsafe
> indexing (as in the vector package) could remain as is for performance
> critical code.

This seems to be the MatLab way of solving problems: Just don't talk about 
them anymore. :-)

However, I had the idea of adding a Cyclic shape to my array library:

In comfort-array I use distinct types for the array shape and its indices, 
where the index type is a type function of the shape type. I had the plan 
to add a Cyclic shape type in order to better support the Discrete Fourier 
Transform. A DFT actually transforms a cyclic signal to a cyclic frequency 
spectrum. This means, a Fourier transform library like fftw would still 
transform an array with indices from 0 to n-1, but you can access the 
frequency (-1) with index (-1) although it is stored at index (n-1).

More information about the Haskell-Cafe mailing list