Your one-liner on parentheses nesting has an ancient and honorable pedigree. See the Ken Iverson Wikipedia page <https://en.wikipedia.org/wiki/Kenneth_E._Iverson> (in the IBM (1960-1980) section) and the third paragraph of Alan Perlis's "*APL is more French than English*" <https://www.jsoftware.com/papers/perlis78.htm>.
On Sun, Mar 15, 2020 at 12:38 AM Devon McCormick <[email protected]> wrote: > There's this (adapted from my new "Array Thinking" page on the J wiki): > NB. String from https://wiki.c2.com/?LispShowOffExamples > string=. '(flet ((inside-p (obj) (lambda (d) (inside-p obj (ray-point > ray d)))))' > string,:' '-.~":'()' ([:+/\1 _1 0{~i.) string NB. Example result > (flet ((inside-p (obj) (lambda (d) (inside-p obj (ray-point ray d))))) > 1111112333333333344443344444444554455555555555555666666666666666654321 > NB. Final "1" indicates we are missing a closing paren > The one-liner is " '()' ([:+/\1 _1 0{~i.) string"; the bit preceding this > simply squishes the parentheses' counts to single, text digits so they > align character-by-character with the string (won't work if the string > presents more than 9 levels of nesting). > Also, that "LISP Show-off" page referenced might be a good source of > succinct algos amenable to translation into J. > > Another one I resurrected recently is something I wrote a long time ago in > APL called "interval sum". Given two two-column tables - "iv0" and "iv1" - > of start, stop points defining intervals, this expression returns the > (#iv0) x #iv1 table of intersections between each interval. This is > positive for intervals that overlap, negative for disjunct intervals > (measuring the gap between them), and zero for intervals that share only > one endpoint. So, for example: > > (iv0=. 0 5,_1 4,2 3,6 3,5 8,:6 8);iv1=. _1 0,1 3,0 5,_1 8,:9 10 > +----+-----+ > | 0 5|_1 0| > |_1 4| 1 3| > | 2 3| 0 5| > | 6 3|_1 8| > | 5 8| 9 10| > | 6 8| | > +----+-----+ > > iv0 (([:|: [:/:~"1 [) (([:|:[ 0}~ [:- 0{[) +/ . <./ ]0}~ [:- 0{]) [:|: > [:/:~"1 ]) iv1 > 0 2 5 5 _4 > 1 2 4 5 _5 > _2 1 1 1 _6 > _3 0 2 3 _3 > _5 _2 0 3 _1 > _6 _3 _1 2 _1 > > The following attempts to illustrate how the inner product " +/ . <./ " > works. The trick is that we negate the 0th column of each set of intervals > (after sorting so that the starting point of the interval is less than or > equal to the ending point) so that the <./ gives us the negative of the > larger of the two starting points. I have a marvelous proof of why this > works but the margin is too small to contain it*. > > In any case, we see here the two sets of intervals as shown above but with > their initial columns negated and the result of the inner product > positioned in a way to try to make clear what is happening: > +----+-----------------+ > ||:iv1| 1 _1 0 1 _9 | > |iv0 | 0 3 5 8 10 | > +-----+----------------+ > | 0 5 | 0 2 5 5 _4 | (0<.1)+(5<.0)=0; (0<._1)+(5<.3)=2; > (0<.0)+(5<.5)=5... > | 1 4 | 1 2 4 5 _5 | (1<.1)+(4<.0)=1; (1<._1)+(4<.3)=2; > (1<.0)+(4<.5)=4... > |_2 3 |_2 1 1 1 _6 | > |_6 3 |_3 0 2 3 _3 | > |_5 8 |_5 _2 0 3 _1 | > |_6 8 |_6 _3 _1 2 _1 | > +-----+----------------+ > > *Come to NYCJUG on Tuesday, April 14th to see the proof. > > > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
