Index   <<   >>

The long and the short of it

This example takes a conventional multi-line J definition and compares it to a tacit form of the same verb. The function is named entree and its definition is

   m=.>./x
   i=.<:#x
   y=.''
   z=.y
   b=.-.+./"1 DC x
L0)$.=.$.(i=0)LR L1
   $.=.$.(m~:i{x)LR L2
   x=.i{z
   y=.(x(i{b)LR<x),y
   j=.<:i
   $.=.L2((j{x)<i{x)LR $
   z=.(<(j{z),y)j}z
   y=.''
   i=.j
   $.=.L0
L2)i=.<:i
   $.=.L0
L1)i=.<:#x
   y=.''
   m=.<:m
   $.=.$.(m~:0)LR L0
   z=.(x=1)#z
   z(1=#z)LR 0{z

[Editor’s note: the above definition uses labels L0) and the $. sequence control that are no longer supported. In 1995 they were superseded by the if. else. etc. control structures.]

This 23-line definition, which includes two defined verbs DC and LR , four conditional and two unconditional branches, and eight local variables, can be replaced by the following six definitions, which contain no branches and no variables.

entree =. et@(<"0@[ ,. ])
 et=. et0`et1@.ett
  et0=.i.@0:
  et1=.mask <@({:@{.,et@}.);.1 ]
  ett=. *@#
   mask=.(={.)@:({."1)

Both take a depth list left argument, and a list of boxed values as a right argument, and yield a tree with the given depth and values.

See the paper From Trees into Boxes, by Steinbrook and McDonnell, in the APL93 conference proceedings for a detailed discussion.