# Accumulator Generator in Joy

## Christopher Diggins — 2006-10-25 18:14:29

    I found an interesting post at

    http://www.paulgraham.com/accgen.html

    about accumulator generatos in different languages.

    In Joy, if I am not mistaken, the solution is:

    DEFINE ag == [+] cons;

    Am I correct?

## Magnus Jonsson — 2006-10-25 18:47:07

    Hi Chris,

    Sorry, you're not correct. Your code corresponds to the lisp code:

    (defun foo (n) (lambda (i) (+ n i)))

    (setf f (foo 10))

    (funcall f 2) -> 12

    (funcall f 0) -> 10

    (funcall f 10) -> 20

    whereas what Paul asks for is:

    (defun foo (n) (lambda (i) (incf n i)))

    (setf f (foo 10))

    (funcall f 2) -> 12

    (funcall f 0) -> 12

    (funcall f 10) -> 22

    But since Joy is purely functional that can't be done in Joy.

    Best,

    Magnus

     On Wed, 25 Oct 2006, Christopher Diggins wrote:

    > I found an interesting post at

    > http://www.paulgraham.com/accgen.html

    > about accumulator generatos in different languages.

    >

    > In Joy, if I am not mistaken, the solution is:

    >

    > DEFINE ag == [+] cons;

    >

    > Am I correct?

    >

## Christopher Diggins — 2006-10-25 19:09:42

    Thanks Magnus!

     On 10/25/06, Magnus Jonsson <magnus@...> wrote:

    >

    >   Hi Chris,

    >

    > Sorry, you're not correct. Your code corresponds to the lisp code:

    >

    > (defun foo (n) (lambda (i) (+ n i)))

    >

    > (setf f (foo 10))

    > (funcall f 2) -> 12

    > (funcall f 0) -> 10

    > (funcall f 10) -> 20

    >

    > whereas what Paul asks for is:

    >

    > (defun foo (n) (lambda (i) (incf n i)))

    > (setf f (foo 10))

    > (funcall f 2) -> 12

    > (funcall f 0) -> 12

    > (funcall f 10) -> 22

    >

    > But since Joy is purely functional that can't be done in Joy.

    >

    > Best,

    > Magnus

    >

    > On Wed, 25 Oct 2006, Christopher Diggins wrote:

    >

    > > I found an interesting post at

    > > http://www.paulgraham.com/accgen.html

    > > about accumulator generatos in different languages.

    > >

    > > In Joy, if I am not mistaken, the solution is:

    > >

    > > DEFINE ag == [+] cons;

    > >

    > > Am I correct?

    > >

    >

    >

    >

    [Non-text portions of this message have been removed]

## John Cowan — 2006-10-25 19:25:15

    Christopher Diggins scripsit:

     > I found an interesting post at

    > http://www.paulgraham.com/accgen.html

    > about accumulator generatos in different languages.

    >

    > In Joy, if I am not mistaken, the solution is:

    >

    > DEFINE ag == [+] cons;

     It's impossible to design inherently stateful procedures like this in Joy,

    short of writing the current value of n to a file and then reading it back

    on every invocation.

    --

    Said Agatha Christie / To E. Philips Oppenheim  John Cowan

    "Who is this Hemingway? / Who is this Proust?   cowan@...

    Who is this Vladimir / Whatchamacallum,         http://www.ccil.org/~cowan

    This neopostrealist / Rabble?" she groused.

    	--George Starbuck, Pith and Vinegar

## sa@dfa.com — 2006-10-25 19:59:53

    not so fast:

    http://tech.groups.yahoo.com/group/concatenative/msearch?query=accgen&charset=utf-8

    concatenative@yahoogroups.com wrote on 10/25/2006 03:25:15 PM:

     > Christopher Diggins scripsit:

    > > I found an interesting post at

    > > http://www.paulgraham.com/accgen.html

    > > about accumulator generatos in different languages.

    > >

    > > In Joy, if I am not mistaken, the solution is:

    > >

    > > DEFINE ag == [+] cons;

    >

    > It's impossible to design inherently stateful procedures like this in

     Joy,

     > short of writing the current value of n to a file and then reading it

     back

     > on every invocation.

    >

    > --

    > Said Agatha Christie / To E. Philips Oppenheim  John Cowan

    > "Who is this Hemingway? / Who is this Proust?   cowan@...

    > Who is this Vladimir / Whatchamacallum,

     http://www.ccil.org/~cowan

     > This neopostrealist / Rabble?" she groused.

    >    --George Starbuck, Pith and Vinegar

    >

    >

    >

    > Yahoo! Groups Links

    >

    >

    >

    >