Are there any resources on recursive programming in J?  Couldn't find much
by searching.
I would particularly like to know about scoping, and also so-called free
variables.

It seems to me that the enforced naming of variables as 'x' and 'y' might
cause problems in nested functions, necessitating awkward renaming and
copying.

I will give a little example here (my apologies to those unfamiliar with
Scheme.)
I am trying to write a routine that will return ALL the factors of a
number, not just the prime ones.
I do this by using an auxiliary routine that takes the number to factor and
a list of numbers still to combine.

;; function (unique numlist) corresponds to J's ~.
;; function (remel alist elem) corresponds to J's   [ }.~ [: >: i.
;; function (primefactors n) corresponds to J's   q:

(define (allfactors n) (af n (primefators n))

(define (af num divisors)
  (if (null? divisors) (list num)
      (let ((uniquefactors (unique divisors)))
           (flatten
           (cons num
                 (map (lambda(x) (af (/ num x) (remel divisors x)))
                      uniquefactors))))))

Now I tried to express this in J, but can't even get to first base, because
of the scoping problems I mentioned.
I realise that recursion is not the primary mode for programming J, and a
good solution may instead use something like running totals (\), but for
the time being I am stuck.
Any suggestions gratefully received.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to