----------------------------- 1. Given these definitions: a = b + 3 b = 7 f x = b * x + x - 3 reduce this expression: f (2 * a) ----------------- f (2 * a) = f (2 * (b + 3)) = f (2 * (7 + 3)) = f (2 * 10) = f 20 = b * 20 + 20 - 3 = b * 20 + 17 = 7 * 20 + 20 - 3 = 140 + 20 - 3 = 160 - 3 = 157 ----------------------------- 2. Given these definitions: x = 3 * y y = 7 f x = x + 2 z = x + f x where x = y + f 3 reduce this expression: z * x - 2 ----------------- z * x - 2 = (x + f x where x = y + f 3) * x - 2 = (y + f 3 + f (y + f 3)) * x - 2 = (7 + (3 + 2) + f (7 + f 3)) * x - 2 = (7 + 5 + f (7 + f 3)) * x - 2 = (12 + f (7 + f 3)) * x - 2 = (12 + f (7 + 3 + 2)) * x - 2 = (12 + f 12) * x - 2 = (12 + (12 + 2)) * x - 2 = (12 + 14) * x - 2 = 26 * x - 2 = 26 * 3 * y - 2 = 26 * 3 * 7 - 2 = 546 - 2 = 544 ----------------------------- 3. Given these definitions: k = True h x = (x && k) || not x m x = if x < 5 then h False else h (k || x==1) reduce this expression: m 5 ----------------- m 5 = if 5 < 5 then h False else h (k || 5==1) = h (k || 5==1) = h (k || False) = h (True || False) = h True = (True && k) || not True = (True && True) || False = True || False = True ----------------------------- 4. Given these definitions: p = g 5 q = g (p - 4) g x = if x > 3 then x+2 else g (x-5) reduce this expression: (p * 2) - q ----------------- (p * 2) - q = (g 5 * 2) - q = ((if 5 > 3 then 5+2 else g (5-5)) * 2) - q = ((if True then 5+2 else g (5-5)) * 2) - q = ((5+2) * 2) - q = (7 * 2) - q = 14 - q = 14 - g (p - 4) = 14 - g (g 5 - 4) = 14 - g (7 - 4) { remembering g 5 => 7 from above } = 14 - g 3 = 14 - (if 3 > 3 then 3+2 else g (3-5)) = 14 - (if False then 3+2 else g (3-5)) = 14 - g (3-5) = 14 - g (-2) = 14 - if -2 > 3 then -2+2 else g (-2-5) = 14 - if False then -2+2 else g (-2-5) = 14 - g (-2-5) = 14 - g (-7) = ... never terminates ... { since x in g x continually decreases } ----------------------------- 5. Given these definitions: zap f x = if x < 20 then f (x + 3) else f (zap f x) w y = y * 2 - 1 r = 5 reduce this expression: zap w r ----------------- zap w r = if r < 20 then w (r + 3) else w (zap w r) = if 5 < 20 then w (r + 3) else w (zap w r) = if True then w (r + 3) else w (zap w r) = w (r + 3) = w (5 + 3) = w 8 = 8 * 2 - 1 = 16 - 1 = 15 -----------------------------