[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

accumulator generator

I'm trying to build a collection of a simple SICP kind
of problem expressed in a lot of different languages.
The problem is to write a function that generates 
accumulators-- to define a function that takes a number n, 
and returns a function that takes another number i and
returns n incremented by i.  (Incremented by, not plus).

Here, for reference, is the code in Scheme:

(define (foo n) 
  (lambda (i) (set! n (+ n i))))

I have canonical code for a couple languages, including 
several Lisps, Perl, and Python.  I would love to see
how it looks in the many other languages that people on
this list know.  I'm especially interested in knowing
what, if any, would be the canonical implementation in
Java and C++...

Thanks!  --pg

Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience