Re: [Haskell-cafe] working with lists of couples

2006-11-23 Thread Clare
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

Re: [Haskell-cafe] working with lists of couples

2006-11-18 Thread J. Garrett Morris
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

[Haskell-cafe] working with lists of couples

2006-11-17 Thread Clara Zamolo
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

Re: [Haskell-cafe] working with lists of couples

2006-11-17 Thread Henning Thielemann
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]

Re: [Haskell-cafe] working with lists of couples

2006-11-17 Thread Valentin Gjorgjioski
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:

Re: [Haskell-cafe] working with lists of couples

2006-11-17 Thread Valentin Gjorgjioski
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...