Thanks, Dan. An example showing what *doesn't* work and explaining why
not is a huge aid to understanding it.

When I fIrst saw $:  I thought I had an immediate use for it in
definitions like this:

foo=: 3 : 0
0 $: y
:
...
)

...I'm forever changing the names of verbs and forgetting their
recursive invocation. That's quite an elephant trap if you've ever
done it, particularly if the first verb's still there.

But I can't get the above to work, and I'm not altogether sure why.
I can only guess that $: is retaining the memory of whether it has
been called monadically or dyadically.



On Wed, Jan 26, 2011 at 5:30 PM, Dan Bron <[email protected]> wrote:
> Ian wrote:
>>  Hands up who understands ~help/dictionary/d212.htm
>
> $: is used for (anonymous) recursion. For example, where you might've
> written:
>
>           fact =: * fact@:<:^:(1&<)
>
>           fact 5
>        120
>
> using $:, you could've omitted the verb-naming step (which is incidental to
> its function, or at least you'd like that to be true):
>
>          ( * $:@:<:^:(1&<) ) 5
>        120
>
> but note: $: refers to the _longest_ verb that contains it.  So whereas:
>
>           factMas2  =:  2 + fact
>           factMas2 5
>        122
>
> vs:
>
>           (2 +  ( * $:@:<:^:(1&<) ))  5
>        532
>
> All of a sudden, $:'s scope has broadened to include the 2&+ (on _every_
> invocation, including the recursive ones).  Of course, you could fix this by
> manually limiting the scope of  $:
>
>           fact1 =:  * $:@:<:^:(1&<)
>           fact1Mas2 =: 2 + fact1
>
>           fact1Mas2 5
>        122
>
> ... but that kind of defeats the purpose a bit.  Also, if anyone ever
> decides to apply  f.  and it hits your  $:-verb, it'll fix you good:
>
>
>           fact1Mas2 f. 5  NB. Theoretically identical to line above
>        3
>
> There are ways around this (e.g.  2 + 3 : '( * $:@:<:^:(1&<) ) y' )  but
> none is entirely satisfying.
>
> -Dan
>
> PS:   f.  should wrap all verbs in parens before quoting them in an explicit
> context.
>
>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to