home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / mbug / mbug172.arc / APL-Z.LBR / APL.NZT / APL.NOT
Text File  |  1979-12-31  |  17KB  |  507 lines

  1.  
  2.  
  3.                               APL/Z
  4.                                by
  5.                           S. Gownowicz
  6.  
  7.  
  8.                     Programming Introduction
  9.                                for
  10.                               CP/M
  11.  
  12.  
  13.  
  14.  
  15.                                by
  16.                          Kevin Smathers
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. Document:        APL/Z.1
  38. Initial release: 8/87
  39. Update history:
  40.  
  41. A short introduction
  42.  
  43.      This  document is tailored for the typical CP/M  programmer,
  44. who  has never used APL before.   It is written to complement  S.
  45. Gownowicz's public domain programming language, APL/Z.
  46.      The  APL  programming language was originally defined  in  a
  47. book by Kenneth Iverson,  _A_Programming_Language_,  published in
  48. 1962.   Iverson's  intention  was to create a  symbolic  language
  49. which would be adequate for describing concisely,  many important
  50. algorithms in mathematics.   As a result, the original APL used a
  51. myriad of special symbols including subscripts,  superscripts and
  52. two-dimensional  program  format  with arrows  to  show  flow  of
  53. control.
  54.      A  modified version of the original APL,  termed APL/360 was
  55. implemented  in  the  late 60's by Iverson and a  group  at  IBM.
  56. Since then APL has been implemented on a variety of machines, all
  57. based largely on the APL/360 design.   APL has two distinguishing
  58. characteristics which set it apart from other languages.
  59.  
  60. 1.    It is designed to be interpreted.  As such it enjoys all of
  61. the  unique  capabilities that interactive programming  languages
  62. have.   APL  includes  a  number  of  special  features  designed
  63. exclusively  for an interactive environment,  inluding  immediate
  64. execution  of expressions entered at the terminal,  and  entering
  65. and editing facilities for subprograms that are associated with a
  66. workspace.  Workspaces in APL/Z can be saved and restored in part
  67. or in whole.
  68.  
  69. 2.    APL directly processes entire data structures.   In APL the
  70. primitive  operations  accept entire  arrays  as  arguments,  and
  71. produce  arrays as results.   Thus in APL the basic unit of  data
  72. tends  to  be an array rather than a single array element  as  in
  73. other  languages.   This  emphasis on array-processing gives  APL
  74. programs   a  unique  appearance  quite  different   from   other
  75. languages.
  76.  
  77.      The  specialized character set of APL lends it enough  power
  78. that a typical one line program can result in a surprising amount
  79. of computation.  APL is ideally suited for the user who wishes to
  80. avoid  key-board  heavy  languages.   It is well  suited  to  the
  81. interactive  environment,  because only a few lines of input  are
  82. required  to get the results you might typically be looking  for.
  83. On  the other hand,  APL is poorly suited for long  programs,  or
  84. programs  which are meant to be maintained over a long period  of
  85. time.
  86.  
  87.                               -----
  88.     Programming languages specialist Daniel McCracken [1970]
  89.     notes that it once took him four hours to decode a four-
  90.     line APL program.
  91.  
  92.  
  93. Introduction to the APL:ASCII keyboard
  94.  
  95.      APL  (A Programming Language) is an  unstructured  language,
  96. with  a  specialized  character  set devoted  to  performing  the
  97. functions  of  the programming language.   In APL  there  are  no
  98. lowercase  alphabetic symbols.   Unshifted letters produce  their
  99. uppercase counterparts, while shifted letters produce the special
  100. APL symbols.   To avoid confusion,  we will use lowercase symbols
  101. for unshifted APL characters.
  102.      Since CP/M machines typically don't have APL character sets,
  103. we  will be using some special conventions.   They fall into four
  104. categories.
  105.  
  106.      1. characters which must be remapped
  107.      2. characters which are common to both APL and ASCII
  108.      3. characters which cannot be remapped
  109.      4. extra characters
  110.  
  111. 1.   The characters listed below have been remapped for ascii:
  112. _________________________________________________________________
  113.  APL   ASCII
  114. _____________
  115.   ..
  116.                     Dieresis (Umlaut) is unmapped (unused in APL)
  117. _____________
  118.   __
  119.         _           High-minus becomes underline
  120. _____________
  121.  
  122.   <=    {           Less-than-or-equal becomes open-brace
  123. _____________
  124.  
  125.   >=    }           Greater-than-or-equal becomes close-brace
  126. _____________
  127.  
  128.   /=    #           Not-equal becomes pound-sign
  129. _____________
  130.  
  131.   \/    !           Logical-or becomes exclamation-point
  132. _____________
  133.  
  134.   /\    &           Logical-and becomes ampersand
  135. _____________
  136.  
  137.   x     *           Times becomes asterisk
  138. _____________
  139.   .
  140.   -     %           Divided-by becomes percent-sign
  141. __.__________
  142.  
  143.   <-    `           Left-arrow becomes grave-accent
  144. _____________
  145.  
  146.   ->    @           Right-arrow becomes at-sign
  147. __________________________________________________________________
  148. 2.    Certain  keys on the APL keyboard also appear on the  ascii
  149. keyboard.  These are not remapped.  They are:
  150. __________________________________________________________________
  151.  
  152. [  Open bracket
  153. ]  close bracket
  154. (  open paren
  155. )  close paren
  156. /  stroke
  157. \  back-stroke
  158. ,  comma
  159. .  period
  160. ;  semi-colon
  161. :  colon
  162. +  plus
  163. -  minus
  164. <  less-than
  165. >  greater-than
  166. =  Equals
  167. __________________________________________________________________
  168.  
  169.  
  170. 3.    Apl also uses additional symbols which we won't be mapping.
  171. These correspond to the shifted keyboard keys.
  172. __________________________________________________________________
  173.  
  174. Q  Question mark                             see (?) in pt. 4
  175. W  Omega            the greek symbol
  176. E  Element          epsilon
  177. R  Rho              the greek symbol
  178. T  Invert (tilde`)                           see (~) in pt. 4
  179. Y  Up-arrow
  180. U  Down-arrow
  181. I  iota             the greek symbol
  182. O  circle           a large open circle
  183. P  power                                     see (^) in pt. 4
  184. A  alpha            the greek symbol
  185. S  cieling          an upside-down 'L'
  186. D  floor            a right-side-up 'L'
  187. F  underline
  188. G  del              an upside-down delta
  189. H  delta            the greek symbol
  190. J  little-circle    a small open circle
  191. K  single quote                              see (') in pt. 4
  192. L  quad             a large open square
  193. Z  subset           an open semi-circle
  194. X  superset         a close semi-circle
  195. C  intersection     an upside-down 'U'
  196. V  union            a right-side-up 'U'
  197. B  encode           an upside-down 'T'
  198. N  decode           a right-side-up 'T'
  199. M  vertical bar                              see (|) in pt. 4
  200. __________________________________________________________________
  201.  
  202.  
  203.  
  204. 4.    That leaves us with several ASCII symbols which haven't yet
  205. been  used,  so  in  order  to simplify  reading  and  typing  in
  206. programs,  the  rest  of the ascii set has been assigned  to  the
  207. characters  on  the  shifed alpha APL keyboard  which  they  most
  208. resemble, or whose function they most closely match.
  209. __________________________________________________________________
  210.  
  211. |    M
  212. ~    T
  213. $    H
  214. ^    P
  215. '    K
  216. "    K
  217. ?    Q
  218. __________________________________________________________________
  219.  
  220.  
  221. Delving into the APL interpreter
  222.  
  223.      APL lines begin tabbed in 8 spaces.   Here you will type you
  224. command, and the interpreter will respond on the next line.
  225.  
  226.      2
  227. 2
  228.      2+2
  229. 4
  230.  
  231.      Here  are four dyadic (meaning with two operands)  operators
  232. you should recognize.
  233.  
  234.      2*3                 multiply
  235. 6
  236.      4%2                 divide
  237. 2
  238.      51                 subract
  239. 4
  240.      2+1                 add
  241. 3
  242.  
  243.      An  important  feature to remember about APL is that  dyadic
  244. operators always use whatever they find immediately on their left
  245. side,  and  the result of evaluating everything on  their  right.
  246. This means that '+' has no precedence over '*'.
  247. .___________________.
  248. .                   .
  249. .     2*3+16%3      .
  250. .           +      .
  251. .         +        .
  252. .       +          .
  253. .     +            .
  254. .___________________.
  255.  
  256.      So that this equation would produce:
  257.  
  258.      2*3+16%3
  259. 4
  260.  
  261.      This  holds  true for all operators,  not just  mathematical
  262. ones.   However,  this  sequence  can  be  altered  in  APL  when
  263. neccessary,  by grouping the term which are to be evaluated first
  264. within parentheses.
  265.  
  266.      (2*3)+1(6%3)
  267. 5
  268.  
  269.      Produces the expected result.   These operators can also  be
  270. applied  to  lists.   A  list  is simply a  sequence  of  numbers
  271. separated by spaces.  Try typing
  272.  
  273.      1+1 2 3
  274. 2 3 4
  275.  
  276.      2*3 2 1
  277. 6 4 2
  278.  
  279.      APL also makes reals available to you,  simply by putting  a
  280. decimal  point within the number.   Exponential notation can also
  281. be used as you would expect.
  282.  
  283.      3.14159*2
  284. 6.28318
  285.  
  286.      6.22e12%1000
  287. 6.022e9
  288.  
  289.      Remember  that  uppercase  letters  are  reserved  for   APL
  290. functions.   Another  operator you will need to know is the unary
  291. minus.
  292.  
  293.      _3+5
  294. 2
  295.  
  296.      And the single quote which is used to create arrays of ASCII
  297. data.
  298.  
  299.      'abcde'
  300. abcde
  301.  
  302.      abcde is a 5 element list.
  303.  
  304.      Dyadic  operators can also be used on two arrays.   In  this
  305. case, their shapes must be equal.  (More on shapes later.)
  306.  
  307.      1 2 3*4 5 6
  308. 4 8 10
  309.  
  310.      Operands  are paired two at a time,  to produce the  result.
  311. Since  arrays are used so frequently in APL,  a simple method  is
  312. provided  for creating arrays which count up.   In  general,  the
  313. monadic  iota  function will produce an array starting at  1  and
  314. counting up to the argument on it's right side.   Iota expects to
  315. find  a scalar,  or a vector of at most one element on it's right
  316. side.
  317.  
  318.      Monadic Iota:
  319.  
  320.      I5
  321. 1 2 3 4 5
  322.  
  323.      I5 5
  324. rank error
  325.      I5 5
  326.      ^
  327.  
  328.      Attempting  to feed iota a higher degree vector will  result
  329. in the error shown.  APL is complaining that the right hand value
  330. is not of the proper rank, also known as dimension.
  331.      Another  dyadic function is the  assignment  function,  left
  332. arrow.   We  will  use  the grave-accent (`)  to  represent  this
  333. character.  Values, such as arrays, character arrays, and scalars
  334. can  be  freely  assigned  to  variables  using  the  left-arrow.
  335. Variables must be alphabetic,  or may additionally start with the
  336. symbol delta ($).
  337.  
  338.      $hello`'Hi there'
  339.      hello
  340. Hi there
  341.  
  342.      'hello'  is an example of a character vector (its shape  has
  343. one dimension, arrays have more than one dimension in the general
  344. case).  Remember,  variables  cannot be uppercase,  as those  are
  345. reserved for the APL functions.
  346.      The  assignment  statement  introduced  above,   is  another
  347. example of a dyadic function.  This particular function, however,
  348. must use a variable as it's left side.  In all other respects, it
  349. fits  the normal perception of what a dydadic function should be.
  350. For example, you might type a line like this:
  351.  
  352.      a`1+b`3
  353.  
  354.      Evaluated right to left, this resolves to ( b`3 ),( a`b+1 ).
  355. In other words, a will have the value 4 and b will have the value
  356. 3.   But now that we have these fine variables defined,  we  will
  357. need a way to save and recall them for later use.
  358.      For  this purpose,  there are the so called system commands.
  359. Basically,  system commands are used to make enquiries about  the
  360. status of the environment or work_space.  Here are a few to start
  361. with:
  362.  
  363.      )VARS               list all defined variables
  364.      )ERASE var_list     deallocate a list of variables
  365.      )SAVE ws_name       save the current workspace as ws_name
  366.      )LOAD ws_name       load a workspace from ws_name
  367.      )FNS                list all defined functions
  368.  
  369.      Additionally,  it should now be high time to go over some of
  370. the errors that you may encounter within APL
  371.  
  372.      CHARACTER ERROR
  373.           an unknown character has appeared out of place.
  374.      SYNTAX ERROR
  375.           a badly formed expression was typed
  376.      DOMAIN ERROR
  377.           the function is not defined for the value given.
  378.      LENGTH ERROR
  379.           a binary function was used with unmatched vector shapes
  380.      VALUE ERROR
  381.           the indicated variable has not been assigned a value
  382.      RANK ERROR
  383.           the number of dimensions of the input is not allowed
  384.      WS FULL
  385.           the last function used up all of the unused workspace
  386.      DEFN ERROR
  387.           your definition was illegal,  one of the variable names
  388.           was probably at fault.
  389.  
  390.      In APL, the standard way of editing is to backspace until
  391. you are over the character in error, press ATTN and type the tail
  392. of  the  string again.   Otherwize,  backspace  will  not  remove
  393. characters from the line.
  394.      Back to functions now, here is another diadic function.  The
  395. function catenation is represented by a comma (,).  For example,
  396.  
  397.      a`'hi'
  398.      b`' there'
  399.      a,b
  400. hi there
  401.  
  402.      This  is  an  example  of the catenation  of  two  character
  403. vectors.   Refreshing your memory, a dyadic function is one which
  404. acts  on  two values.   A monadic function works  with  just  one
  405. value.   A  niladic function is one which requires no values  for
  406. input  at all.
  407.      This  all leads to user defined functions.   The great power
  408. of  APL  is  most  closely linked  with  its  ability  to  define
  409. functions  on the fly which can be used in the same way that  all
  410. of the system defined functions are.
  411.  
  412.      As  with all identifiers,  function names must begin with an
  413. alphabetic (unshifted) letter or delta ($), and may continue with
  414. any other alpha or numeric characters,  but not APL character set
  415. characters.   S$  and  T$ are reserved variables  which  will  be
  416. discussed later.
  417.  
  418.      A function is defined as follows:
  419.  
  420.      G function_name
  421.      equations
  422.      G
  423.  
  424.      The  surrounding  del's  (G)  are  the  function  definition
  425. characters.   They  open  and  close the  function  editor.   The
  426. function  above  would have no explicit  results,  and  no  input
  427. variables.   The  function  would be niladic.   A common use  for
  428. niladic functions without explicit results is to list all of  the
  429. member  functions  of a workspace.   A niladic function would  be
  430. written  which included that documentation in  print  statements,
  431. and would have a name you could remember such as 'menu'.
  432.  
  433.      G menu
  434.      'This is a file of useless functions:'
  435.      'GRIK: experimental nothing function'
  436.      'GRAK: same as above'
  437.      'YICK: pretty boring huh?'
  438.      G
  439.      menu
  440. This is a file of useless functions:
  441. GRIK: experimental nothing function
  442. GRAK: same as above
  443. YICK: pretty boring huh?
  444.  
  445.      Non-explicit  functions  always return each of  the  results
  446. generated  on a separate line,  as though they had been typed  at
  447. the  keyboard  in that sequence.   Non-explicit functions do  not
  448. pass values to other functions.   That is the job of the explicit
  449. function.
  450.  
  451.      G z`pi
  452.      z`3.1415
  453.      G
  454.      pi
  455. 3.1415
  456.      2*pi
  457. 6.2830
  458.  
  459.      Before  exiting the function,  the explicit variable in  the
  460. explicit function declaration must be given a value to return  to
  461. the next function.   The next form of the function declaration is
  462. the  monadic  function.   For instance,  the  following  function
  463. would change temperatures from Fahrenheit to Celsius.
  464.  
  465.      G c ` temp f
  466.      c ` (f-32) * 5 / 9
  467.      G
  468.      temp 32
  469. 0
  470.  
  471.      The full fledged form of the function is a dyadic,  explicit
  472. function.   Listed  below  are all of the  possible  combinations
  473. which can be used to create a function
  474.  
  475. Argl           Explicit       Non Explicit
  476.  0   Niladic   G z`fn         G fn
  477.  1   Monadic   G z`fn a       G fn a
  478.  2   Dyadic    G z`a fn b     G a fn b
  479.  
  480.      I'd  like  to take just a moment to digress.   When  talking
  481. about  functions in any language,  it is important to talk  about
  482. scoping  rules.   In basic,  where there are no local  variables,
  483. scoping is global.   In Pascal,  where a variable name refers  to
  484. the name most closely linked to its own structural shell, scoping
  485. is  termed static.   In general scoping means,  "If I define  two
  486. variables,  both  with the name 'x' which one will I be referring
  487. to  in this procedure?"  APL uses a concept of scoping  which  it
  488. has in common with LISP.  If the variable is not defined locally,
  489. it is checked for locality to the procedure which called it,  and
  490. if not there, the process is repeated until the variable is found
  491. by  unwinding the calling stack.   This type of scoping is  named
  492. 'dynamic'.
  493.      As  you must assume from the preceding discussion,  APL  has
  494. its own way of declaring internal variables.  These variables are
  495. available  to all functions called by the function which  defined
  496. them, until the name is redefined.
  497.  
  498.      The  form  for  the declaration of a local  variable  is  as
  499. follows:
  500.  
  501.      G fn ; lv1 ; lv2 ; lv 3
  502.  
  503.      There  may  be as many local variables  as  necessary,  each
  504. appended  to the list with a new semicolon.   Old values for  the
  505. variables  named are restored when the function finally  returns.
  506. The local values are not retained.
  507.