User-Defined Functions/Operators/Hyperators: Difference between revisions

From NARS2000
Jump to navigationJump to search
No edit summary
No edit summary
Line 2: Line 2:
   <li><p>'''User-defined Functions and Operators'''</p>
   <li><p>'''User-defined Functions and Operators'''</p>
     <ul>
     <ul>
       <li><p>Within a user-defined function/operator header, the '''left''' and '''right''' arguments may consist of one or more names separated by one or more blanks and enclosed in parentheses.  In this context, the caller of the function/operator must pass a scalar or an appropriate length vector argument.  Upon invocation of the function/operator, these values are assigned one item per name.</p>
       <li><p>Within a user-defined function/operator header, the '''left''' and '''right''' arguments may consist of one or more names each separated by one or more blanks and enclosed in parentheses.  In this context, the caller of the function/operator must pass a scalar or an appropriate length vector argument.  Upon invocation of the function/operator, these values are assigned one item per name.</p>


<p>For example, a function whose header looks like</p>
<p>For example, a function whose header looks like</p>
Line 11: Line 11:


<p>Note that if only one name appears within parentheses in the left or right argument, vector arguments must be singletons.</p></li>
<p>Note that if only one name appears within parentheses in the left or right argument, vector arguments must be singletons.</p></li>
      <li><p>A user-defined function/operator header result may consist of two or more names separated by one or more blanks.  In this context, when the function exits, the individual names must all have a value (else a <apll>VALUE ERROR</apll> is signaled), the names are joined together in a strand, and the resulting vector is returned as the result.  If the header contains only name for the result, it may '''not''' be enclosed in parentheses.</p></li>


       <li><p>The result of a user-defined function/operator may be marked as non-displayable by enclosing the result part of the header in braces, as in <apll>∇ {Z}←FOO R</apll>. If the result part of the header consists of multiple names, either <apll>∇ {Z<sub>1</sub> Z<sub>2</sub>}←FOO R</apll>, <apll>∇ ({Z<sub>1</sub> Z<sub>2</sub>})←FOO R</apll>, or <apll>∇ {(Z<sub>1</sub> Z<sub>2</sub>)}←FOO R</apll> may be used to mark the result as non-displayable.</p></li>
       <li><p>A user-defined function/operator header '''result''' may consist of two or more names each separated by one or more blanks.  In this context, when the function exits, the individual names must all have a value (else a <apll>VALUE ERROR</apll> is signaled), the names are joined together in a strand, and the resulting vector is returned as the result. If the header contains only one name for the result, it may '''not''' be enclosed in parentheses.</p></li>


       <li><p>A user-defined function/operator may be called with an optional left argument (ambivalent) by enclosing the left argument in braces, as in <apll>∇ Z←{L} FOO R</apll>.  If the left argument part of the header consists of multiple names, either <apll>∇ Z←{L<sub>1</sub> L<sub>2</sub>} FOO R</apll>, <apll>∇ Z←({L<sub>1</sub> L<sub>2</sub>}) FOO R</apll>, or <apll>∇ Z←{(L<sub>1</sub> L<sub>2</sub>)} FOO R</apll> may be used to mark the left argument as optional.  The system function <apll>⎕NC</apll> may be used to detect the presence/absence of the left argument.</p></li>
      <li><p>The '''result''' of a user-defined function/operator may be marked as non-displayable by enclosing the result part of the header in braces, as in <apll>∇ {Z}←FOO R</apll>. If the result part of the header consists of multiple names, either <apll>∇ {Z<sub>1</sub> Z<sub>2</sub>}←FOO R</apll>, <apll>∇ ({Z<sub>1</sub> Z<sub>2</sub>})←FOO R</apll>, or <apll>∇ {(Z<sub>1</sub> Z<sub>2</sub>)}←FOO R</apll> may be used to mark the result as non-displayable.</p></li>
 
       <li><p>A user-defined function/operator may be called with an '''optional left''' argument ('''ambivalent''') by enclosing the left argument in braces, as in <apll>∇ Z←{L} FOO R</apll>.  If the left argument part of the header consists of multiple names, either <apll>∇ Z←{L<sub>1</sub> L<sub>2</sub>} FOO R</apll>, <apll>∇ Z←({L<sub>1</sub> L<sub>2</sub>}) FOO R</apll>, or <apll>∇ Z←{(L<sub>1</sub> L<sub>2</sub>)} FOO R</apll> may be used to mark the left argument as optional.  The system function <apll>⎕NC</apll> may be used to detect the presence/absence of the left argument.</p></li>


       <li><p>Thanks to Dyalog APL for proposing and implementing the above features.</p></li>
       <li><p>Thanks to Dyalog APL for proposing and implementing the above features.</p></li>


       <li><p>A user-defined function/operator may accept the axis operator as an additional argument, just as primitive function/operators do.  The axis operator may be specified in the header as in <apll>∇ Z←FOO[X] R</apll> for a function, <apll>∇ Z←(LO FOO[X]) R</apll> for a monadic operator, and <apll>∇ Z←(LO FOO[X] RO) R</apll> for a dyadic operator.</p></li>
       <li><p>A user-defined function/operator may accept the '''axis operator''' as an additional argument, just as primitive function/operators do.  The axis operator may be specified in the header as in <apll>∇ Z←FOO[X] R</apll> for a function, <apll>∇ Z←(LO FOO[X]) R</apll> for a monadic operator, and <apll>∇ Z←(LO FOO[X] RO) R</apll> for a dyadic operator.</p></li>


       <li>A user-defined function/operator may define a separate entry point for when it is called on a prototype &mdash; use the line label <apll>⎕PROTOTYPE:</apll>.  For example, the monadic function <apll>FOO</apll> when used in <apll>FOO¨R</apll> where <apll>R</apll> is an empty array is entered at the line labeled <apll>⎕PROTOTYPE:</apll>.  If no line has that label, the function/operator is entered at line 1.</li>
       <li>A user-defined function/operator may define a '''separate entry point''' for when it is called on a prototype &mdash; use the line label <apll>⎕PROTOTYPE:</apll>.  For example, the monadic function <apll>FOO</apll> when used in <apll>FOO¨R</apll> where <apll>R</apll> is an empty array is entered at the line labeled <apll>⎕PROTOTYPE:</apll>.  If no line has that label, the function/operator is entered at line 1.</li>
     </ul>
     </ul>
   </li>
   </li>

Revision as of 13:16, 28 August 2008

  • User-defined Functions and Operators

    • Within a user-defined function/operator header, the left and right arguments may consist of one or more names each separated by one or more blanks and enclosed in parentheses. In this context, the caller of the function/operator must pass a scalar or an appropriate length vector argument. Upon invocation of the function/operator, these values are assigned one item per name.

      For example, a function whose header looks like

      ∇ Z←FOO (R1 R2)

      can be called with a two-element vector (or a scalar which is extended to a two-element vector) right argument only. If the right argument is of rank greater than one, a RANK ERROR is signaled. If the right argument is not of the proper length, a LENGTH ERROR is signaled.

      Note that if only one name appears within parentheses in the left or right argument, vector arguments must be singletons.

    • A user-defined function/operator header result may consist of two or more names each separated by one or more blanks. In this context, when the function exits, the individual names must all have a value (else a VALUE ERROR is signaled), the names are joined together in a strand, and the resulting vector is returned as the result. If the header contains only one name for the result, it may not be enclosed in parentheses.

    • The result of a user-defined function/operator may be marked as non-displayable by enclosing the result part of the header in braces, as in ∇ {Z}←FOO R. If the result part of the header consists of multiple names, either ∇ {Z1 Z2}←FOO R, ∇ ({Z1 Z2})←FOO R, or ∇ {(Z1 Z2)}←FOO R may be used to mark the result as non-displayable.

    • A user-defined function/operator may be called with an optional left argument (ambivalent) by enclosing the left argument in braces, as in ∇ Z←{L} FOO R. If the left argument part of the header consists of multiple names, either ∇ Z←{L1 L2} FOO R, ∇ Z←({L1 L2}) FOO R, or ∇ Z←{(L1 L2)} FOO R may be used to mark the left argument as optional. The system function ⎕NC may be used to detect the presence/absence of the left argument.

    • Thanks to Dyalog APL for proposing and implementing the above features.

    • A user-defined function/operator may accept the axis operator as an additional argument, just as primitive function/operators do. The axis operator may be specified in the header as in ∇ Z←FOO[X] R for a function, ∇ Z←(LO FOO[X]) R for a monadic operator, and ∇ Z←(LO FOO[X] RO) R for a dyadic operator.

    • A user-defined function/operator may define a separate entry point for when it is called on a prototype — use the line label ⎕PROTOTYPE:. For example, the monadic function FOO when used in FOO¨R where R is an empty array is entered at the line labeled ⎕PROTOTYPE:. If no line has that label, the function/operator is entered at line 1.
  • User-defined Operators

    • A user-defined operator is distinguished from a user-defined function by its header, such that in the place where the function name normally appears, it contains a left operand name, an operator name, and an optional right operand name (for dyadic operators only), all enclosed in parentheses. For example, operator part of the header for a monadic operator looks like (LO OP1), and a dyadic operator looks like (LO OP2 RO).

      Both types of user-defined operators may be called monadically or dyadically, as in

      Monadic derived function Dyadic derived function
      Monadic operator: ∇ Z←(LO OP1) R ∇ Z←L (LO OP1) R
      Dyadic operator: ∇ Z←(LO OP1 RO) R ∇ Z←L (LO OP1 RO) R
    • Just like user-defined functions, user-defined operators may have multiple names in the result, left, and right arguments (but not the left and/or right operands), an optional left argument, an axis operator, and a non-displayable result.

      A header using all these features might look like

      ∇ {(Z1 Z2)}←{(L1 L2 L3)} (LO OP2[X] RO) (R1 R2 R3 R4)