[racket] for/fold accum val at each step

From: J G Cho (gcho at fundingmatters.com)
Date: Sat Aug 10 18:56:27 EDT 2013

I think I understand the following example from the documentation:

(define (example)
  (for/fold ([sum 0]
             [averages null]) ;; 2 accums named sum and rev-roots are
initialized
            ([i '(1 2 3 4)]) ;; i will iterate over 1..4
            (values
             (+ sum i) ;; adding to sum
             (cons (sqrt i) averages) ;; consing to rev-roots
             )))

(example) ->
10
'(2 1.7320508075688772 1.4142135623730951 1)


But I realize I don't really understand when I try the following:

(define (example2)
  (for/fold ([sum 0]
             [averages null])
            ([i '(1 2 3 4)])
            (values
             (+ sum i) ;; adding to sum
             (cons (/ sum i) averages) ;; record running average but sum is
not what I think it should be
             )))

(example2) ->
10
'(1 1/2 1 1/2 0)

Not reducing for readability, I was hoping for
(10/4 6/3 3/2 1)

How can I tweak the code?

jGc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130810/90ef4668/attachment.html>

Posted on the users mailing list.