You can use loop, for example:
(defun running-sum (xs)
(loop with sum = 0
for x in xs
collect (setf sum (+ sum x))))
(running-sum '(1 2 3 4))
In principle, this is one and the same thing, but instead of a global one, a local variable is used and can be more understandable.
Alternatively, you can define a recursive function and a wrapper function:
(defun running-sum-recursive (xs)
(running-sum-recursive2 0 xs))
(defun running-sum-recursive2 (sum xs)
(if (eq xs nil)
nil
(let ((new-sum (+ sum (car xs))))
(cons new-sum (running-sum-recursive2 new-sum (cdr xs))))))
(running-sum-recursive '(1 2 3 4))
, loop .
, Haskell :
runningSum xs = scanl1 (+) xs
runningSum [1, 2, 3, 4]
scanl1 function. , - Lisp ( ), Lisp .
: , Common Lisp - scanl scanl1, :
(defun scanl (f val xs)
(loop for x in xs
collect (setf val (funcall f val x))))
(defun scanl1 (f xs)
(cons (car xs)
(scanl f (car xs) (cdr xs))))
(scanl1
: huaiyuan , .