For computational purposes, I favor primitive recursion
(http://en.wikipedia.org/wiki/Primitive_recursive_arithmetic) over
elementary recursion
(http://en.wikipedia.org/wiki/Elementary_recursive_arithmetic).
Still, I thought I'd post an explicit implementation of Recursion. I
understand that being explicit bothers some people, but I like its
simplicity. Simplicity is a virtue?
Recursion=:1 :0
'`if else test next'=. m
[: (if else^:(-.@test))/f. (, next@{:)^:(test@{:)^:_ f.
)
For fully general array support you'd want boxing and unboxing each
step of the way. So think of this as an optimized implementation?
Thanks,
--
Raul
On Sun, Feb 23, 2014 at 8:37 PM, Jose Mario Quintana
<[email protected]> wrote:
> Tacit recursion without $:
>
> Is there ever a case for tacit recursion without the use of the
> self-reference verb ($:)?
>
> One reason is if one wants to define and fix a tacit verb in terms of one
> or more tacit recursive verbs. The problem is that the official public
> version of J lacks the means to provide a direct scope for the recursion
> phrase containing the self-references. It is possible, but inefficient, to
> circumvent this problem by employing recursion without $:. This was the
> motivation for developing the recursion scope adverb (103!.0) extension
> [0,1]. Thus, this is no longer an issue when using a patched version of
> J.
>
> What about when writing mutually recursive anonymous verbs?
>
> One can even use $: to accomplish this using the recursion scope (103!.0).
> How? See, for example, hint/solution (a). A reasonably interesting test
> case is the implementation of the female and male sequences [7]. (See [1]
> to find where to get a suitable Windows J DDL version with recursion
> scope.)
>
> What about when writing recursive tacit adverbs since $: is only meant to
> self-refer verbs?
>
> The agenda form (@. N) where N is boxed is a fairly general, but often a
> cumbersome and only occasionally a convenient, scheme to write tacit
> adverbs. However, the (@. N) form is not necessary. An alternative way is
> via the adverb (Adv) in [2] that adverbializes a verb; see also the adverb
> (av) in [3] (or a similar adverb, for example, the permissive adverb (adv)
> defined in [1]). The generic defining sentence form is ((`'') (workhorse
> f.Adv) (`:6)) where workhorse is a tacit verb that takes the adverb's
> gerund as an argument and constructs the atomic representation of the
> desired word to be produced by the adverb (and any valid computable atomic
> representation can be produced, in principle, by a workhorse verb since
> tacit verbal programming is (Turing) complete). The burden of working with
> atomic representations can be mitigated by using heterodox wicked verbs
> that can take and produce any type of word (see, [1] and references there
> in) and the form is in this case simplified to (darkhorse f. Adv). There
> is one issue though, the agenda form (@. N) is used in the definition of
> the adverb (Adv)! However, the adverb (Adv) can be rewritten without using
> the agenda form. How? See, hint/solution (b).
>
> Thus, if a recursive method is well-advised, it could always be implemented
> in the definition of the workhorse, or the darkhorse, via $:. Therefore,
> there is never a case for recursion without the use of $: with just one
> type of exception (as far as I know). How come? The exception occurs when
> it is required because of syntactic reasons! What? Consider the following:
>
> One can write tacitly an adverb (Recursion) that reproduces the behavior of
> (`:4) described in [4]. (How? See hint/solution (c).) So that,
>
> fact=. *`1:`*`<: Recursion NB. Factorial
>
> fact 5
> 120
>
> Fib=. >&1`(i.@>:)`(] , +/@(_2&{.)@])`<: Recursion NB. Fibonacci
>
> Fib 7
> 0 1 1 2 3 5 8 13
>
> One can even produce an adverb (Recur) that that takes the arguments
> (Proposition, Basis, Combine, and Reduce) as a strand; this is a form
> pioneered by Dan (see, for example, [5,6]),
>
> fact=. * 1: * <: Recur NB. Factorial
>
> fact 5
> 120
>
> Fib=. (>&1) (i.@>:) (] , +/@(_2&{.)@]) <: Recur NB. Fibonacci
>
> Fib 7
> 0 1 1 2 3 5 8 13
>
> Furthermore, one can even write a fixed adverb (sna) that takes as a strand
> (of course!) the atomic representation of any adverb, which takes a gerund
> (in the form of an argument list), and (sna) also takes the tally (#) of
> that gerund and produces a corresponding (fixed) adverb, which takes the
> arguments as a strand. How? The subject of this message implies the
> answer: recursively (without $:)! For more details see the hint/solution
> (d). For instance, the adverb (Recur) was defined as,
>
> ar=. 5!:1@<
>
> Recur=. (ar'Recursion') 4 sna NB. Recur is fixed automatically
>
> (Incidentally, although Recursion and Recur can produce a wide variety of
> recursive verbs, more complex recursive verbs, such as the recursive
> definition of the Ackermann function, do not seem to fit the (Proposition,
> Basis, Combine, and Reduce) mold. Am I mistaken?)
>
> Is there any benefit for defining adverbs in strand form (apart from
> allowing one to write neat expressions)? I think so: these expressions
> are, or should be, right-associative and provide flexible means to refer to
> the argument of a derived adverb, the argument of the derived adverb of a
> derived adverb, and so on; thus, one can easily bond adverb arguments. For
> example, often the Reduce recursion part is decrement (<:), so one can
> easily write an adverb (rd) that would accommodate those cases,
>
> rd=. <: Recur NB. rd is fixed automatically
>
> fact=. * 1: * rd
>
> fact 5
> 120
>
> Fib=. (>&1) (i.@>:) (] , +/@(_2&{.)@]) rd
>
> Fib 7
> 0 1 1 2 3 5 8 13
>
> Sometimes the order of the arguments of the source adverb might not be
> convenient for bonding. For the typical adverbs taking a gerunds, it is
> quite easy to derive a target adverb with an arbitrary permutation of the
> arguments: just adverbialize a permutation verb first; for example, ((P&{
> Adv) Recursion) where P is an arbitrary permutation. It is not as easy to
> permute the arguments of adverbs taking strands. Yet, one can write a
> generic adverb Permute to derive a target adverb, which takes a list of
> arguments as a gerund, from a permutation verb and the (atomic
> representation of the) source adverb, which takes its arguments as a
> strand. The derived verb can in turn be easily converted to take its
> arguments as a strand. For example,
>
> recur=. (3 2 1 0&{) (ar'Recur') Permute 4 sna
>
> <: * 1: * recur 5
> 120
> <: (] , +/@(_2&{.)@]) (i.@>:) (>&1) recur 7
> 0 1 1 2 3 5 8 13
>
> recur=. |. (ar'Recur') Permute 4 sna
>
> <: * 1: * recur 5
> 120
> <: (] , +/@(_2&{.)@]) (i.@>:) (>&1) recur 7
> 0 1 1 2 3 5 8 13
>
> * 1: * <: Recur 5
> 120
> * 1: * <: |.(ar'recur') Permute 4 sna 5
> 120
>
> (>&1) (i.@>:) (] , +/@(_2&{.)@]) <: Recur 7
> 0 1 1 2 3 5 8 13
> (>&1) (i.@>:) (] , +/@(_2&{.)@]) <: |.(ar'recur') Permute 4 sna 7
> 0 1 1 2 3 5 8 13
>
> How can one write Permute? See hint/solution (e).
>
> [0] J functional programming extensions
> http://www.jsoftware.com/pipermail/programming/2013-March/031835.html
> [1] J Functional Programming Extensions
> http://journalofj.com/index.php/vol-2-no-2-october-2013
> [2] Recursion
>
> http://www.jsoftware.com/pipermail/programming/2013-November/034177.html
> [3] Tacit adverbial programming patterns
>
> http://www.jsoftware.com/pipermail/programming/2013-February/031673.html
> [4] Gerunds and Representations
> http://www.snakeisland.com/gerunds.htm
> [5] How to make this conjunction tacit
> http://www.jsoftware.com/pipermail/programming/2013-October/033639.html
> [6] How to make this conjunction tacit
> http://www.jsoftware.com/pipermail/programming/2013-October/033654.html
> [7] Hofstadter's female and male sequences
>
> http://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Female_and_Male_sequences
>
> The hints/solutions follow after the countdown:
>
> ,. @: |. @: i. 43
> 42
> 41
> 40
> 39
> 38
> 37
> 36
> 35
> 34
> 33
> 32
> 31
> 30
> 29
> 28
> 27
> 26
> 25
> 24
> 23
> 22
> 21
> 20
> 19
> 18
> 17
> 16
> 15
> 14
> 13
> 12
> 11
> 10
> 9
> 8
> 7
> 6
> 5
> 4
> 3
> 2
> 1
> 0
>
> NB. The following were referred as hints/solutions. They are solutions
> written using a
> NB. permissive extended version of J described in [1]; but, there are
> also hints because the
> NB. ideas could be applied to write solutions using an official version
> of J. Verbalized
> NB. primitive adverbs and conjunctions can be produced using an
> alternative definition for
> NB. Cloak (]:): Cloak=.
> (<(<,'4'),<(<(<,'4'),<;:'0:`'),<(<,'4'),<;:',^:') (0:`)(,^:). In
> NB. addition, tacit adverbs can replace derived tacit conjunctions which
> are not possible to
> NB. produce using the official version. Orthodox tacit solutions are,
> in principle, also
> NB. possible (with the virtual exception of (a)), but probably hard to
> write. As its name
> NB. implies, the orthodox solution to question (b) does not require any
> extensions.
> NB.
> NB. Moreover, for the law abiding explicit writers (if there are any
> still reading this far)
> NB. I do not see any reason why anonymous solutions could not be written
> explicitly; then
> NB. again, I do not write explicitly, what do I know?
>
> NB. The hints/solutions are presented slightly out of order ((a), (b),
> (d), (c) and (d)).
>
> NB. General preliminary pro-words...
>
> (ar=. 5!:1@<) (st=.7!:2@:] ; 6!:2) NB. Verbs
> 5!:1@< (7!:2@:] ; 6!:2)
> (o=. @:) NB. Conjunction
> @:
> (x=. o[) (y=. o]) (g=. "_1) (e=. &.>) (c=. "_) (f=. &{::) NB. Adverbs
> (((((o[)(o]))("_1))(&.>))("_))(&({::))
>
> NB. (a) Mutual
> recursion....................................................................
>
> pre=. o <:
> zip=. @.(0 = ])
>
> NB. Odd/Even (first a classic simple example)
> NB. see http://en.wikipedia.org/wiki/Mutual_recursion
>
> NB. Using pro-verbs
>
> Even=. ( Odd pre)`1: zip
> Odd =. (Even pre)`0: zip
>
> (Even g ,: Odd g) o i. 11
> 1 0 1 0 1 0 1 0 1 0 1
> 0 1 0 1 0 1 0 1 0 1 0
>
> NB. Anonymously using $: and recursion scope (103!:0)
>
> NB. The idea is to write the recursive verbs as agenda cases of a single
> encompassing
> NB. recursive verb.
>
> Even=. 0&oe
> Odd =. 1&oe
>
> ( oe=. ( ( Odd pre y)`1: zip)`((Even pre y)`0: zip) @. [ )
> Odd@:<:@:]`1:@.(0 = ])`(Even@:<:@:]`0:@.(0 = ]))@.[
>
> ( oe=. oe f. ) NB. Unfortunately,
> 1&oe@:<:@:]`1:@.(0 = ])`(0&oe@:<:@:]`0:@.(0 = ]))@.[
>
> NB. Fixing oe manually,
> ( oe=. ( ( (1&$:) pre y)`1: zip)`(((0&$:) pre y)`0: zip) @. [ )
> 1&$:@:<:@:]`1:@.(0 = ])`(0&$:@:<:@:]`0:@.(0 = ]))@.[
>
> Even f.
> 0&(1&$:@:<:@:]`1:@.(0 = ])`(0&$:@:<:@:]`0:@.(0 = ]))@.[ (103!:0))
> Odd f.
> 1&(1&$:@:<:@:]`1:@.(0 = ])`(0&$:@:<:@:]`0:@.(0 = ]))@.[ (103!:0))
>
> (Even f.g ,: Odd f.g) o i. 11
> 1 0 1 0 1 0 1 0 1 0 1
> 0 1 0 1 0 1 0 1 0 1 0
>
> NB. Female/Male sequences
> NB.
> http://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Female_and_Male_sequences
>
> NB. Using pro-verbs
>
> female=. (] - male o (female pre))`1: zip M.
> male=. (] - female o (male pre))`0: zip M.
>
> (female f.g ,: male f.g) o i. 22
> 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12 13 13
> 0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12 12 13
>
> st'(female g ,: male g) o i. 222'
> ┌─────┬─────────┐
> │19136│0.0159093│
> └─────┴─────────┘
>
> NB. Anonymously using $: and recursion scope (103!:0)
>
> NB. Again, the idea is to define the recursive verbs as agenda cases of
> a single
> NB. encompassing recursive verb.
>
> female=. 0&fm
> male=. 1&fm
>
> fm=. ((] - male o (female pre y))`1: zip)`((] - female o (male pre
> y))`0: zip) @. [ M.
>
> fm f. NB. Unfortunately,
> (] - 1&fm@:(0&fm@:<:@:]))`1:@.(0 = ])`((] - 0&fm@:(1&fm@:<:@:]))`0:@.(0 =
> ]))@.[M.
>
> NB. Fixing fm manually,
> fm=. ((] - (1&$:) o ((0&$:) pre y))`1: zip)`((] - (0&$:) o ((1&$:) pre
> y))`0: zip) @. [ M.
>
> male f.
> 1&((] - 1&$:@:(0&$:@:<:@:]))`1:@.(0 = ])`((] - 0&$:@:(1&$:@:<:@:]))`0:@.(0
> = ]))@.[M. (103!:0))
> female f.
> 0&((] - 1&$:@:(0&$:@:<:@:]))`1:@.(0 = ])`((] - 0&$:@:(1&$:@:<:@:]))`0:@.(0
> = ]))@.[M. (103!:0))
>
> (female f.g ,: male f.g) o i. 22
> 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12 13 13
> 0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12 12 13
>
> st'(female f.g ,: male f.g) o i. 222'
> ┌──────┬──────────┐
> │155136│0.00219392│
> └──────┴──────────┘
>
> NB. There is a space-time tradeoff but the huge savings in space and
> time for
> NB. both versions come from the use of memoization (M.).
>
>
> NB.
> ........................................................................................
>
> NB. Someone might think that in a permissive environment the rules for
> writing tacitly are
> NB. discarted. On the contrary, they are more relevant than ever and it
> is crucial to have
> NB. in mind one of my favorite generic rules, "Learn the rules so you
> know how to break them
> NB. properly." Why? Because the power emanating from the tacit rules
> of J is multiplied
> NB. when it is delivered at higher levels (where tacit rules were not
> supposed to operate).
>
> NB. General preliminary pro-words for programming wickedly...
>
> Cloak=. ]: NB. The monadic case is equivalent to,
> NB. Cloak=. (<(<,'4'),<(<(<,'4'),<;:'0:`'),<(<,'4'),<;:',^:')
> (0:`)(,^:)
>
> NB. Verbalizing all primitive adverbs and conjunctions (only a few are
> used in this session),
>
> ( (X=. 'power tilde dot even odd colon obverse adverse cut fit foreign
> slash slashdot back backdot trap brace rank tie knot evoke atop agenda at
> amper under dual appose') (;:x ,: ]) Y=. ;: '^: ~ . .. .: : :. :: ;. !. !:
> / /. \ \. ]. } " ` `. `: @ @. @: & &. &.: &:' )
> ┌─────┬─────┬───┬────┬───┬─────┬───────┬───────┬───┬───┬───────┬─────┬────────┬────┬───────┬────┬─────┬────┬───┬────┬─────┬────┬──────┬──┬─────┬─────┬────┬──────┐
> │power│tilde│dot│even│odd│colon│obverse│adverse│cut│fit│foreign│slash│slashdot│back│backdot│trap│brace│rank│tie│knot│evoke│atop│agenda│at│amper│under│dual│appose│
> ├─────┼─────┼───┼────┼───┼─────┼───────┼───────┼───┼───┼───────┼─────┼────────┼────┼───────┼────┼─────┼────┼───┼────┼─────┼────┼──────┼──┼─────┼─────┼────┼──────┤
> │^: │~ │. │.. │.: │: │:. │:: │;. │!. │!: │/
> │/. │\ │\. │]. │} │" │` │`. │`: │@ │@. │@:│&
> │&. │&.: │&: │
> └─────┴─────┴───┴────┴───┴─────┴───────┴───────┴───┴───┴───────┴─────┴────────┴────┴───────┴────┴─────┴────┴───┴────┴─────┴────┴──────┴──┴─────┴─────┴────┴──────┘
> ( (X)=. ]: o < e Y ) NB. Verbalizing primitive adverbs and conjunctions
> ┌───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───...
> │(]:(<'^:'))│(]:(<,'~'))│(]:(<,'.'))│(]:(<'..'))│(]:(<'.:'))│(]:(<,':'))│(]:(<':.'))│(]:(<'::'))│(]:(<';.'))│(]:(<'!.'))│(]:(<'!:'))│(]:(<,'/'))│(]:(<'/.'))│(]:(<,'\'))│(]:(<'\.'))│(]:(<'].'))│(]:(<,'}'))│(]:(<,'"'))│(]:(<,'`'))│(]:(<'`.'))│(]:(<'`:'))│(]:...
> └───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───...
>
> ( (X=. 'bdot derivative Derivative secant fix hypergeometric LevelAt
> memo spread TaylorCoeff WeightedTaylor TaylorApprox') (;:x ,: ]) Y=. ;: 'b.
> d. D. D: f. H. L: M. S: t. t: T.' )
> ┌────┬──────────┬──────────┬──────┬───┬──────────────┬───────┬────┬──────┬───────────┬──────────────┬────────────┐
> │bdot│derivative│Derivative│secant│fix│hypergeometric│LevelAt│memo│spread│TaylorCoeff│WeightedTaylor│TaylorApprox│
> ├────┼──────────┼──────────┼──────┼───┼──────────────┼───────┼────┼──────┼───────────┼──────────────┼────────────┤
> │b. │d. │D. │D: │f. │H. │L: │M. │S:
> │t. │t: │T. │
> └────┴──────────┴──────────┴──────┴───┴──────────────┴───────┴────┴──────┴───────────┴──────────────┴────────────┘
> ( (X)=. ]: o < e Y ) NB. Verbalizing the rest of the primitive adverbs
> and conjunctions
> ┌───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┬───────────┐
> │(]:(<'b.'))│(]:(<'d.'))│(]:(<'D.'))│(]:(<'D:'))│(]:(<'f.'))│(]:(<'H.'))│(]:(<'L:'))│(]:(<'M.'))│(]:(<'S:'))│(]:(<'t.'))│(]:(<'t:'))│(]:(<'T.'))│
> └───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┴───────────┘
>
> ( train=. evoke&6 ) NB. train
> evoke&6
>
> NB. Preliminary wicked pro-words...
>
> box=. < o train g f. NB. Boxing verbs from gerund
>
>
> ( an=. <@:((":0) ,&< ]) ) NB. Atomizing a noun
> <@:((,'0') ,&< ])
> ( ew=. adverb ]: (ar'an') ) NB. Encasing a word (adv)
> (1]:(<(<'@:'),<(<,'<'),<(<,'3'),<(<<;._1 ' 0 0'),(<(<,'&'),<;:',<'),<,']'))
> ( ew=. adverb ]: an (f.ew) )
> (1]:(<(,'0');<@:((,'0') ,&< ])))
> ( af=. an o fix f.) NB. Atomizing after fixing (an alternative
> to atomic representations)
> <@:((,'0') ,&< ])@:(]:(<'f.'))
> ( wl=. 104!:1 ) NB. Word from linear (similar to Dan's
> dont)
> 104!:1
>
> NB. These (verbalized primitive adverbs and conjunctions) operate as
> brain surgeons,
> NB. borrowing Dan's analogy [8], on conscious patients (verbs and
> nouns). Neither there is
> NB. a need to knock the arguments out into atomic representations (via
> `'') nor to wake them
> NB. up via a culminating train (`:6).
>
> NB. (b) Adv without (@.
> N)..................................................................
>
> NB. An orthodox way...
>
> ( a0=. `'' ) ( a1=. (@:[) ((<'&')`) (`:6) ) ( a2=. (`(an _)) (`:6) )
> ((`'')(((@:[)(&`))(`:6)))((`_)(`:6))
>
> (('a0'f.) (u a1) ('a2'f.))
> ((`'')(&(u@:[)))((`_)(`:6))
>
> ( Adv0=. ((ar'a0')`) (`(ar'a1')) (`(ar'a2') ) (`:6) )
> (((`''`)(`(((@:[)(&`))(`:6))))(`((`_)(`:6))))(`:6)
> u Adv0
> ((`'')(&(u@:[)))((`_)(`:6))
>
> 1 2 3 % Adv0 NB. Testing...
> 1 0.5 0.333333
>
> NB. A wicked way...
>
> ( Adv=. ((af'a0')`) (`(af'a1')) (`(af'a2') ) (`:6) )
> (((`''`)(`(((@:[)(&`))(`:6))))(`((`_)(`:6))))(`:6)
>
> u Adv
> ((`'')(&(u@:[)))((`_)(`:6))
>
> 1 2 3 % Adv NB. Testing...
> 1 0.5 0.333333
>
> st e '1 2 3 % Adv0' ; '1 2 3 % Adv' NB. They look the same but they are
> not,
> ┌─────────────────┬─────────────────┐
> │┌────┬──────────┐│┌────┬──────────┐│
> ││6272│3.80344e_5│││5120│1.38856e_5││
> │└────┴──────────┘│└────┴──────────┘│
> └─────────────────┴─────────────────┘
>
>
> NB.
> ........................................................................................
>
> NB. adv and conj adverbs...
> ( adv=. 'ew'f. (adverb ]: adverb ((&]:) ew)) ) ( conj=. 'ew'f. (adverb
> ]: conjunction ((&]:) ew)) )
> ((1]:(<(,'0');<@:((,'0') ,&<
> ])))(1]:(<(,'0');1&]:)))((1]:(<(,'0');<@:((,'0') ,&<
> ])))(1]:(<(,'0');2&]:)))
>
> Fetch=. (] ([ amper train y)(({::`'')c))e o i. f.adv NB. Word mnemonics
> 5 Fetch NB. Example use (see (c) below),
> ┌───────┬───────┬───────┬───────┬───────┐
> │0&({::)│1&({::)│2&({::)│3&({::)│4&({::)│
> └───────┴───────┴───────┴───────┴───────┘
>
> NB. Strand forms...
>
> NB. (d) Gerund to strand adverb
> converter...................................................
>
> NB. Writing the adverb (sna) is a lot easier that one might think at
> first glance because,
> NB. due to the right-associativity property, one only has to write one
> adverb (sna) that
> NB. (using the number of arguments) collects the rest of the strand as a
> list (the
> NB. arguments, and the adverb to be converted) and evaluates this list
> as a train formed by
> NB. the (atomic representation of the) curtail (}:) and the tail ({:).
>
> NB. sna...
>
> C=. _2 NB. Counter position
> last=. 1 >: C f NB. Is the last word in the queue?
> dec=. <@:(<: o (C f)) C} ] NB. Decrementing the counter
> next=. {: train o , an o dec NB. Producing the next adverb
> eval=. train o ((an o }:) ; {:) NB. Evaluating an adverb form ( [: (an o
> }:) {: Train )
> main=. (next`(eval o (C&}.)) @. last) o knot f.conj NB. knoting and
> reproducing itself and finally evaluating
> sna=. (af'main')&(train o ([ ,(an o (>:y ; [)))f.) adv NB. The (main)
> conjunction is referring indirectly to itself
> sna NB. It is fixed...
> (1]:(<(,'0');(<(,'0');(2]:(<(,'0');({: (]:(<'`:'))&6@:, <@:((,'0') ,&<
> ])@:(<@:(<:@:(_2&({::))) _2} ]))`((]:(<'`:'))&6@:(<@:((,'0') ,&< ])@:}: ;
> {:)@:(_2&}.))@.(1 >: _2&({::))@:(]:(<'`.')))))&((]:(<'`:'))&6@:([ ,
> <@:((,'0') ,&< ])@:(>:@:] ; [)))))
>
>
> NB.
> ........................................................................................
>
> NB. Sometimes the number of arguments of an adverb is variable. This
> case can be handled
> NB. using a sentinel. Both, sna and particularly sa, were inspired by
> [9]. Their anonymous
> NB. (fixed) recursive implementation is based on the classic trick
> (Gödelian self-reference,
> NB. Quines, u/y combinators): self reference and reproduction via
> indirect reference to a
> NB. self-representation; see also [10,11,12] and references there in.
>
> NB. sa...
>
> cap=. (<'[:') (train x -: ]) [ NB. Using cap ([:) as a sentinel
> main=. ({:y train o , an o knot)`(eval o }:y) @. cap f.conj NB. knoting
> and reproducing itself and finally evaluating
> sa=. (fix'main') (af'main') NB. The (main) conjunction is referring
> indirectly to itself
> sa NB. It is fixed...
> (2]:(<(,'0');({:@:] (]:(<'`:'))&6@:, <@:((,'0') ,&<
> ])@:(]:(<'`.')))`((]:(<'`:'))&6@:(<@:((,'0') ,&< ])@:}: ;
> {:)@:}:@:])@.((<'[:') ((]:(<'`:'))&6@:[ -: ])
> [)))(<(,'0');(2]:(<(,'0');({:@:] (]:(<'`:'))&6@:, <@:((,'0') ,&<
> ])@:(]:(<'`.')))`((]:(<'`:'))&6@:(<...
>
> NB. Train...
>
> darkhorse=. (('train'(wl ew) (train x at ]) train o (([ , (;`'') ,
> ])/))) f.
> u0`u1`u2`u3 template=. darkhorse f. adv NB. Arguments as a gerund
> train@:(u0 ; u1 ; u2 ; u3)
> [: u0 u1 u2 u3 Train=. (af'template') sa NB. Arguments as a strand
> train@:(u0 ; u1 ; u2 ; u3)
>
> NB. (c) Recursion (reproduces the behavior of
> (`:4))........................................
>
> ( 'v0 v1 v2 v3 self'=. 5 Fetch ) NB. Verbs mnemonics
> ┌───────┬───────┬───────┬───────┬───────┐
> │0&({::)│1&({::)│2&({::)│3&({::)│4&({::)│
> └───────┴───────┴───────┴───────┴───────┘
> ( $:`'' ( items=. box o (,~) ) v0`v1`v2`v3 ) NB. Verbs
> ┌──┬──┬──┬──┬──┐
> │v0│v1│v2│v3│$:│
> └──┴──┴──┴──┴──┘
>
> sentence=. (v1 tie ([: v2 (self at v3) Train)) agenda v0
> NB. v1 ` ( v2 $: @: v3 ) @. v0
>
> [: v2 (self at v3) Train NB. is just a convenient (and more meaningful)
> way to write,
> train@:(v2 ; self at v3)
>
> ( darkhorse=. sentence o ($:`''&items) )v0`v1`v2`v3
> v1`(v2 $:@:v3)@.v0
>
> ( Recursion=. darkhorse f.adv ) NB. Recursion as a
> fixed adverb
> (1]:(<(,'0');((1&({::) (]:(<,'`')) (]:(<'`:'))&6@:(2&({::) ; 4&({::)
> (]:(<'@:')) 3&({::))) (]:(<'@.'))
> 0&({::))@:((,<'$:')&(<@:((]:(<'`:'))&6)"_1@:(,~)))))
>
> ( fact=. *`1:`*`<: Recursion ) NB. Factorial
> 1:`(* $:@:<:)@.*
> fact 5
> 120
>
> ( Fib=. >&1`(i.@>:)`(] , +/@(_2&{.)@])`<: Recursion ) NB. Fibonacci
> i.@>:`((] , +/@(_2&{.)@]) $:@:<:)@.(>&1)
> Fib 7
> 0 1 1 2 3 5 8 13
>
>
> NB.
> ........................................................................................
>
> Recur=. (ar'Recursion') 4 sna NB. Recur is fixed
> automatically
>
> fact=. * 1: * <: Recur NB. Factorial
>
> fact 5
> 120
>
> Fib=. (>&1) (i.@>:) (] , +/@(_2&{.)@]) <: Recur NB. Fibonacci
>
> Fib 7
> 0 1 1 2 3 5 8 13
>
> NB. Bonding...
>
> rd=. <: Recur NB. rd is fixed automatically
>
> fact=. * 1: * rd
>
> fact 5
> 120
>
> Fib=. (>&1) (i.@>:) (] , +/@(_2&{.)@]) rd
>
> Fib 7
> 0 1 1 2 3 5 8 13
>
>
> NB. (e)
> Permutations........................................................................
>
> 'Proposition Basis Combine Reduction' (i."_ 0 &: ;:) 'Reduction Combine
> Basis Proposition'
> 3 2 1 0
>
> NB. Typical adverb taking a gerund
> <:`*`1:`* (((3 2 1 0&{)Adv) Recursion) 5
> 120
>
> tgv=. `:6
> (*`1:`*`<:) (af'Recur') ga=. (af'tgv') 2 sna
> 1:`(* $:@:<:)@.*
> (<:`*`1:`*) (3 2 1 0&{adv) (af'Recur') ga
> 1:`(* $:@:<:)@.*
>
> afTemplate=. an o ([: an o ([: (0&{) ((af'adv')c) Train) ((an o (1&{))c)
> ((af'ga')c) Train) f.adv
> ( ( 3 2 1 0&{ adv ) (af'Recur' )
> ( ga ) )
> (1]:(<(,'0');3 2 1 0&{))((2]:(<(,'0');({: (]:(<'`:'))&6@:, <@:((,'0') ,&<
> ])@:(<@:(<:@:(_2&({::))) _2} ]))`((]:(<'`:'))&6@:(<@:((,'0') ,&< ])@:}: ;
> {:)@:(_2&}.))@.(1 >: _2&({::))@:(]:(<'`.'))))((<(,'0');(2]:(<(,'0');({:
> (]:(<'`:'))&6@:, <@:((,'0') ,&< ])@:...
> NB. template=. (3 2 1 0&{adv) (af'Recur') ga
> NB. recur=. (af'template') 4 sna
> NB. <: * 1: * recur 5
> NB. <: (] , +/@(_2&{.)@]) (i.@>:) (>&1) recur 7
> NB.
> NB. (3 2 1 0&{) ` (af'Recur') afTemplate
> NB. an o ([: an o ([: (0&{) ((af'adv')c) Train) ((an o (1&{))c)
> ((af'ga')c) Train) ((3 2 1 0&{)) ` (af'Recur')
> NB. recur=. (((3 2 1 0&{) ` (af'Recur')) afTemplate) 4 sna
> NB. recur=. ((3 2 1 0&{) (af'Recur') (af'afTemplate') 2 sna) 4 sna
>
> Permute=. (af'afTemplate') 2 sna
>
> recur=. (3 2 1 0&{) (ar'Recur') Permute 4 sna NB. Reversing the list os
> arguments,
>
> <: * 1: * recur 5
> 120
> <: (] , +/@(_2&{.)@]) (i.@>:) (>&1) recur 7
> 0 1 1 2 3 5 8 13
>
> recur=. |. (ar'Recur') Permute 4 sna NB. An equivalent direct
> way,
>
> <: * 1: * recur 5
> 120
> <: (] , +/@(_2&{.)@]) (i.@>:) (>&1) recur 7
> 0 1 1 2 3 5 8 13
>
> NB. Alternatively...
>
> recur=. (3 2 1 0&{) (af'Recur') Permute 4 sna
>
> <: * 1: * recur 5
> 120
> <: (] , +/@(_2&{.)@]) (i.@>:) (>&1) recur 7
> 0 1 1 2 3 5 8 13
>
> recur=. |. (af'Recur') Permute 4 sna
>
> <: * 1: * recur 5
> 120
> <: (] , +/@(_2&{.)@]) (i.@>:) (>&1) recur 7
> 0 1 1 2 3 5 8 13
>
> NB. Checking...
>
> * 1: * <: Recur 5
> 120
> * 1: * <: |.(ar'recur') Permute 4 sna 5
> 120
>
> (>&1) (i.@>:) (] , +/@(_2&{.)@]) <: Recur 7
> 0 1 1 2 3 5 8 13
> (>&1) (i.@>:) (] , +/@(_2&{.)@]) <: |.(ar'recur') Permute 4 sna 7
> 0 1 1 2 3 5 8 13
>
>
> NB. [8] making a 'first' adverb tacit
> NB.
> http://www.jsoftware.com/pipermail/programming/2013-November/033914.html
> NB. [9] Third argument (was: avoid boxing with fills - ?)
> NB.
> http://www.jsoftware.com/pipermail/programming/2009-July/015541.html
> NB. [10] J Myths Puzzles (Last Spoiler)
> NB.
> http://www.jsoftware.com/pipermail/programming/2008-March/009915.html
> NB. [11] Tacit Programming in Action: A Decade of Experience (Slides
> 99-111)
> NB. http://www.2bestsystems.com/j/J_Conference_2012. All
> NB. [12] Y Combinator -- "the J way"
> NB.
> http://www.jsoftware.com/pipermail/programming/2012-June/028335.html
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm