Explicit-to-tacit translator

From J Wiki
Jump to navigation Jump to search


<=   =>

There is a primitive which automatically converts one-line explicit definitions to an equivalent tacit definition. You can learn a lot about tacit programming by writing one line explicit definitions, converting them to tacit form, and studying the resulting tacit definition.

Let's do this with an explicit fahrenheit definition. A left argument of 3 to : creates an explicit definition. A left argument of 13 to : creates a tacit definition.

   fx =:  3 : '32 + y * 9 % 5'	NB.  3 explicit
   ft =: 13 : '32 + y * 9 % 5'	NB. 13 tacit


Use the Edit | Session Display Form | Linear Display.

   ft
32 + 1.8 * ]


At first glance this is confusing as it introduces several new things at once. The first thing to do is to look at the boxed display.

Use the Edit | Session Display Form | Boxed to select the Box Display.

   ft
+--+-+---------+
|32|+|+---+-+-+|
|  | ||1.8|*|]||
|  | |+---+-+-+|
+--+-+---------+


At the top level of boxing there are 3 boxes. This is a train with three elements: noun verb verb. The train noun verb verb is treated as noun"_ verb verb and this is a fork.  You can take this thing apart by giving names to the parts and looking at them separately.

The first element of the fork is the phrase 32"_ . Give this a name and experiment with it a bit as a monadic verb.

   left =: 32"_
   left 123
32
   left i.5
32


Whatever you give left as an argument, it just returns 32. You've seen the " conjunction before, but not with a constant left argument. Let's look this up in NuVoc. When you turn to the definition " for rank you will notice that there are three pages of definitions, each with its own page. The three pages are:

	Rank	u " n
	Rank	m " n
	Assign rank	m " v	u " v	mv lv rv


The different definitions are for the rank conjunction used with different types of arguments. In the headings m and n indicate noun arguments and u and v indicate verb arguments. Your earlier use of " involved a verb left argument and a noun right argument and is covered by the first definition. Both 32 and _ (infinity) are nouns so it is the second definition that is relevant.

Reading the definition for m " n makes it clear that the observations are correct. With a right rank of _ , the derived verb applies to its entire right argument, and no matter what it is, it returns the left argument, which is 32.

Let's look at the right element of the fork.

   right =: 1.8 * ]
   right 23
41.4
   right 10
18


Let's not worry about the details of the definition, but again, by observation, what the verb right does is to multiply its argument by 1.8 (which is 9%5).

The final definition is a fork.

   ff =: left + right
   ff 100
212
   ff 0
32

Compare your custom built tacit definition from the previous lesson with the automatically translated one and note how different they are.

32&+@(1.8&*)
32+1.8*]

Tacit programming is very rich and varied and is tightly tied to adverbs and conjunctions such as bond, atop, and rank, and to trains such as hook and fork.

<=   =>

Primer Index               Hover to reveal titles   -   Click to access   -   Current page is highlighted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
45 46 47 48
50 51 52 53 54 55 56 57
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
97 98 99 100 101 102 103 104 105 106