
OK, so this is a fairly basic question about list processing. Several times now, I have found myself wanting to process a list to produce a new list. However, the way later elements are processed depends on what the earlier elements are - in other words, this isn't a simple "map". What is the best way to handle this? According to the theory, anything that consumes a list and produces a value is some kind of fold. [Assuming it traverses the list in a sensible order!] So it looks like you could implement this as a fold. But should that be a LEFT-fold or a RIGHT-fold? (I always get confused between the two!) Actually, anything that takes a value and processes it repeatedly to produce a list is technically an unfold. [Again assuming a sensible processing order.] So it looks like this function could also be an UNfold. As in, you take a list as input, and keep unfolding until that list becomes empty. So it looks like this can be implemented as a fold or an unfold. But neither way looks especially easy. Both of these patterns seem to be more general than necessary; I only want to take 1 element of input and produce 1 element of output, but keeping track of a running state. I guess I could use some sort of state monad and make the whole thing monadic - but that again is overkill for what I'm trying to do. Any hints here?

andrewcoppin:
OK, so this is a fairly basic question about list processing.
Several times now, I have found myself wanting to process a list to produce a new list. However, the way later elements are processed depends on what the earlier elements are - in other words, this isn't a simple "map".
What is the best way to handle this?
According to the theory, anything that consumes a list and produces a value is some kind of fold. [Assuming it traverses the list in a sensible order!] So it looks like you could implement this as a fold. But should that be a LEFT-fold or a RIGHT-fold? (I always get confused between the two!)
Sounds like a mapAccum, a combination of map and fold, http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v%3Ama... If you gave a concrete example it would be easier to diagnose. -- Don

Why is there no mapAccumL' (strict)? Just a library deficiency that we can remedy or am I missing something? Don Stewart wrote:
andrewcoppin:
OK, so this is a fairly basic question about list processing.
Several times now, I have found myself wanting to process a list to produce a new list. However, the way later elements are processed depends on what the earlier elements are - in other words, this isn't a simple "map".
What is the best way to handle this?
According to the theory, anything that consumes a list and produces a value is some kind of fold. [Assuming it traverses the list in a sensible order!] So it looks like you could implement this as a fold. But should that be a LEFT-fold or a RIGHT-fold? (I always get confused between the two!)
Sounds like a mapAccum, a combination of map and fold,
http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v%3Ama...
If you gave a concrete example it would be easier to diagnose.
-- Don _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Wed, 11 Jun 2008, Thomas M. DuBuisson wrote:
Why is there no mapAccumL' (strict)? Just a library deficiency that we can remedy or am I missing something?
The strictness in foldl' is needed to avoid that unevaluated computations accumulate until the end of the list. In mapAccumL and scanl this danger is smaller. If you process the elements of the output list in order then the intermediate states are also computed step by step. However, if you evaluate only the last element of the output list you have the same problem like with foldl.

Am Mittwoch, 11. Juni 2008 20:17 schrieb Andrew Coppin:
So it looks like this can be implemented as a fold or an unfold. But neither way looks especially easy. Both of these patterns seem to be more general than necessary; I only want to take 1 element of input and produce 1 element of output, but keeping track of a running state.
scanl might be a sufficiently lightweight way to do that (or it might not, depending on what you actually want to do).

Hey Andrew, On 11 jun 2008, at 20:17, Andrew Coppin wrote:
According to the theory, anything that consumes a list and produces a value is some kind of fold. [Assuming it traverses the list in a sensible order!] So it looks like you could implement this as a fold. But should that be a LEFT-fold or a RIGHT-fold? (I always get confused between the two!) Check out http://foldl.com/ and http://foldr.com ;)
-chris
participants (6)
-
Andrew Coppin
-
Chris Eidhof
-
Daniel Fischer
-
Don Stewart
-
Henning Thielemann
-
Thomas M. DuBuisson