If you're using all of racket, here's a way to do it with heavy usage of higher order functions
#lang racket (define (cumulative-sum xs) (map (curry foldl + 0) (build-list (length xs) (compose (curry take xs) add1)))) On Thu, Jan 22, 2015 at 6:34 AM, Matthias Felleisen <matth...@ccs.neu.edu> wrote: > > #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 <rus...@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 > > > ____________________ > Racket Users list: > http://lists.racket-lang.org/users >
____________________ Racket Users list: http://lists.racket-lang.org/users