Professional Documents
Culture Documents
Ex 3
Ex 3
Exercise 3.1 (Skeleton: Map.hs). We want to represent mappings from type String to
type a using the following interface:
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.
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 yellow one 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 4(−b, b, c). Clearly, the sides 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.
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.