[racket] Calculating cumulative sum of the list

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Thu Jan 22 09:11:32 EST 2015

Something like this perhaps:

(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))



2015-01-22 14:53 GMT+01:00 Alexandr M <rus314 at gmail.com>:
> Hello,
>
> I am trying to replicate functionality of this code in Python:
>
> https://gist.github.com/m-blog/22b7c5d31b3839ffba50#file-cumsum-py
>
> in Racket without usage of any libraries.
>
> What would the most optimal / concise way to do it in Racket?
>
> Thank you.
>
> --
> Best regards,
> Alex
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>



-- 
--
Jens Axel Søgaard


Posted on the users mailing list.