# Evaluation (Lec 07)

## yukita@k.hosei.ac.jp

### •Evaluate and Unevaluated

### •Set(=) and SetDelayed(:=)

See the difference between **Set **

and **SetDelayed. **

**lhs := rhs**

assignsrhsto be the delayed value of **lhs**

. **rhs**

is maintained in an unevaluated form.When **lhs**

appears,it is replaced by **rhs**

,evaluated afresh each time.

Further examples show that the function definition can be affected by the global variable.

### •Function Definitions and Global Variables

The following function definition is affected by the global variable which is not desirable in many cases.

The following way of definition works fine even when there exists a global variable having the same name as the parameter.

But still we have

What happens when **SetDelayed **

is used.

### •The HoldFirst attribute

A function with the **HoldFirst **

attribute accepts the first argument without evaluation.

#### •With SetDelayed (:=)

#### •With Set (=)

### •Examples of Set and SetDelayed

The following function definition will yield a warning, but you will see it works fine.

If you use **SetDelayed**

, you will not see any warnings.

### •Altering array elements

The following assignment is not allowed.

However, if the array is stored in a variable, then assignment makes sense.

Let us see how altering array elements is carried out in a function definition.

Is this what you expect?

Still, you got the undesired result. A solution to this drawback is to use the **HoldFirst **

attribute.

### •Hold

The Hold function protects an expression from evaluation.

### •Exercises

#### •Problem 1

Explain the difference between the two definitions of the function **f**

: one using **Set**

and the other using **SetDelayed**

. If you are not familiar with the **Condition**

(**/;**

) operator, consult the on-line help.

Definition with Set;

Definition with SetDelayed;

#### •Problem 2

Write a function **g**

that behaves like below.

**g[x^y]**

returns `{x,y}`.

**g[2^10]**

returns `{2,10}`.

##### •Solution

Converted by *Mathematica*
(June 3, 2003)