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 3


Sheet published: Monday, May 23rd
Exercise session: Thursday, June 2nd, 12:00

Exercise 3.1 (Skeleton: Map.hs). We want to represent mappings from type String to
type a using the following interface:

type Map a = String → Maybe a


empty :: Map a
insert :: (String, a) → Map a → Map a
lookup :: String → Map a → Maybe a

Each value of type Map a represents such a mapping. Note that those values are functions.
empty represents the empty map. insert adds a key-value-pair to a map, potentially
overwriting existing mappings for the same key. lookup looks up a key (String) in the map
and returns the value it maps to – or Nothing if the key does not exist in the map.

a) Implement empty, insert and lookup. Verify that your implementa-


tion of insert prioritizes the latest inserted item, e.g. check that
lookup "foo" (insert ("foo", 42) (insert ("foo", 4711) empty)) evaluates to
Just 42.

empty = \ → Nothing
insert (s, a) m = \x → if x == s then Just a else m x
lookup x m = m x

b) Maps can also be represented by lists of type [(String, a)]. In fact, the Haskell Prelude
provides a lookup function of type String → [(String, a)] → Maybe a (the actual type
of that library function is more general, supporting any Eq type instead of String).
Note that the lookup function from Prelude has been hidden in our template file such
that we can implement our own function having that name, but you can still access it
as Prelude.lookup.
Implement a function fromList :: [(String, a)] → Map a that transforms a list into the
corresponding map. Make sure that for all such lists xs and Strings s the result of
lookup s (fromList xs) and Prelude.lookup s xs are the same.
What happens when you evaluate lookup "foo" (fromList (repeat ("foo", 42)))?

fromList :: [(String, a)] → Map a


fromList = foldr insert empty

1
B = (a, −(a + c), c) C = (a, b, −(a + b))

The oriented height of the triangle is given by the sum of its coordinates: h = a + b√
+ c, see
Figure 2 for an appealing visual proof. (The length of the sides is consequently 2/ 3 · h ≈
1.1547 · h.)
Exercise 3.2. The position (relatively to the origin of a coordinate system) and size
type Triangle
of equilateral triangles= (Integer,
of whichInteger,
one Integer)
edge is horizontal can be represented using three
numbers. height :: Triangle
We will use the Integer
→ following type:
height (a, b, c) = a + b + c
type
As Triangle = (Integer
an example, , Integer
the triangle in the , middle )
Integerbelow is represented by (2, 3, 5) and has
height 10. As all three coordinates are positive, the origin lies in the interior of the triangle.
For example, the triangle below in the middle is represented by (2, 5, 3). The ones on the
The other two examples demonstrate that the coordinates may well be negative and that we
left and right are represented by (2, −2, 3) and (2, −8, 3).
can represent both upward and downward pointing triangles.

3
3

2
b
c a
b
c

−2
2

3 −8
a 5

c
b
2

a
Observe that the three triangles differ only in one coordinate. In general, if we move one
Noteside
that
of the origintowards
a triangle of thethecoordinate system
opposite vertex, does not
the height need
of the to be
triangle inside When
decreases. the triangle
and that
thethe three
height numbers
becomes
Functional zero, can
pearls be negative.
the triangle has degenerated to a 5point. If we keep moving, the
height turns
Implement negative and
a function we obtain
height a downward
:: Triangle pointing
→ Integer triangle.
that calculates the height of such a
The oriented height of an equilateralQuite
triangle equals the sum
appealingly, of
the(a, its coordinates,
triangular the oriented distances
coordinate
triangle. Given
of the sidelines to the origin. For non-negative
ameasures
triangle this fact
b,
is
c), look
known as
at thesystem
Viviani’s
allows(a,
triangles
theorem, of
after
for0,a 0),
uniform
(0, b,representation
as 0) and (0, 0, c)
of both triangles and points. In particular, the vertices the triangle 4(a, b, c) are A =
pictured below in yellow, orange and
Vincenzo Viviani (1622–1703). For the proof, consider the triangle 4(a, b, c). red:
(−(b + c), b, c), B = (a, −(a + c), c), and C = (a, b, −(a + b)).
type Point = (Integer, Integer, Integer)
data ABC = A | B | C -- names of triangle sides and vertices

c
b
a

Observe that each coordinate defines a triangle anchored in the origin: 4(a, 0, 0) drawn in yellow,
4(0, b, 0) drawn in orange, and 4(0, 0, c) drawn in red. The small triangles can be lined up within
the large triangle by shifting the height :: Triangle
yellow one → Integer
to the right and the red one to the top: 4(a, 0, 0) becomes
4(a, b, −b) and 4(0, 0, c) becomes height (a,b,b,c).c)Clearly,
4(−b, = a +theb sides
+ c of the small triangles add up to
the side of the large triangle and the same holds for the heights.
Fig. 2. Visual proof of Viviani’s theorem.

vertex :: ABC → Triangle → Point


vertex A ( , b, c) = (−(b + c), b, c)
vertex B (a, , c) = (a, −(a + c), c)
vertex C (a, b, ) = (a, b, −(a + b))
side :: ABC → Triangle → Pair Point
side A t = pair (vertex B t) (vertex C t)
side B t = pair (vertex C t) (vertex A t)
side C t = pair (vertex A t) (vertex B t)
For the center we decrease each coordinate by one third of the height.
center :: Triangle → Point -- assumes that height is a multiple of 3
center t@(a, b, c) = (a − x, b − x, c − x) where x = height t div 3 2

Exercise 1 Q: How are the six equilateral unit triangles around the origin represented?
A: 4(1, 0, 0), 4(−1, 0, 0), 4(0, 1, 0), etc.

You might also like