>>  <<  Usr  Pri  JfC  LJ  Phr  Dic  Voc  !:  Help  Learning J

Chapter 15: Tacit Operators

15.1 Introduction

J provides a number of built-in operators - adverbs and conjunctions. In Chapter 13 we looked at defining our own operators explicitly. In this chapter we look at defining adverbs tacitly.

15.2 Adverbs from Conjunctions

Recall from Chapter 07 the Rank conjunction, ("). For example, the verb (< " 0) applies Box (<) to each rank-0 (scalar) item of the argument.
   < " 0  'abc'
+-+-+-+
|a|b|c|
+-+-+-+
A conjunction takes two arguments. If we supply only one, the result is an adverb. For example, an adverb to apply a given verb to each scalar can be written as (" 0)

each =: " 0 < each z =: < each 'abc'
"0 <"0 +-+-+-+
|a|b|c|
+-+-+-+

The scheme is, that for a conjunction C and a noun N, the expression (C N) denotes an adverb such that:

        x (C N)  means  x C N
The argument to be supplied to the conjunction can be a noun or a verb, and on the left or on the right. Altogether there are four similar schemes:
        x (C N)  means  x C N
        x (C V)  means  x C V
        x (N C)  means  N C x
        x (V C)  means  V C x
The sequences CN CV NC and CV are called "bidents". They are a form of bonding whereby we take a two-argument function and fix the value of one of its arguments to get a one-argument function. However, there is a difference between bonding a dyadic verb (as in + & 2 for example) and bonding a conjunction. With the conjunction, there is no need for a bonding operator such as &. We just write (" 0) with no intervening operator. The reason is that in the case of + & 2, omitting the & would give + 2 which means: apply the monadic case of + to 2, giving 2. However, conjunctions don't have monadic cases, so the bident (" 0) is recognised as a bonding.

Recall the "Under" conjunction &. from Chapter 08 whereby f&.g is a verb which applies g to its argument, then f then the inverse of g. If we take f and g to be:

   f =: 'f' & ,
   g =: >
then we see that f is applied inside each box:

z (f &. g) z
+-+-+-+
|a|b|c|
+-+-+-+
+--+--+--+
|fa|fb|fc|
+--+--+--+

Now, using the form CV, we can define an adverb EACH to mean "inside each box":

EACH =: &. > f EACH z f EACH z
&.> f&.> +-+-+-+
|a|b|c|
+-+-+-+
+--+--+--+
|fa|fb|fc|
+--+--+--+

15.3 Compositions of Adverbs

If A and B are adverbs, then the bident (A B) denotes an adverb which applies A and then B. The scheme is:
         x (A B)   means (x  A) B

15.3.1 Example: Cumulative Sums and Products

There is a built-in adverb \ (backslash, called Prefix). In the expression f \ y the verb f is applied to successively longer leading segments of y. For example:
   < \ 'abc'
+-+--+---+
|a|ab|abc|
+-+--+---+
The expression +/ \ y produces cumulative sums of y:
   +/ \ 1 2 3
1 3 6
An adverb to produce cumulative sums, products, and so on can be written as a bident of two adverbs:
   cum =: / \   NB. adverb adverb
   

z =: 2 3 4 + cum z * cum z
2 3 4 2 5 9 2 6 24

15.3.2 Generating Trains

Now we look at defining adverbs to generate trains of verbs, that is, hooks or forks.

First recall from Chapter 14 the Tie conjunction (`), which makes gerunds, and the Evoke Gerund adverb (`: 6) which makes trains from gerunds.

Now suppose that A and B are the adverbs:

   A =: * `    NB. verb conjunction
   B =: `: 6   NB. conjunction noun
   
Then the compound adverb
   H =: A B
is a hook-maker. Thus <: H generates the hook * <: , that is "x times x-1"

<: A <: A B h =: <: H h 5
+-+--+
|*|<:|
+-+--+
* <: * <: 20

15.3.3 Rewriting

It is possible to rewrite the definition of a verb to an equivalent form, by rearranging its terms. Suppose we start with a definition of the factorial function f. Factorial 5 is 120.
   f =: (* ($: @: <:)) ` 1:  @. (= 0:)
   f 5
120
The idea now is to rewrite f to the form $: adverb, by a sequence of steps. Each step introduces a new adverb. The first new adverb is A1, which has the form conj verb.
   A1 =: @. (= 0:)      
   g  =: (* ($: @: <:)) ` 1: A1
   g 5
120
Adverb A2 has the form conj verb
   A2 =: ` 1:          
   h  =: (* ($: @: <:)) A2 A1
   h 5
120
Adverb A3 has the form adv adv
   A3 =: (* `) (`: 6)   
   i  =: ($: @: <:) A3 A2 A1 
   i 5
120
Adverb A4 has the form conj verb
   A4=: @: <:  
   j  =: $: A4 A3 A2 A1 
   j 5
120
Combining A1 to A4:
   A =: A4 A3 A2 A1 
   k =: $:  A
   k 5
120
Expanding A:
   m =: $: (@: <:) (* `) (`: 6) (` 1:) (@. (= 0:))
   m 5
120
We see that m and f are the same verb:

f m
(* $:@:<:)`1:@.(= 0:) (* $:@:<:)`1:@.(= 0:)

This is the end of Chapter 15.


NEXT
Table of Contents
Index


The examples in this chapter were executed using J version 701. This chapter last updated 29 Jul 2012
Copyright © Roger Stokes 2012. This material may be freely reproduced, provided that this copyright notice is also reproduced.


>>  <<  Usr  Pri  JfC  LJ  Phr  Dic  Voc  !:  Help  Learning J