Defining a sequence of elements depending on the previous ? - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Vincent L

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

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.