Hi,
there is a classic programming exercice in C++ : transposing a matrix inplace for matrix stored in a linear fashion.
It's trivial for square matrix, but for non square matrix, you have a non trivial permutation of elements and then it's natural to determine the cycle which makes the permutation (see https://en.wikipedia.org/wiki/In-place_matrix_transposition).

I wonder if it's possible to compute this cycle in a lazy way in a one liner in Haskell ? Basically the idea is that linear index i is moved to linear index j based on the shape of the matrix, and then jis moved to k and so on... basically the value of the next index is depending only on the previous one. If you have the function that turns an element to the next one, is there an elegant way to build a sequence out of it ? I suppose it could be done via a fold but it would be much nicer with a the same kind of trick with lazy evaluation + zipwith for factorial and fibonacci sequence.

Hi,

there is a classic programming exercice in C++ : transposing a matrix inplace for matrix stored in a linear fashion.

It's trivial for square matrix, but for non square matrix, you have a non trivial permutation of elements and then it's natural to determine the cycle which makes the permutation (see https://en.wikipedia.org/wiki/In-place_matrix_transposition).

I wonder if it's possible to compute this cycle in a lazy way in a one liner in Haskell ? Basically the idea is that linear index

`i`

is moved to linear index`j`

based on the shape of the matrix, and then`j`

is moved to`k`

and so on... basically the value of the next index is depending only on the previous one. If you have the function that turns an element to the next one, is there an elegant way to build a sequence out of it ? I suppose it could be done via a fold but it would be much nicer with a the same kind of trick with lazy evaluation + zipwith for factorial and fibonacci sequence.I don't really understand the details of the matrix stuff, but it sounds like you might be after iterate, or more generally unfold

Or just transpose :: [[a]] -> [[a]]. The shape flips when you transpose. Eg. 3x2 becomes 2x3 Matrix.