Yes the code you are suggesting is certainly linear and it takes without
doubt time n.
But I was looking for a solution using foldl that of course takes time n.
The problem of the following solution is that it gives a reversed result,
and of course i can't just reverse the result.
couples = snd
On 11/17/06, Henning Thielemann [EMAIL PROTECTED] wrote:
On Fri, 17 Nov 2006, Clara Zamolo wrote:
buildCouples = snd . foldl op (0,[])
where
op (v,xs) x = (v+x,xs++[(x,v)])
You could make something like this that doesn't have quadratic-type
appends by accumulating functions
Hello,
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.
An example:
input: [1,2,3,4]
output: [(1,0)(2,1)(3,3)(4,6)]
The problem could
On Fri, 17 Nov 2006, Clara Zamolo wrote:
Hello,
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.
An example:
input: [1,2,3,4]
On 17.11.2006 19:29 Clara Zamolo wrote:
Hello,
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.
An example:
input: [1,2,3,4]
output:
On 17.11.2006 19:51 Valentin Gjorgjioski wrote:
So, this algorithm should work fine for you
buildCouples :: [Int]-Int-[(Int,Int)]
buildCouples (x:[]) s = [(x,s)]
buildCouples (x:xs) s = [(x,s)] ++ (buildCouples xs (x+s)).
Please ignore the . at the end, it is a typo :( Sorry again...