What is the most efficient purely functional algorithm for generating all prefixes of a list?

I think you are right. There can be no sharing of the spine of the list because all the tails are different. Therefore the list of prefixes, if fully evaluated, would take the full?(n^2) space, which must take?(n^2) time to generate Note that (a lazier version of) the function you wrote is available in Data.

List as inits There is a neat optimization you can do though. This equation holds: map (foldl f z) . Inits = scanl f z And scanl runs in linear time.So if you can phrase the thing you want to do to each prefix as a left fold, then you can avoid the quadratic complexity of building the list of prefixes.

I think you are right. There can be no sharing of the spine of the list because all the tails are different. Therefore the list of prefixes, if fully evaluated, would take the full?(n^2) space, which must take?(n^2) time to generate.

Note that (a lazier version of) the function you wrote is available in Data. List as inits. There is a neat optimization you can do though.

This equation holds: map (foldl f z) . Inits = scanl f z And scanl runs in linear time. So if you can phrase the thing you want to do to each prefix as a left fold, then you can avoid the quadratic complexity of building the list of prefixes.

If you represent lists as contiguous storage plus start and end index (similar to bytestrings), you can share the storage and just need to traverse once to build the list of indices. The algorithm would not change, just the representation. For this particular use case, using snoc lists (binary lists, but nested from the end, not the start, of the list) would also allow for sharing of sublists, right?

I think it's relative clear, that his question targets an ordinary haskell list . -1 – FUZxxl Nov 27 '10 at 11:37 1 That depends on whether you are looking at the syntax of the example or at the question as stated. Given the accepted answer, the limitation to binary lists may have been intended.

But that is not a property of purely functional algorithms, not even of such algorithms in Haskell, just of this particular list representation. – claus Nov 27 '10 at 18:06 Yes, the limitation to binary lists was intended. I could have made that clearer.

– Matthew Leon Grinshpun Nov 30 '10 at 21:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions