<div dir="ltr">Scratch that, naive memoization doesn't fix it. Will tinker with this more out of curiosity</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 22, 2015 at 1:23 PM, Jack Firth <span dir="ltr"><<a href="mailto:jackhfirth@gmail.com" target="_blank">jackhfirth@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Add memoization to fix that<div><br></div><div><div>(define (memo f)</div><div> (let ([cache (make-hash)])</div><div> (lambda xs</div><div> (hash-ref! cache xs (thunk (apply f xs))))))</div></div><div><br></div><div><div style="font-size:13px">(define (cumulative-sum xs)</div><div style="font-size:13px"> (map (memo (curry foldl + 0))</div><span class=""><div style="font-size:13px"> (build-list (length xs)</div><div style="font-size:13px"> (compose (curry take xs)</div><div style="font-size:13px"> add1))))</div></span></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 22, 2015 at 11:21 AM, Matthias Felleisen <span dir="ltr"><<a href="mailto:matthias@ccs.neu.edu" target="_blank">matthias@ccs.neu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Consider the performance implications.<br>
<div><div><br>
<br>
On Jan 22, 2015, at 1:59 PM, Jack Firth <<a href="mailto:jackhfirth@gmail.com" target="_blank">jackhfirth@gmail.com</a>> wrote:<br>
<br>
> If you're using all of racket, here's a way to do it with heavy usage of higher order functions<br>
><br>
> #lang racket<br>
><br>
> (define (cumulative-sum xs)<br>
> (map (curry foldl + 0)<br>
> (build-list (length xs)<br>
> (compose (curry take xs)<br>
> add1))))<br>
><br>
> On Thu, Jan 22, 2015 at 6:34 AM, Matthias Felleisen <<a href="mailto:matthias@ccs.neu.edu" target="_blank">matthias@ccs.neu.edu</a>> wrote:<br>
><br>
> #lang racket<br>
><br>
> ;; [Listof Number] -> [Listof Number]<br>
> ;; given (a b c ...) produce (a (+ a b) (+ a b c) ...)<br>
> (define (cumulative-sum l #;"final private:" [so-far 0])<br>
> (cond<br>
> [(empty? l) '()]<br>
> [else (define next (+ so-far (first l)))<br>
> (cons next (cumulative-sum (rest l) next))]))<br>
><br>
> (module+ main<br>
> (cumulative-sum '(1 2 3)))<br>
><br>
> ;; The comments are HtDP-style, but the optional entry parameter is not available in the teaching languages.<br>
><br>
><br>
> On Jan 22, 2015, at 8:53 AM, Alexandr M <<a href="mailto:rus314@gmail.com" target="_blank">rus314@gmail.com</a>> wrote:<br>
><br>
> > Hello,<br>
> ><br>
> > I am trying to replicate functionality of this code in Python:<br>
> ><br>
> > <a href="https://gist.github.com/m-blog/22b7c5d31b3839ffba50#file-cumsum-py" target="_blank">https://gist.github.com/m-blog/22b7c5d31b3839ffba50#file-cumsum-py</a><br>
> ><br>
> > in Racket without usage of any libraries.<br>
> ><br>
> > What would the most optimal / concise way to do it in Racket?<br>
> ><br>
> > Thank you.<br>
> ><br>
> > --<br>
> > Best regards,<br>
> > Alex<br>
> > ____________________<br>
> > Racket Users list:<br>
> > <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
><br>
><br>
> ____________________<br>
> Racket Users list:<br>
> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
><br>
<br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>