Ex 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Prof. Dr.

Ralf Hinze TU Kaiserslautern


M.Sc. Sebastian Schloßer
Fachbereich Informatik
AG Programmiersprachen

Functional Programming: Exercise 2


Sheet published: Friday, May 13th
Exercise session: Thursday, May 19th, 12:00

Exercise 2.1 (Skeleton: BinaryTrees.hs). Consider the following data type:

data Tree elem = Empty | Node (Tree elem) elem (Tree elem)

a) Write a function left :: Tree a → Maybe a that returns the left-most element in the
given tree, or Nothing if the tree is empty.
b) Write a function reverseTree :: Tree a → Tree a that returns a tree whose inorder
traversal is the reverse of the original trees’s inorder traversal.
The specification is: inorder (reverseTree t) == reverse (inorder t).
Hint: Solve it in a graphical way first, then write the code.

Exercise 2.2 (Skeleton: Calculus.hs). Lisa Lista’s younger brother just went through
differential calculus at high school. She decides to implement derivatives in Haskell to
be able to easily double-check his homework solutions. To this end she introduces the
following datatype:

infixl 6 :+:
infixl 7 :∗:
infixl 8 :ˆ:
infixl 9 :◦:
data Function
= Const Rational — constant function
| Id — identity
| Function :+: Function — addition of functions
| Function :∗: Function — multiplication of functions
| Function :ˆ: Integer — power with constant exponent
| Function :◦: Function — composition of functions
deriving (Show )

The idea is that each element of Function represents a function over the rationals1 e.g.
Id represents \x → x , Const r represents \x → r , Id :ˆ: (−2) represents \x → x ˆˆ(−2),
and (Id :ˆ: 5) :◦: (Id :+: Const 3) represents \x → (x + 3)ˆˆ5 (which could also be written
as (\x → x ˆˆ5) ◦ (\x → x + 3)). Note that we use (ˆˆ) instead of (ˆ) because the former
allows for negative exponents, where x ˆˆ(−n) == 1 / (x ˆˆn).
In general, if e1 represents f1 and e2 represents f2 , then e1 :+: e2 represents the
function \x → (f1 x ) + (f2 x ), e1 :∗: e2 represents the function \x → (f1 x ) ∗ (f2 x ),
e1 :◦: e2 represents the function \x → f1 (f2 x ), and e1 :ˆ: n represents the function
\x → (f1 x )ˆˆn.

1
The type Rational in Haskell represents rational numbers exactly as the ratio of two Integers.

1
a) Define a function apply :: Function → (Rational → Rational ) that applies the repre-
sentation of a function to a given value. In a sense, apply maps syntax to semantics:
the representation of a function is mapped to the actual Haskell function.
b) Define a function derive :: Function → Function that computes the derivative of a
function. Hint: For the derivation of :ˆ: you probably need to convert an Integer value
to Rational , you can use the function toRational there.
c) After Lisa has captured the rules of derivatives as a Haskell function, she tests the
implementation on a few simple examples. The initial results are not too encouraging:

iiii derive (Const 1 :+: Const 2 :∗: Id )


Const (0 % 1) :+: (Const (0 % 1) :∗: Id :+: Const (2 % 1) :∗: Const (1 % 1))
iiii derive (Id :◦: Id :◦: Id )
(Const (1 % 1) :◦: Id :∗: Const (1 % 1)) :◦: Id :∗: Const (1 % 1)

Implement a function simplify ::Function → Function that simplifies the representation


of a function using the laws of algebra. (This is a lot harder than it sounds!)
Hint: use smart constructors.

You might also like