On Mon, Feb 11, 2013 at 8:04 AM, Alex Giannakopoulos <[email protected]> wrote: > 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.
Recursion is built into the language in various ways (and into other languages, for that matter). First off, + can be defined recursively (see also: peano numbers), and so can * (multiplication can be defined recursively based on addition) and some recursive processes are best implemented by using primitives. Furthermore, one of the big issues that recursion gets used for, in some languages, is traversal - and that is also built into the language. Next, J has ^: (induction) which is analogous to recursion and in some cases more suitable to some problems than recursion is. After that, J has $: which is suitable for classroom demonstrations of recursive concepts to relate that idea to other concepts (and can also be used in simple recursive cases). Finally, you can use explicit reference to achieve recursion. > 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. But 'x' and 'y' are local variables and cannot be used outside the explicit context where they appear. Note that you can also use explicit contexts if you want to nest $: recursion. Note also that this kind of recursive nesting can be expensive and perform slowly and in general you should try to avoid explicit recursion whenever possible. > 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. http://rosettacode.org/wiki/Factors#J That said, let's also consider your approach: > 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. One problem here is that you are expecting to be able to refer to local variables in a nested local context. Technically, that's possible in J (if the exterior context is an explicit context and the interior context is an implicit context) but implicit contexts do not give you a new named scope. The preferred approach, in J, would be to not use nested scopes. Alternatively, you can use locales instead of locals (though that has a performance penalty as well, and locales are named so they are not garbage collected - you must explicitly inform the language when you are through with them. Also, J does not favor complex anonymous blocks of code - you can do anonymous functions in a variety of ways, but it's best that they are simple (I think this is in part a reaction to people complaining that APL was hard to read - though honestly most of that criticism was from people who could not be bothered to learn the core vocabulary of the language). Anyways, if you could remind me wether / in scheme gives an integer or fractional result, and what the definition of remel is, I could give you some literal translations using various approaches. FYI, -- Raul ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
