Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread Matthias Felleisen
he. >>> >>> (use gauche.collection) >>> >>> (define (cumulative-sums xs) >>> (map-accum (lambda (x sum) (values (+ x sum) (+ x sum))) 0 xs)) >>> >>> (cumulative-sums '(1 3 5 7)) >>> ===> >>> (1 4 9 16)

Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread Neil Toronto
On Thu, 1/22/15, Alexandr M wrote: Subject: [racket] Calculating cumulative sum of the list To: users@racket-lang.org Date: Thursday, January 22, 2015, 7:53 AM Hello, I am trying to replicate functionality of this code in Python: https://gist.github.com/m-blog/22b7c5d31b3839ff

Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread William James
cum-sums '(1 3 5 7)) '(1 4 9 16) On Thu, 1/22/15, Alexandr M wrote: Subject: [racket] Calculating cumulative sum of the list To: users@racket-lang.org Date: Thursday, January 22, 2015, 7:53 AM Hello, I am trying to replicate function

Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread Michael Wilber
0 xs)) > > (cumulative-sums '(1 3 5 7)) > ===> > (1 4 9 16) > > > ---------------- > On Thu, 1/22/15, Alexandr M wrote: > > Subject: [racket] Calculating cumulative sum of the list > To: users@racket-lang.org > Date: Thursday, January 22, 2015, 7:53 AM > > Hell

Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread Matthias Felleisen
u, 1/22/15, Alexandr M wrote: > > Subject: [racket] Calculating cumulative sum of the list > To: users@racket-lang.org > Date: Thursday, January 22, 2015, 7:53 AM > > Hello, > > I am trying to replicate functionality of this code in > Python: > > https://gist.g

Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread William James
andr M wrote: Subject: [racket] Calculating cumulative sum of the list To: users@racket-lang.org Date: Thursday, January 22, 2015, 7:53 AM Hello, I am trying to replicate functionality of this code in Python: https://gist.github.com/m-blog/22b7c5d31b3839ffba50#file-cumsum-py in Racket wit

Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread Jack Firth
Scratch that, naive memoization doesn't fix it. Will tinker with this more out of curiosity On Thu, Jan 22, 2015 at 1:23 PM, Jack Firth wrote: > Add memoization to fix that > > (define (memo f) > (let ([cache (make-hash)]) > (lambda xs > (hash-ref! cache xs (thunk (apply f xs)) >

Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread Jack Firth
Add memoization to fix that (define (memo f) (let ([cache (make-hash)]) (lambda xs (hash-ref! cache xs (thunk (apply f xs)) (define (cumulative-sum xs) (map (memo (curry foldl + 0)) (build-list (length xs) (compose (curry take xs)

Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread William James
(require srfi/1) (unfold-right null? (curry apply +) cdr (reverse '(1 3 5 7))) ===> '(1 4 9 16) On Thu, 1/22/15, Alexandr M wrote: Subject: [racket] Calculating cumulative sum of the list To: users@racket-lang.org Date: Thursda

Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread Matthias Felleisen
Consider the performance implications. On Jan 22, 2015, at 1:59 PM, Jack Firth wrote: > 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 (le

Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread Jack Firth
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

Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread Matthias Felleisen
#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))])) (

Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread Pierpaolo Bernardi
Here's a straightforward way: (define (cumulative-sum list) (let loop ([list list] [acc 0]) (if (null? list) list (let ([s (+ (first list) acc)]) (cons s (loop (rest list) s)) On Thu, Jan 22, 2015 at 2:53 PM, Alexandr M wrote: > Hello, > > I am trying to replicate

Re: [racket] Calculating cumulative sum of the list

2015-01-22 Thread Jens Axel Søgaard
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 Alexand

[racket] Calculating cumulative sum of the list

2015-01-22 Thread Alexandr M
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 __