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)
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
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
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
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
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
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))
>
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)
(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
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
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
#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))]))
(
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
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
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
__
15 matches
Mail list logo