Chapter 5
Evaluation of Expressions

from
Introduction to Programming with Mathematica,
2nd Ed.

by
Richard J. Gaylord
Samuel N. Kamin
Paul R. Wellin

Copyright 1995 TELOS/Springer-Verlag

5.2 Creating Rewrite Rules

Code from book

rand1[x_] := Random[Real, x]

rand2 = Random[]

?Apply

?rand1

?rand2

Exercises

Ex.1a

randLis1[n_] := Table[Random[], {n}]

?randLis1

Ex.1b

randLis2[n_] := (x = Random[]; Table[x, {n}])

?randLis2

Ex.1c

randLis3[n_] := (x := Random[]; Table[x, {n}])

?randLis3

Ex.1d

randLis4[n_] := Table[Random[], {n}]

?randLis4

5.3 Expressions

Code from book

FullForm[x + 2 y + z^2]

Part[{a, b, c, d, e}, 2]

Part[{a, b, c, d, e}, -2]

Part[x + 2 y + z^2, 3]

Part[x + 2 y + z^2, -3]

Part[a^3, 0]

Head[{a, b}]

Append[w + x y,  z]

FullForm[w + x y]

FullForm[w + x y + z]

FullForm[Apply[Power, Plus[a, b]]]

Map[Head, {a, List, "give me a break"} ]

Map[Head, {2, 5.201, 3 + 4I, 5/7}]

FullForm[{darwin, "read my lips", 1, 3 + 4I, 5/7}]

"read my lips"[[1]]

Part[3 + 4I, 1]

Exercises

Ex.1a

((x^2 + y) z / w)[[2, 1, 2]]

FullForm[((x^2 + y) z / w)]

Ex.1b

(a/b)[[2, 2]]

FullForm[(a/b)]

Ex.1c

FullForm[(x^2 + b x^2 + c x^2)]

5.4 Patterns

Code from book

MatchQ[x^2, x^2]

MatchQ[x^2, x^_Integer]

MatchQ[x^2, x^_Real]

MatchQ[x^2, x^_]

MatchQ[x^2, x^y_]

MatchQ[x^2, _Symbol^2]

MatchQ[x^2, _List^2]

MatchQ[x^2, _Symbol^_Integer]

MatchQ[x^2, _Symbol^_]

MatchQ[x^2, _^2]

MatchQ[x^2, _^_Integer]

MatchQ[x^2, _^_]

MatchQ[x^2, _Power]

MatchQ[x^2, _]

MatchQ[x^2, __]

MatchQ[x^2, ___]

MatchQ[{a, b, c}, __]

MatchQ[{a, b, c}, {___}]

MatchQ[{a, b, c}, x__]

MatchQ[{a, b, c}, {x___}]

MatchQ[{a, b, c}, _?ListQ]

MatchQ[{a, b, c}, _?NumberQ]

MatchQ[{a, b, c}, _List?(Length[#] >2&)]

MatchQ[{a, b, c}, _List?(Length[#] > 4&)]

MatchQ[x^2, _^y_ /; EvenQ[y]]

MatchQ[x^2, _^y_ /; OddQ[y]]

MatchQ[{4, 6, 8}, x_ /; Length[x] > 4]

MatchQ[{4, 6, 8}, {x___} /; Length[x] > 4]

MatchQ[x^2, x^_Real | x^_Integer]

MatchQ[x^2, x^(_Real | _Integer)]

Exercises

Ex.1

FullForm[x^3 + y z]

MatchQ[(x^3 + y z), _Plus]

MatchQ[(x^3 + y z), _Power + _Times]

Ex.2

FullForm[{5, erina, "give me a break"}]

MatchQ[{5, erina, "give me a break"}, _List]

MatchQ[{5, erina, "give me a break"}, 
       {_Integer, _Symbol, _String}]

Ex.3

FullForm[{4, {a, b}, "g"}]

MatchQ[{4, {a, b}, "g"}, x_List /; Length[x] == 3]

MatchQ[{4, {a, b}, "g"}, _List?(Length[#] == 3&)]

MatchQ[{4, {a, b}, "g"}, {_, y_, _} /; y[[0]] == List]

MatchQ[{4, {a, b}, "g"}, {x_, y_, z_} /; AtomQ[z]]

MatchQ[{4, {a, b}, "g"}, {x_, _, _} /; EvenQ[x]]

5.5 Term Rewriting

Code from book

square[x_?OddQ] := x^2

square[3]

Trace[square[3], TraceOriginal->True]

Exercises

Ex.1

Trace[ReleaseHold[Thread[Hold[MatchQ[{1,0,0,1,1}, 
                                     {0,1,0,1,0}]
     ]]]]

Trace[Apply[Times, Thread[Plus[{1, 2}, {3, 4}]], 2]]

Trace[ReleaseHold[Apply[Times,
   Thread[Hold[Plus][{1, 2}, {3, 4}]], 2]]]

5.6 Transformation Rules

Code from book

Table[x, {5}] /. x -> Random[]

Trace[Table[x, {5}] /. x -> Random[]]

Table[x, {5}] /. x :> Random[]

Trace[Table[x, {5}] /. x :> Random[]]

{a, b, c} /. List -> Plus

{{3, 4}, {7, 2}, {1, 5}} /. {x_, y_} -> {y, x}

{a, b, c} /. {c -> b, b -> a}

{a, b, c} //. {c -> b, b -> a}

maxima1[x_List] := Union[Rest[FoldList[Max, 0, x]]]

maxima1[{3, 5, 2, 6, 1, 8, 4, 9, 7}]

maxima2[x_List] := 
   x //. {a___, b_, c___, d_, e___} /; d <= b :> {a, b, c, e}

maxima2[{3, 5, 2, 6, 1, 8, 4, 9, 7}]

Exercises

Ex.1

maxima1[x_] := Union[Rest[FoldList[Max, -Infinity, x]]]

maxima2[x_List] :=
    x//.{a___, b_, c___, d_, e___} /;
         d <= b :> {a, b, c, e}

Trace[maxima1[{3, 5, 2, 6, 1, 8, 4, 9, 7}]]

Trace[maxima2[{3, 5, 2, 6, 1, 8, 4, 9, 7}]]

Ex.2

Trace[y = 11; a = 9; (y + 3 /. y -> a)]

Ex.3

Hold[y = 11]; a = 9; (y + 3 /. y -> a)

Ex.4

Trace[g[x_] = x /. Plus[z___] -> Times[z]]

Clear[a,g]

g[x_] := x /. Literal[Plus[z___]] :> Times[z]

g[a + b + c]

Ex.5

unNest[lis_] := Map[(# //.{x__List}->x)&, lis]

unNest[{{a, a, a}, {a}, {{b, b, b}, {b, b}}, {a, a}}]

Ex.6

sum[lis_] := First[lis //. {x_, y___} -> x + {y}]

sum[{1, 5, 8, 3, 9, 3}]