Evaluation (Lec 07)

yukita@k.hosei.ac.jp

•Evaluate and Unevaluated

Head[{1, 2, 3, 4}]

List

FullForm[{1, 2, 3, 4}]

List[1, 2, 3, 4]

Head[{{1, 0}, {0, 1}}]

List

FullForm[{{1, 0}, {0, 1}}]

List[List[1, 0], List[0, 1]]

Head[100]

Integer

FullForm[100]

100

Head[Unevaluated[Plus[1, 2]]]

Plus

Head[Plus[1, 2]]

Integer

Trace[Head[Plus[1, 2]]]

{{1 + 2, 3}, Head[3], Integer}

Trace[Head[Unevaluated[Plus[1, 2]]]]

{Head[1 + 2], Plus}

•Set(=) and SetDelayed(:=)

See the difference between Set and SetDelayed.

x = 100

100

a = x

100

x = 1000

1000

a

100

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.

b := x

b

1000

x = 1

1

b

1

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

Clear[x, f]

x = 100

100

•Function Definitions and Global Variables

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

f[x_] = x^3

1000000

f[2]

1000000

f[y]

1000000

Clear[f]

x

100

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

f = Function[x, x^3]

Function[x, x^3]

f[y]

y^3

But still we have

f[x]

1000000

What happens when  SetDelayed is used.

Clear[f, x]

x = 100

100

f[x_] := x^3

f[10]

1000

f[x]

1000000

Clear[x]

f[x]

x^3

•The HoldFirst attribute

A function with the HoldFirst attribute accepts the first argument without evaluation.

•With  SetDelayed (:=)

ClearAll[f]

f[x_ + y_] := x^2 + y^2

f[2 + 3]

f[5]

SetAttributes[f, HoldFirst]

f[2 + 3]

13

•With Set (=)

ClearAll[f]

f[x_ + y_] = x^2 + y^2

x^2 + y^2

f[2 + 3]

f[5]

SetAttributes[f, HoldFirst]

f[2 + 3]

13

•Examples of Set and SetDelayed

ClearAll[f]

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

f[n_, exp_] = Part[exp, n]

Part :: pspec :  Part specification  n  is neither an integer nor a list of integers.

exp [[ n ]]

f[3, {1, 2, 3}]

3

If you use SetDelayed, you will not see any warnings.

Clear[f]

f[n_, exp_] := Part[exp, n]

f[3, {1, 2, 3}]

3

{1, 2, 3, 4}[[3]]

3

•Altering array elements

The following assignment is not allowed.

{1, 2, 3, 4}[[3]] = 100

Set :: setps :  {1, 2, 3, 4}  in assignment of part is not a symbol.

100

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

a = {1, 2, 3, 4}

{1, 2, 3, 4}

a[[3]] = 100

100

a

{1, 2, 100, 4}

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

Clear[f, n, x]

f[exp_, n_, x_] = (exp[[n]] = x ; exp)

Set :: pspec :  Part specification  n  is neither an integer nor a list of integers.

exp

f[{1, 2, 3, 4}, 3, 100]

{1, 2, 3, 4}

a = {1, 2, 3, 4}

{1, 2, 3, 4}

f[a, 3, 100]

{1, 2, 3, 4}

Is this what you expect?

Clear[f]

f[exp_, n_, x_] := (exp[[n]] = x ; exp)

f[{1, 2, 3, 4}, 3, 100]

Set :: setps :  {1, 2, 3, 4}  in assignment of part is not a symbol.

{1, 2, 3, 4}

a

{1, 2, 3, 4}

f[a, 3, 100]

Set :: setps :  {1, 2, 3, 4}  in assignment of part is not a symbol.

{1, 2, 3, 4}

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

SetAttributes[f, HoldFirst]

f[a, 3, 100]

{1, 2, 100, 4}

•Hold

The Hold function protects an expression from evaluation.

ClearAll[f, x, y]

f[x_^n_ - y_^n_] = x - y

x - y

f[x^3 - y^3]

x - y

f[100^5 - 99^5]

f[490099501]

SetAttributes[f, HoldFirst]

f[100^5 - 99^5]

1

ClearAll[f]

f[x_^n_ - y_^n_] = Hold[x - y]

Hold[x - y]

f[x^3 - z^3]

Hold[x - z]

ReleaseHold[%]

x - z

SetAttributes[f, HoldFirst]

f[100^5 - 99^5]

Hold[100 - 99]

ReleaseHold[%]

1

•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;

ClearAll[f, x]

f[x_] = x /; ListQ[x]

x /; ListQ[x]

f[x_] = {x} /; Not[ListQ[x]]

{x} /; ! ListQ[x]

f[100]

100 /; ListQ[100]

f[{1, 2, 3}]

{1, 2, 3} /; ListQ[{1, 2, 3}]

Definition with SetDelayed;

Clear[f]

f[x_] := x /; ListQ[x]

f[x_] := {x} /; Not[ListQ[x]]

f[{1, 2, 3}]

{1, 2, 3}

f[5]

{5}

•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)