< Back to IRCAM Forum

Accumulation in lisp

Hello,

I’m trying create a lisp loop on a lists with 3 numbers. For each list I want:
((second - first)*third)+previous-result

An example list and a correct result would be:
((0 24.250002 1.0) (24.250002 48.500004 0.9992113) (48.500004 72.75001 0.9984226) (72.75001 97.00001 0.99763394) (97.00001 121.25001 0.99684525) (121.25001 145.50002 0.9960565)) —>
(24.250002 48.48088 72.692635 96.885254 121.05875 145.21312)

I already managed to do it with a patch (accum, collect and a lambda patch), now I’m trying to find the lisp version.
From the two attempts below, “new-window-frame-wrong” is wrong: it is adding all results, not just previous result.
The function “new-window-frame-dx” simply uses dx->x to add up values after the loop, and the result is correct.
But it would be good to understand the mistake in the first function.

Best
Ruben

(defun new-window-frame-wrong (lists)
(let*(
(accum 0)
(window-values (loop for l in lists
sum accum into l-sum
collect (setf accum (om+ (om* (om- (second l)(first l) ) (third l)) l-sum) )
))
)
window-values
)
)

(defun new-window-frame-dx (lists)
(let*(
(window-values (loop for l in lists
collect (om* (om- (second l)(first l) ) (third l))
))
)
(cdr(dx->x 0 window-values ))
)
)

Hi Ruben.

In your code:
(setf accum (om+ (om* (om- (second l)(first l) ) (third l)) l-sum) )
you’re storing the value in 'accum.

So just replace 'l-sum with 'accum in that same statement to access the value from the previous iteration:

(defun new-window-frame (lists)  
  (let ((accum 0))  
    (loop for l in lists  
       collect (setf accum (om+ (om* (om- (second l) (first l)) (third l)) accum)))))