(define (cumulative-sums xs)
(rest (reverse (for/fold ([sums '(0)]) ([x xs])
(cons (+ (first sums) x) sums)))))
Or
(define (cumulative-sums xs)
(define s 0)
(for/list ([x xs])
(set! s (+ s x))
s))
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
>
--
--
Jens Axel Søgaard
____________________
Racket Users list:
http://lists.racket-lang.org/users
racket@> (define (cumsum lst [so-far 0])
(match lst
['() '()]
[(cons first rest)
(cons (+ so-far first)
(cumsum rest (+ so-far first)))]))
racket@> (cumsum '(1 3 5 7))
'(1 4 9 16)
There *is* a `flvector-sums` in `math/flonum`, which operates only on
flonums. If you can believe this, the documentation for that function
even *gives example code* for computing cumulative sums!
Stupid git.
Neil ⊥