Something very cool that works though not quite as well as I hoped.  It is 
possible for an adverb to inject other adverbs into a tacit verb.  One 
application of this is having syntactic sugar for tacit code:

eval
=: 1 : ' a: 1 :  m'
strbracket
=: {.@:[ , ] , {:@[
lrA
=: 1 : '5!:5 < ''u''' 

sugartable =:  > ({. (, <) ;: inv@:}. ) each ;: each cutLF 0 : 0 
Y @:] 
X @:[ 
Y0 @:(0 {:: ]) 
Y1 @:(1 {:: ])] 
Y2 @:(2 {:: ])] 
X0 @:(0 {:: [) 
X1 @:(1 {:: [) 
H @:{. 
R @:}. 
T @:{: 
)

sugarA =: 2 : 0 
('()'  strbracket '[ ', ('()' strbracket u lrA) ,'] [  ]', , ('()' strbracket 
;: inv)"1 (;: ,. (<'=.'),. ({:"1 sugartable) {~ ({."1 sugartable) i. ;:) n) 
eval 
)

This design allows not having every possible syntactic sugar definition stored 
in z

   sugartable 
┌──┬────────────────┐ 
│Y │@: ]            │ 
├──┼────────────────┤ 
│X │@: [            │ 
├──┼────────────────┤ 
│Y0│@: ( 0 {:: ] )  │ 
├──┼────────────────┤ 
│Y1│@: ( 1 {:: ] ) ]│ 
├──┼────────────────┤ 
│Y2│@: ( 2 {:: ] ) ]│ 
├──┼────────────────┤ 
│X0│@: ( 0 {:: [ )  │ 
├──┼────────────────┤ 
│X1│@: ( 1 {:: [ )  │ 
├──┼────────────────┤ 
│H │@: {.           │ 
├──┼────────────────┤ 
│R │@: }.           │ 
├──┼────────────────┤ 
│T │@: {:           │ 
└──┴────────────────┘ 

the code is based on this possible expression:

   2 3 ([ (]X + +/Y) ] [ ](X=. @:[) (Y =. @:])) 1 2 3 
8 9
   ([ (]X + +/Y) ] [ ](X=. @:[) (Y =. @:])) 
[ (]@:[ + +/@:]) ] [ ]@:[@:]

simplest use:
   (+:X + +:Y) sugarA 'Y X ' 
[ (+:@:[ + +:@:]) ] [ ]@:]@:[ 

    (+:H R X + +:Y) sugarA 'Y X H R' 
[ (+:@:{.@:}.@:[ + +:@:]) ] [ ]@:]@:[@:{.@:}. 


sugarA takes as n arg a list of sugar adverbs defined from sugartable.  The v 
arg isn't strictly needed as the whole table could be inserted into the 
resulting verb, but passing a table allows for unused sugar to be variables in 
scope, and if the whole table was always used, we might as well use the much 
more convenient z predefinitions.

The u arg is a tacit verb with variables from sugartable.  

The verb that will be executed above is equivalent to:

(+:@:{.@:}.@:[ + +:@:])         NB. double of head of rest of x + double of y

The result could be cleaned up easily to be just this.

   1 2(+:H R X + +:Y) sugarA 'Y X H R' 3 
10 

The only reason I say this does not work well is that there needs to be an even 
number of adverbs in order not to change your tacit verb from hook to fork, as 
the variables are initially parsed as verbs.  So to use an odd number of 
adverbs, you need to bracket or remove some:

   1 2(+:(H X) + +:Y) sugarA 'Y X H R' 3 
8 

or
   1 2(+:H X + +:@]) sugarA 'Y X H R' 3 
8 

This problem does not occur with predefined adverbs, and so given the typing 
savings of "sugarA 'Y X H R'", its easy to prefer that approach.  The problem 
can also be eliminated by passing u as a string, so the approach is interesting 
even though I am putting it aside for now.  


Hopefully, it is also an interesting conjunction example:  Building a tacit 
verb with external references.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to