File r37/lisp/csl/html/r37_0211.html artifact 86d59ee6a8 part of check-in e08999f63f



<A NAME=OPERATOR>

<TITLE>OPERATOR</TITLE></A>
<b><a href=r37_idx.html>INDEX</a></b><p><p>



<B>OPERATOR</B> _ _ _  _ _ _  _ _ _  _ _ _ <B>declaration</B><P>
<P>
 
Use the <em>operator</em> declaration to declare your own operators. 
 <P> <H3> 
syntax: </H3>
<P>
<P>
<em>operator</em>&lt;identifier&gt;{,&lt;identifier&gt;}* 
<P>
<P>
<P>
&lt;identifier&gt; can be any valid REDUCE identifier, which is not the name 
of a 
<A HREF=r37_0345.html>matrix</A>, 
<A HREF=r37_0188.html>array</A>, scalar variable or previously-defined 
operator. 
<P>
<P>
 <P> <H3> 
examples: </H3>
<P><PRE><TT>
operator dis,fac; 

let dis(~x,~y) = sqrt(x^2 + y^2); 

dis(1,2); 

  SQRT(5) 


dis(a,10); 

        2
  SQRT(A  + 100) 


on rounded; 

dis(1.5,7.2); 

  7.35459040329


let fac(~n) = if n=0 then 1
               else if not(fixp n and n&gt;0)
                then rederr &quot;choose non-negative integer&quot;
               else for i := 1:n product i;
 

fac(5); 

  120 


fac(-2); 

  ***** choose non-negative integer

</TT></PRE><P>The first operator is the Euclidean distance metric, the distance 
of point 
(x,y) from the origin. The second operator is the factorial. 
<P>
<P>
Operators can have various properties assigned to them; they can be 
declared 
<A HREF=r37_0196.html>infix</A>, 
<A HREF=r37_0200.html>linear</A>, 
<A HREF=r37_0222.html>symmetric</A>, 

<A HREF=r37_0187.html>antisymmetric</A>, or 
<A HREF=r37_0206.html>noncom</A><em>mutative</em>. 
The default operator is prefix, nonlinear, and commutative. 
Precedence can also be assigned to operators using the declaration 

<A HREF=r37_0213.html>precedence</A>. 
<P>
<P>
Functionality is assigned to an operator by a 
<A HREF=r37_0199.html>let</A> statement or 
a 
<A HREF=r37_0195.html>forall</A>...<em>let</em> statement, 
(or possibly by a procedure with the name 
of the operator). Be careful not to redefine a system operator by 
accident. REDUCE permits you to redefine system operators, giving you a 
warning message that the operator was already defined. This flexibility 
allows you to add mathematical rules that do what you want them to do, but 
can produce odd or erroneous behavior if you are not careful. 
<P>
<P>
You can declare operators from inside 
<A HREF=r37_0055.html>procedure</A>s, as long as they 
are not local variables. Operators defined inside procedures are global. 
A formal parameter may be declared as an operator, and has the effect of 
declaring the calling variable as the operator. 
<P>
<P>
<P>


REDUCE Historical
REDUCE Sourceforge Project | Historical SVN Repository | GitHub Mirror | SourceHut Mirror | NotABug Mirror | Chisel Mirror | Chisel RSS ]