Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!mach1!torn!utnut!cs.utexas.edu!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!decwrl!csus.edu!sfsuvax1.sfsu.edu!emclean
From: emclean@sfsuvax1.sfsu.edu (Emmett McLean)
Subject: jfaq (page 2 draft)
Message-ID: <1993Mar27.113042.19549@csus.edu>
Sender: news@csus.edu
Organization: San Francisco State University
Date: Sat, 27 Mar 1993 11:30:42 GMT
Lines: 169

NB. Here is a stab at a second page for a J FAQ.

NB. Comments, suggestions, and additions are welcome.  

NB. ==================================================================
NB. Once you've installed J on your machine you can load 
NB. this file into J by typing 0!:2 <'thisfilename'
    9!:3(5)

NB. The best place to find answers to your questions is to
NB. ftp to watserv1.waterloo and get the archive of postings 
NB. to comp.lang.apl in "languages/apl/comp.lang.apl"

NB. The questions in this post address a few fundamental 
NB. programming topics.

NB. 1. How do I read a text file into a character table?
 
NB. 2. How do I reassign a column in a table ?

NB. 3. How do I reassign a row in a table ?

NB. 4. How do I implement selective reassignment ?

NB. 5. How do I use explicit definition to write a program which
NB.    loops (branches) ?

NB. =================================================================
NB. 1. How do I read a text file into a character table ?
NB. =================================================================
 
NB. Try :
    Read  =. >@(<&}:;.2)@(1!:1&<)
NB. Usage is :
NB. Read 'filename'

NB. =================================================================
NB. 2. How do I reassign a column in a table ?
NB. =================================================================

NB. Comment. In a language like APL row reassignment into a 
NB. table is accomplished using bracket notation. Ken Iverson
NB. felt that this lead to asymmetries in the language.
NB. In J column and row reassignment can be accomplished
NB. in a variety of ways.   The list below is not 
NB. comprehensive. It is designed so that one can can study 
NB. the examples and experiment with various approaches and 
NB. choose a verb best suiting his asthetic sense
NB. or which runs the fastest for his particular application.
NB. Don't expect to understand much of what follows if you
NB. are new to J and/or do not have a copy of the "Dictionary
NB. of J". Use the verb test to experiment with time and
NB. space measures of the following:

test =. >@(6!:2 ; 7!:2)

NB. For reassigning one column try:
    ac0 =. -@(>&{.)@[ |."1 {.@(>&}.)@[ ,. }."1@(>&{.@[ |."1 ])
    ac1 =. ,@>&(1&{)@[`($@] #. i.@{.@$@] ,. >&(0&{)@[)`]}
NB. (ac1 requires jv6.1 or 6.2)
    ac1s =. '(>@(1&{) x.) (($ y.) #. (i.@{.@$ y.) ,"0/ (>@(0&{) x.)) } y.':11
    ac2 =.  '(>@(1&{) x.) (>@(0&{) x.) }"1 y.':11 
NB. Usage :
    ] table =. 10 * i. 4 3
    (2    ;    3 3 3 3) ac0  table
    (2    ;    3 3 3 3) ac1  table
    (2    ;    3 3 3 3) ac1s table
    ((,2) ; ,. 3 3 3 3) ac2  table

NB. For reassigning several columns at once try ac2 or ac1s :
    ac2 =. '(>@(1&{) x.)  (>@(0&{) x.) }"1 y.':11 
NB. Usage :
    (0 2 ; 4 2 $ 3) ac2 table
    (0 2 ; 4 2 $ 3) ac1s table


NB. =================================================================
NB. 3. How do I reassign a row in a table ?
NB. =================================================================

NB. For reassigning one row try :
ra0 =. -@(>&{.)@[ |. >&}.@[ , }.@(>&{.@[ |. ])
NB. Usage :
    ( 2 ; 3 # 3 ) ra0 table

NB. For reassigning several rows at once try:
ar1 =. ac2&.|:
NB. Usage :
    (0 2 ; |: 2 3 $ 3) ar1 table

NB. =================================================================
NB. 4. How do I implement selective reassignment ?
NB. =================================================================

   s1 =.'(''''&$@>&(1&{) x.) (($ y.) #. (>@(0&{) x.)) } y.':11
   s2 =. ''&$@>&(1&{)@[`($@] #. >&(0&{)@[)`]}

NB. If you are using Jv6.0 or earlier try s1
NB. s2 only works on Jv6.1 and higher.
NB. Useage:
    (1 1 ; 100) s1 table
    (1 1 ; 100) s2 table

NB. =================================================================
NB. 5. How do I use explicit definition to write a program which loops?
NB. =================================================================

NB. (BTW,  these examples are meant to provide a starting point for 
NB. those familiar with imperative languages.)
 
   a =. i. 0 0           NB. Create a null character table
   a =. a, 'i =. 1'      NB. Initialize counting index       
   a =. a, 'n =. 100 '   NB. set an upper limit to the number of loops
   a =. a, 'loop) '      NB. earlier post.
   a =. a, 'y. =. y. + 2' 
   a =. a, 'i =. >: i'   NB. Increment the counter
   a =. a, '$. =. > (i = 100) { loop ; end '  NB. branch to loop if i neq 100
   NB. Else branch to end
   a =. a, 'end) y.'      
   r =. a : '' "0
   NB. Create an explicit defn verb from the character
   NB. table a and define it so if acts on the atomic
   NB. components of its inputs. 
   5!:2 <'r'             NB. Display verb

   NB. One important point, the comparison defining the loop condition
   NB. the result must have rank 0. For example :

   $ (100 = 100)

   NB. In the more general of comparisons, one sometimes has to cast the result
   NB. of the comparison to be a scalar 0 or a scalar 1. For example, suppose 
   NB. a loop terminates if two nouns a and c (which are not scalars) match .
   NB. Then the statement in the definition of the character table b would
   NB.  look like :

   NB. a =. a, '$. =. > ''''&$@(i = 100) { loop ; end '

   NB. - - - - -
   NB.   For those familiar with APL's execute command, here is the
   NB.   corresponding J implementation :
   b =. i. 0 0  
   b =. b, 'i =. 1'       
   b =. b, 'n =. 100' 
   b =. b, 'loop) ".(i -: n)#''$.=. end'' '  
   NB. if i matches n then
   NB. then the suite is assign
   NB. to point to end (and what
   NB. follows end which in this
   NB. case is nothing).
   b =. b, 'y. =. y. + 2'
   b =. b, 'i =. >: i'
   b =. b, '$. =. loop'
   b =. b, 'end) y.'
   t =. b : '' "0   

   NB.  - - - - -
   NB. Here is more efficient example

   l =. 0 0 $'' 
   l=.l,  'n =. 99'
   l=.l,  '$. =., n #,: loop_start -. loop_end '
   l=.l,  'loop_start)'
   l=.l,  'y. =. y. + 2 '
   l=.l,  'loop_end)'
   s=. l : ''  "0

   NB. execute r, s, and t simultaneously with an arugment of 3 :
   r`s`t`:0 (3)
