Midterm Test Answers

1.

Sqrt[2]

Sqrt[4]

Plus[a, b]

Sin  /@  {0,  Pi}

Times  @@  (i + j)

(a + b*x)[[2, 2]]

x + y  //.  y ->  x

Map[{#[[1]] + I*#[[2]]} &, {{1, 2}, {2, 3}}]

{x, y}  =  {1, 2}^2

?x

?y

Clear[x, y]

{a, b} . {c, d}

a === b

MatchQ[x, x_]

MatchQ[x^2, y_]

Head[h[a]]

Sum[1, {i, 1, 1, 1}]

Set[x, 5]

?x

2.

(a)

f[{x_, y___}]  :=  x

f[{a, b, c}]

f[{a}]

f[{}]

f returns first element of a list.

(b)

g[x_List]  := Fold[(10*#1 + #2) &, 0,  x]

g[{1, 2, 3}]

g[{1, 0, 2, 3, 1}]

g[{3}]

g[{1, 2}]

g[{a, b, c}]  //  Simplify

3.

A two-line code would suffice.

treedepth[{}]  :=  0;
treedepth[t_List]  :=  
      1  +  Max[treedepth[t[[2]]], treedepth[t[[3]]]]

treedepth[{}]

treedepth[{e, {}, {}}]

treedepth[{e, {c, {}, {}}, {}}]

treedepth[{e, {c, {}, {d, {}, {}}}, {}}]