MaxEvalDepth , Hold , Eval , While , Until , If , SystemCall , Function , Use , For , ForEach , Apply , LocalSymbols , Subst , WithValue , TraceExp , TraceRule , Control flow functions

Control flow functions


MaxEvalDepth(n)

Use this command to set the maximum evaluation depth. This will catch any infinite recursion. For example, f(x):=f(x); and then f(x) would keep on calling f(x), which would call f(x), which would call f(x)... etcetera. The interpreter will halt if the call is n deep. The default value is 100000.


Hold(expression)

Hold(expression) : returns expression unevaluated.


Eval(expression)

Eval(expression) : Re-evaluates expression.


While(predicate) body

While(predicate) body : Keep on evaluating "body" while "predicate" evaluates to "True". "predicate" is tested before evaluating the body. While returns "True".


Until(predicate) body

Until repeats a statement until a predicate becomes true. A difference with While is that the statement is evaluated at least once, as opposed to While where the body statement is evaluated zero or more times.


If(predicate,then,else)

If(predicate,then,else) : If statement. If "predicate" evaluates to "True", return result of evaluating the "then" body, else if there is a "else" body, return that result, otherwise return "False".


SystemCall(string)

SystemCall(string) : This will call a command in the surrounding shell Yacas was invoked from. SystemCall can be used to pass commands to other programs or the operating system.


Function("operator",{arguments} ) body

Function("operator",{arguments} ) body : Use this to declare a simple function, one that doesn't need an entire rules data base.


Use("file")

Use("file") : This function loads a file if it was not already loaded with Use.


For(start,predicate,increment) body

For(start,predicate,increment) body : Looping in a C style. "start" gets called, and then "body" as long as "predicate" evaluates to True", evaluating "increment" each time after body was evaluated. Returns "True".


ForEach(item,{list}) body

ForEach(item,{list}) body : This function loops over each element in {list}, assigning it to the variable "item", and calling "body". Returns "True".


Apply("oper",{list})

Apply("oper",{list}) : This function applies a operator to the arguments mentioned in the list. Eg. "Apply("+",{2,3});" would evaluate to "5". You can also apply pure functions, declared using the form {varlist,body}. Example: "Apply( {{x,y},x+y} , {2,3} );" would also evaluate to 5.


LocalSymbols(...)body

Given the symbols passed as the first arguments to LocalSymbols a set of local symbols will be created, and creates unique ones for them, typically of the form $, where symbol was the symbol entered by the user, and number is a unique number. This scheme was used to such a generated symbol can not accientally be entered by a user. Example:
In> LocalSymbols(a,b)a+b
Out> $a6+ $b6;
This is useful in cases where a guaranteed free variable is needed, like in the macro-like functions (For, While, etc.).


Subst(from,to)body

Subst replaces any occurrence of from in body with to.
Example: Subst(x,Sin(y)) x+x -> Sin(y)+Sin(y)


WithValue(variable,value,expression)

WithValue(variable,value,expression) : evaluate expression, with variable set to value. variable and value can be lists of variables and values.


TraceExp(expression)

TraceExp(expression) : turn on tracing facility, and evaluate expression. This is useful for tracing the evaluation of small routines interactively from the command line.


TraceRule(template)expression

Tracerule(template)expression : turn on tracing facility given the template, and evaluate expression. the template is an example of the function to trace on. template=x+y would trace all additions, showing the arguments passed in, and the result of the addition. Only user-defined functions can be traced.

This is useful for tracing a function that is called from within another function. This way you can see how your function behaves in the environment it is used in.

An example invocation of TraceRule is
In( 1 ) = TraceRule(x+y)2+3*5+4;

Which should then show something to the effect of
    ENTER:2+3*5+4

ENTER:2+3*5

ARG:2 <- 2

ARG:3*5 <- 15

LEAVE:2+3*5 -> 17

ARG:2+3*5 <- 17

ARG:4 <- 4

LEAVE:2+3*5+4 -> 21

Out( 0 ) = 21;