Vocabulary/dollarco

From J Wiki
Jump to: navigation, search

>> <<   Back to: Vocabulary Thru to: Dictionary


[x] $: y Self-Reference

Rank Infinity -- operates on [x and] y as a whole -- WHY IS THIS IMPORTANT?



Lets a verb call itself, even if the verb is anonymous.

In a tacit recursive verb, $: indicates a point of recursion. Executing $: reexecutes the recursive verb.


Common Uses

1. To provide a default for the monadic valence of a verb.

   NB. create string to use for the sign of y
   NB. x is (string to use for 0);(string to use for +);(string to use for -)
   NB. result is the correct string for y
   selsign =: ({::~ *)"1 0
   ('';'<CR>';'<DR>') selsign _4
<DR>
   selsign =: (' +-'&$:) : ({::~ *)"0 1 0  NB. Provide default signs for monadic use
   selsign 4
+
   ('';'<CR>';'<DR>') selsign 4  NB. Dyadic use is unaffected
<CR>

This kind of default can also be used when the dyadic valence is defined explicitly:

   NB. Get digits of y using radix x (default 10)
   digits =: (10&$:) : (4 : 0)
x #.^:_1 y
)
   digits 64  NB. default base 10...
6 4
   4 digits 64   NB. ...or use base given as x
1 0 0 0

Here the overall verb is tacit, because it is defined by u : v. The dyadic valence is explicit, but it doesn't enter the picture until after the recursion.

2. To create a tacit recursive verb.

   NB. factorial(0 or 1)=1; otherwise factorial(n) = n * factorial(n-1)
   factorial =: 1: ` (* $:@<:) @. *
   factorial 6
720

More Information

1. When used in the definition of a tacit recursive verb, ($:) means the same as the name of the recursive verb.

   factorial =: 1: ` (* factorial@<:) @. *  NB. Identical to definition above

The advantage of using $: is that the name of the verb can change without requiring any change to the definition of the verb.

2. When used in any other context, $: refers to a portion of the sentence in which it is found: the executing verb-phrase that contains the ($:).

In the following sentences the portion referred to by $: is underlined:
   1: ` (* $:@<:) @. * 6
720
Here we copied the definition of factorial. The entire sequence 1: ` (* $:@<:) @. * is one derived verb, so $: refers to it.
   1 + 1: ` (* $:@<:) @. * 6
721
Here 1: ` (* $:@<:) @. * is still one derived verb. The leading 1 + is not part of this verb. When 1: ` (* $:@<:) @. * is executed (monadically), $: still refers only to it.

3. In no case does $: refer to a larger unit than the sentence it appears in, for example an explicit definition in which it appears.


Details

1. Because $: stands in for the name of the verb containing it, you must be wary of using u f. when u refers to a tacit recursive verb.

Since f. replaces names with their values,  $: inside replaced names will change its meaning.


CategoryVoc CategoryVocRecursion