File r37/lisp/csl/html/r37_0199.html artifact 8494bcb296 part of check-in 52fc28dabe



<A NAME=LET>

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



<B>LET</B> _ _ _  _ _ _  _ _ _  _ _ _ <B>command</B><P>
<P>
 
 <P>
<P>
The <em>let</em> command defines general or specific substitution rules. 
 <P> <H3> 
syntax: </H3>
<P>
<P>
<em>let</em>&lt;identifier&gt; <em>=</em> &lt;expression&gt;{,&lt;identifier&gt;
 
<em>=</em> &lt;expression&gt;}* 
<P>
<P>
<P>
&lt;identifier&gt; can be any valid REDUCE identifier except an array, and in 
some cases can be an expression; &lt;expression&gt; can be any valid REDUCE 
expression. 
<P>
<P>
 <P> <H3> 
examples: </H3>
<P><PRE><TT>
let a = sin(x); 

b := a; 

  B := SIN X; 


let c = a; 

exp(a); 

   SIN(X)
  E       


a := x**2; 

        2
  A := X  


exp(a); 

    2
   X
  E   


exp(b); 

   SIN(X)
  E       


exp(c); 

    2
   X
  E   


let m + n = p; 

(m + n)**5; 

   5
  P  


operator h; 

let h(u,v) = u - v; 

h(u,v); 

  U - V 


h(x,y); 

  H(X,Y) 


array q(10); 

let q(1) = 15; 

  ***** Substitution for 0 not allowed

</TT></PRE><P>The <em>let</em> command is also used to activate a <em>rule sets
</em>. 
 <P> <H3> 
syntax: </H3>
<P>
<P>
<em>let</em>&lt;list&gt;{,&lt;list&gt;}+ 
<P>
<P>
<P>
&lt;list&gt; can be an explicit 
<A HREF=r37_0060.html>rule</A> <em>list</em>, or evaluate 
to a rule list. 
<P>
<P>
 <P> <H3> 
examples: </H3>
<P><PRE><TT>
trig1 := {cos(~x)*cos(~y) =&gt; (cos(x+y)+cos(x-y))/2,
          cos(~x)*sin(~y) =&gt; (sin(x+y)-sin(x-y))/2,
          sin(~x)*sin(~y) =&gt; (cos(x-y)-cos(x+y))/2,
          cos(~x)^2       =&gt; (1+cos(2*x))/2,
          sin(~x)^2       =&gt; (1-cos(2*x))/2}$ 

let trig1;
cos(a)*cos(b); 

  COS(A - B) + COS(A + B)
  ------------------------
             2

</TT></PRE><P>A <em>let</em> command returns no value, though the substitution r
ule is 
entered. Assignment rules made by 
<A HREF=r37_0065.html>assign</A> and <em>let</em> 
rules are at the 
same level, and cancel each other. There is a difference in their 
operation, however, as shown in the first example: a <em>let</em> assignment 
tracks the changes in what it is assigned to, while a <em>:=</em> assignment 
is fixed at the value it originally had. 
<P>
<P>
The use of expressions as left-hand sides of <em>let</em> statements is a 
little complicated. The rules of operation are: 
<P>
<P>
 _ _ _ (i) 
Expressions of the form A*B = C do not change A, B or C, but set A*B to C. 
<P>
<P>
 _ _ _ (ii) 
Expressions of the form A+B = C substitute C - B for A, but do not change 
B or C. 
<P>
<P>
 _ _ _ (iii) 
Expressions of the form A-B = C substitute B + C for A, but do not change 
B or C. 
<P>
<P>
 _ _ _ (iv) 
Expressions of the form A/B = C substitute B*C for A, but do not change B or 
C. 
<P>
<P>
 _ _ _ (v) 
Expressions of the form A**N = C substitute C for A**N in every expression of 
a power of A to N or greater. An asymptotic command such as A**N = 0 sets 
all terms involving A to powers greater than or equal to N to 0. Finite 
fields may be generated by requiring modular arithmetic (the 
<A HREF=r37_0305.html>modular</A> 
switch) and defining the primitive polynomial via a <em>let</em> statement. 
<P>
<P>
<P>
<em>let</em>substitutions involving expressions are cleared by using 
the 
<A HREF=r37_0189.html>clear</A> command with exactly the same expression. 
<P>
<P>
Note when a simple <em>let</em> statement is used to assign functionality to an 

operator, it is valid only for the exact identifiers used. For the use of the 
<em>let</em> command to attach more general functionality to an operator, 
see 
<A HREF=r37_0195.html>forall</A>. 
<P>
<P>
Arrays as a whole cannot be arguments to <em>let</em> statements, but 
matrices as a whole can be legal arguments, provided both arguments are 
matrices. However, it is important to note that the two matrices are then 
linked. Any change to an element of one matrix changes the corresponding 
value in the other. Unless you want this behavior, you should not use 
<em>let</em> for matrices. The assignment operator 
<A HREF=r37_0065.html>assign</A> can be used 
for non-tracking assignments, avoiding the side effects. Matrices are 
redimensioned as needed in <em>let</em> statements. 
<P>
<P>
When array or matrix elements are used as the left-hand side of <em>let</em> 
statements, the contents of that element is used as the argument. When the 
contents is a number or some other expression that is not a valid left-hand 
side for <em>let</em>, you get an error message. If the contents is an 
identifier or simple expression, the <em>let</em> rule is globally attached 
to that identifier, and is in effect not only inside the array or matrix, 
but everywhere. Because of such unwanted side effects, you should not 
use <em>let</em> with array or matrix elements. The assignment operator 
<em>:=</em> can be used to put values into array or matrix elements without 
the side effects. 
<P>
<P>
Local variables declared inside <em>begin</em>...<em>end</em> blocks cannot 
be used as the left-hand side of <em>let</em> statements. However, 

<A HREF=r37_0040.html>begin</A>...<em>end</em> blocks themselves can be used as 
the 
right-hand side of <em>let</em> statements. The construction: 
 <P> <H3> 
syntax: </H3>
<P>
<P>
<em>for all</em>&lt;vars&gt; 
 <em>let</em>&lt;operator&gt;(&lt;vars&gt;)<em>=</em>&lt;block&gt; 
<P>
<P>
<P>
is an alternative to the 
 <P> <H3> 
syntax: </H3>
<P>
<P>
<em>procedure</em>&lt;name&gt;(&lt;vars&gt;)<em>;</em>&lt;block&gt; 
<P>
<P>
<P>
construction. One important difference between the two constructions is that 
the &lt;vars&gt; as formal parameters to a procedure have their global values 
protected against change by the procedure, while the &lt;vars&gt; of a 
<em>let</em> statement are changed globally by its actions. 
<P>
<P>
Be careful in using a construction such as <em>let x = x + 1</em> except inside 

a controlled loop statement. The process of resubstitution continues until 
a stack overflow message is given. 
<P>
<P>
The <em>let</em> statement may be used to make global changes to variables from 

inside procedures. If <em>x</em> is a formal parameter to a procedure, the 
command <em>let x = </em>... makes the change to the calling variable. 
For example, if a procedure was defined by 
<P><PRE><TT>
        procedure f(x,y);
        let x = 15;
</TT></PRE><P><P>
<P>
and the procedure was called as 
<P><PRE><TT>
        f(a,b);
</TT></PRE><P><P>
<P>
<em>a</em>would have its value changed to 15. Be careful when using <em>let</em>
 
statements inside procedures to avoid unwanted side effects. 
<P>
<P>
It is also important to be careful when replacing <em>let</em> statements with 
other <em>let</em> statements. The overlapping of these substitutions can be 
unpredictable. Ordinarily the latest-entered rule is the first to be applied. 
Sometimes the previous rule is superseded completely; other times it stays 
around as a special case. The order of entering a set of related <em>let</em> 
expressions is very important to their eventual behavior. The best 
approach is to assume that the rules will be applied in an arbitrary order. 
<P>
<P>
<P>


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