The tetration is an operation defined as an iterated exponentiation (e.g 2^^4 = 2^(2^(2^2)) = 65536)
I'm learning the J programming language and want to experiment with the ^: (power of verb) function . Specifically, I want to use it to tacitly compute the tetration of two numbers. I want to do this tacitly in order to better understand modifier trains in J.
So far, I've only been able to write this code: ^/@# . It creates a list of x copies of y and inserts ^ between the items, effectively producing y^^x. However, I don't want that solution, because it doesn't let x be infinite, and it also doesn't use the ^: function. I haven't been able to make any further progress. Can anyone help me?
If the point is to experiment with ^:
and tacit code, this is a good use case for the gerund form of ^:
:
2 ^^:(]`1:) 4 NB. 2 {{x ^^:y 1}} 4
65536
2 ^^:((]-1:)`[) 4 NB. 2 {{x ^^:(y-1) x}} 4
65536
A gerund right operand to ^:
that contains only two verbs instead of three, as in v1`v2
, is taken to mean [`v1`v2
.
Just to be clear, since you mentioned exploring modifier trains: tacit verbs should not be confused with modifier trains. Modifier trains are always modifiers (that is, adverbs or conjunctions), as opposed to verbs. While it seems silly to use a tacit conjunction for tetration rather an using a verb, here are two examples just for comparison's sake:
2 ([.&^^:].@1 ].) 4 NB. 2 {{m ^^:n 1}} 4
65536
2 ([.&^^:(].-1:) [.) 4 NB. 2 {{m ^^:(n-1) m}} 4
65536
As a side note, the F::
conjunction does not take a "times" argument as stated in Eelvex's answer. In order to effectively use it this way, you'd have to use Z:
(Terminate Fold) with the F.
variant, as in
4 {{x ] F. (y^] [ _2:Z:x=Z:@0) y}} 2
65536
This example exits at the start of the x-th iteration, keeping only the result of the first x-1 iterations. To clarify: the v
in u F. v
is tacit here in order to bring y
into scope from the containing explicit verb {{...}}
-- hence e.g. the use of Z:@0
. This means x
and y
are being used here as constant tines in a compound fork.