
Donald Bruce Stewart:
zacara:
i'd like to write a function that given a list like [1,2,3,4...] returns a list of couple where the first element is the corresponding element of the string, and the second is the sum of the previous elements. Looks like a scanl (a prefix fold) -- the "of the previous elements" is a clue that you're describing a scan:
Prelude> :t scanl scanl :: (a -> b -> a) -> a -> [b] -> [a]
Prelude> scanl (+) 0 [1..4] [0,1,3,6,10]
You should be able to work out the rest from here (or rewrite it as a fold).
I agree Don, certainly with scanl one can solve the problem elegantly. [For ClareZako: it could be something like f (x:xs) = scanl acc (x,0) xs where acc (x,s) y = (y, s+x) ] On the contrary a solution in terms of a fold is indeed more tricky. Probably ClareZako was actually looking for a solution in term of a scan instead of a fold... otherwise would be interesting to know why he's willing to solve this problem in such a twisted way... don't you agree? Cheers, -- Marco.

On 24/11/2006, at 22:27, Marco Comini wrote:
On the contrary a solution in terms of a fold is indeed more tricky. Probably ClareZako was actually looking for a solution in term of a scan instead of a fold... otherwise would be interesting to know why he's willing to solve this problem in such a twisted way... don't you agree?
It's clear to me http://www.cse.unsw.edu.au/~dons/haskell-1990-2006/msg13946.html http://www.haskell.org/haskellwiki/Homework_help I only hope this was not Marco's student. Or was it? :)
participants (2)
-
Marco Comini
-
Pepe Iborra