Having searched the forum and the wiki I still have yet to see a
proper answer to Paul Graham's Accumulator challenge. Dan Bron's
answer is a one-liner but requires a long lexicalclosure script (more
than 3 or 4 lines anyway)  which does more than the accumulator
challenge asks, also uses needs jmf and only runs under windows.
Whilst there have been various references to using locales I have seen
are no actual answers. And as a j noobie I offer one here

  foo=: 1 : 0
Xc=.'_',~'X_',>c=. cocreate ''
X__c=: x
r=. Xc,'=:',Xc,'+y'
3 : r
)
   bar2=: 2 foo
  bar2 2
4
  bar2 2
6
  bar3=: 3 foo
  bar3 2
5
  bar2 5
11

I tried to combine the last two lines of foo but I had difficulty
getting this right with  Do ".

  foo1=: 1 : 0
Xc=.'_',~'X_',>c=. cocreate ''
X__c=: x
3 : ". 'Xc,''=:'',Xc,''+y'''
)

bar2=: 2 foo1
|domain error: foo1
|       3 :".'Xc,''=:'',Xc,''+y'''

Anyway pleased with a 4 line solution since I have not, possibly
mistakenly, seen anything similar and as short (judging by the wiki
solution not using locales)

I am sure there are other ways of making it less lines, but at least
this is sort of readable to non j users, if I were add a couple of
NB.s etc.

This works for all number types as far as I can tell e.g.

 barcomplex =: 1j2 foo
  barcomplex 2j3
3j5

--
martin

"Reality is that which, when you stop believing in it, doesn't go away" PK Dick
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to