[racket] Calculating cumulative sum of the list
#lang racket
;; [Listof Number] -> [Listof Number]
;; given (a b c ...) produce (a (+ a b) (+ a b c) ...)
(define (cumulative-sum l #;"final private:" [so-far 0])
(cond
[(empty? l) '()]
[else (define next (+ so-far (first l)))
(cons next (cumulative-sum (rest l) next))]))
(module+ main
(cumulative-sum '(1 2 3)))
;; The comments are HtDP-style, but the optional entry parameter is not available in the teaching languages.
On Jan 22, 2015, at 8:53 AM, Alexandr M <rus314 at gmail.com> wrote:
> 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