You are on page 1of 2

Prof.aa Dr. J. Giesl Notes: Please solve these exercises in groups of two!

Functional Programming SS12 Exercise Sheet 8 (due 20.06.2012) M. Brockschmidt, F. Emmes

The solutions must be handed in directly before (very latest: at the beginning of ) the exercise course on Wednesday, 20.06.2012, 11:45, in lecture hall AH 2. Alternatively you can drop your solutions into a box which is located right next to Prof. Giesls oce (until the exercise course starts). Please write the names and immatriculation numbers of all (two) students on your solution. Also please staple the individual sheets! Exercises marked with V3B are only relevant for students attending the V3B (Bachelor Informatik, Master Mathematik) version of the lecture. Unmarked exercises are relevant for all students.

Exercise 1 (Semantics of Simple Haskell):

(3 + 4 + 4 = 11 points)

Give the value of V al ei for the following simple Haskell expressions e1, e2, e3 for the environment = + and (x) = 2, (y ) = 42. Describe your computation in detail, i.e., do not apply more than one of the rules dening V al to one subterm in one step of your solution. For each higher-order function f : Dom Dom that occurs in the calculation, determine what the function f i (), i N, computes. e1 = let isAnswer = \ x -> if x == 42 then 1 else 0 in isAnswer y e2 = let sum = \ x -> if x > 0 then x + sum (x -1) else 0 in sum x e3 = let mult = \ x -> \ y -> if y <= 0 then 0 else x + mult x (y -1) in mult 3 x

Exercise 2 (V3B Transformation to Simple Haskell):

The data structure Nat is dened by data Nat = Z | S Nat

(8 points)

Transform the following Haskell-expression to an equivalent simple Haskell-expression using the transformation rules (1)-(9) from the lecture. Please give all intermediate expressions and indicate in each step which transformation rule was used. You may apply rules to several identical subterms simultaneously. let isEven Z = True isEven (S Z) = False isEven (S (S n)) = isEven n in isEven (S (S (S x))) Please simplify the intermediate expressions according to the following rules: (i) if (isa0tuple exp) then exp1 else exp2 should be replaced by exp1. (ii) (\var->exp1) exp should be replaced by exp1, where exp1 results from exp1 by replacing all (free) occurrences of var by exp.

Functional Programming SS12 Exercise Sheet 8 (due 20.06.2012) (iii) The following simplications should be done in expressions of the form if exp then exp1 else exp2: Every sub-expression if exp then exp1 else exp2 in exp1 should be replaced by exp1. Every sub-expression if exp then exp1 else exp2 in exp2 should be replaced by exp2.