:= | assign a variable or a list; define a function |
Set | assignment |
Clear | undo an assignment |
++ | increment variable |
-- | decrement variable |
Object | create an incomplete type |
LazyGlobal | global variable is to be evaluated lazily |
UniqueConstant | create a unique identifier |
var := expr {var1, var2, ...} := {expr1, expr2, ...} var[i] := expr fn(arg1, arg2, ...) := expr |
expr -- expression to assign to the variable or body of function
i -- index (can be integer or string)
fn -- atom, name of a new function to define
arg1, arg2 -- atoms, names of arguments of the new function fn
The first form is the most basic one. It evaluates the expression on the right-hand side and assigns it to the variable named on the left-hand side. The left-hand side is not evaluated. The evaluated expression is also returned.
The second form is a small extension, which allows one to do multiple assignments. The first entry in the list on the right-hand side is assigned to the first variable mentionedon the left-hand side, the second entry on the right-hand side to the second variable on the left-hand side, etcetera. The list on the right-hand side must have at least as many entries as the list on the left-hand side. Any excess entries are silently ignored. The result of the expression is the list of values that have been assigned.
The third form allows one to change an entry in the list. If the index "i" is an integer, the "i"-th entry in the list is changed to the expression on the right-hand side. It is assumed that the length of the list is at least "i". If the index "i" is a string, then "var" is considered to be an associative list (sometimes called hash table), and the key "i" is paired with the value "exp". In both cases, the right-hand side is evaluated before the assigment and the result of the assignment is True.
The last form defines a function. For example, the assignment fn(x) := x^2 removes any rules previously associated with fn(x) and defines the rule fn(_x) <-- x^2. Note that the left-hand side may take a different form if fn is defined to be a prefix, infix or bodied function. This case is special since the right-hand side is not evaluated immediately, but only when the function fn is used. If this takes time, it may be better to force an immediate evaluation with Eval (see the last example). If the expression on the right hand side begins with Eval(), then it will be evaluated before definining the new function.
A variant of the function definition can be used to make a function accepting a variable number of arguments. The last argument
In> a := Sin(x) + 3; Out> Sin(x)+3; In> a; Out> Sin(x)+3; |
Multiple assignments:
In> {a,b,c} := {1,2,3}; Out> {1,2,3}; In> a; Out> 1; In> b+c; Out> 5; |
Assignment to a list:
In> xs := { 1,2,3,4,5 }; Out> {1,2,3,4,5}; In> xs[3] := 15; Out> True; In> xs; Out> {1,2,15,4,5}; |
Building an associative list:
In> alist := {}; Out> {}; In> alist["cherry"] := "red"; Out> True; In> alist["banana"] := "yellow"; Out> True; In> alist["cherry"]; Out> "red"; In> alist; Out> {{"banana","yellow"},{"cherry","red"}}; |
Defining a function:
In> f(x) := x^2; Out> True; In> f(3); Out> 9; In> f(Sin(a)); Out> Sin(a)^2; |
Defining a function with variable number of arguments:
In> f(x, ...) := If(IsList(x),Sum(x),x); Out> True; In> f(2); Out> 2; In> f(1,2,3); Out> 6; |
Defining a new infix operator:
In> Infix("*&*",10); Out> True; In> x1 *&* x2 := x1/x2 + x2/x1; Out> True; In> Sin(a) *&* Cos(a); Out> Tan(1)+Cos(1)/Sin(1); In> Clear(a); Out> True; In> Sin(a) *&* Exp(a); Out> Sin(a)/Exp(a)+Exp(a)/Sin(a); |
In the following example, it may take some time to compute the Taylor expansion. This has to be done every time the function f is called.
In> f(a) := Taylor(x,0,25) Sin(x); Out> True; In> f(1); Out> x-x^3/6+x^5/120-x^7/5040+x^9/362880- x^11/39916800+x^13/6227020800-x^15/ 1307674368000+x^17/355687428096000-x^19/ 121645100408832000+x^21/51090942171709440000 -x^23/25852016738884976640000+x^25 /15511210043330985984000000; In> f(2); Out> x-x^3/6+x^5/120-x^7/5040+x^9/362880- x^11/39916800+x^13/6227020800-x^15 /1307674368000+x^17/355687428096000-x^19/ 121645100408832000+x^21/51090942171709440000 -x^23/25852016738884976640000+x^25/ 15511210043330985984000000; |
The remedy is to evaluate the Taylor expansion immediately. Now the expansion is computed only once.
In> f(a) := Eval(Taylor(x,0,25) Sin(x)); Out> True; In> f(1); Out> x-x^3/6+x^5/120-x^7/5040+x^9/362880- x^11/39916800+x^13/6227020800-x^15/ 1307674368000+x^17/355687428096000-x^19/ 121645100408832000+x^21/51090942171709440000 -x^23/25852016738884976640000+x^25 /15511210043330985984000000; In> f(2); Out> x-x^3/6+x^5/120-x^7/5040+x^9/362880- x^11/39916800+x^13/6227020800-x^15 /1307674368000+x^17/355687428096000-x^19/ 121645100408832000+x^21/51090942171709440000 -x^23/25852016738884976640000+x^25/ 15511210043330985984000000; |
Set(var, exp) |
exp -- expression to assign to the variable
The statement Set(var, exp) is equivalent to var := exp, but the := operator has more uses, e.g. changing individual entries in a list.
In> Set(a, Sin(x)+3); Out> True; In> a; Out> Sin(x)+3; |
Clear(var, ...) |
In> a := 5; Out> 5; In> a^2; Out> 25; In> Clear(a); Out> True; In> a^2; Out> a^2; |
Local(var, ...) |
By default, all variables in Yacas are global. This means that the variable has the same value everywhere. But sometimes it is useful to have a private copy of some variable, either to prevent the outside world from changing it or to prevent accidental changes to the outside world. This can be achieved by declaring the variable local. Now only expressions within the Prog block (or its syntactic equivalent, the [ ] block) can access and change it. Functions called within this block cannot access the local copy unless this is specifically allowed with UnFence.
In> a := 3; Out> 3; In> [ a := 4; a; ]; Out> 4; In> a; Out> 4; In> [ Local(a); a := 5; a; ]; Out> 5; In> a; Out> 4; |
In the first block, a is not declared local and hence defaults to be a global variable. Indeed, changing the variable inside the block also changes the value of a outside the block. However, in the second block a is defined to be local and now the value outside the block stays the same, even though a is assigned the value 5 inside the block.
var++ |
In> x := 5; Out> 5; In> x++; Out> True; In> x; Out> 6; |
var-- |
In> x := 5; Out> 5; In> x--; Out> True; In> x; Out> 4; |
Object("pred", exp) |
exp -- expression on which "pred" should be applied
In> a := Object("IsNumber", x); Out> Object("IsNumber",x); In> Eval(a); Out> Object("IsNumber",x); In> x := 5; Out> 5; In> Eval(a); Out> 5; |
LazyGlobal(var) |
Places where this is used include the global variables % and I.
The use of lazy in the name stems from the concept of lazy evaluation. The object the global variable is bound to will only be evaluated when called. The LazyGlobal property only holds once: after that, the result of evaluation is stored in the global variable, and it won't be reevaluated again:
In> a:=Hold(Taylor(x,0,30)Sin(x)) Out> Taylor(x,0,30)Sin(x); In> LazyGlobal(a) |
Then the first time you call a it evaluates Taylor(...) and assigns the result to a. The next time you call a it immediately returns the result. LazyGlobal is called for % each time % changes.
In> a:=Hold(2+3) Out> 2+3; In> a Out> 2+3; In> LazyGlobal(a) Out> True; In> a Out> 5; |
UniqueConstant() |
In> UniqueConstant() Out> C9 In> UniqueConstant() Out> C10 |