Category: Uncategorized

  • Exercise 2.3

    Relevant definitions: area :: Shape -> Float area (Rectangle s1 s2) = s1 * s2 area (Polygon (v1 : vs) = polyArea vs where polyArea (v2 : v3 : vs’) = triArea v1 v2 v3 + polyArea (v3 : vs’) polyArea _ = 0 triArea :: Vertex -> Vertex -> Vertex -> Float triArea v1 […]

  • Exercise 2.2

    There are several ways to solve this problem. I used the approach of rotating around the origin incrementing the angle by 2Ï€/n each time. The radius of the figure is given by application of the sine rule to the isosceles triangle formed by two adjacent vertices and the origin. type Side = Float {- Construct […]

  • Exercise 2.1

    data Shape = … | Polygon [Vertex] … type Vertex = (Float, Float) rectangle :: Float -> Float -> Shape rectangle s1 s2 = Polygon [(x, y), (-x, y), (-x, -y), (x, -y)] where x = s1 / 2 y = s2 / 2 rtTriangle :: Float -> Float -> Shape rtTriangle s1 s2 = […]

  • Exercise 1.3

    simple x y z = x * (y + z) From what we have learnt so far: [(2, 3), (4, 5)] :: [(Integer, Integer)] [‘z’, 42] :: {- Error: different types cannot be mixed in a list -} (‘z’, -42) :: (Char, Integer) simple ‘a’ ‘b’ ‘c’ :: {- Error: simple cannot be called with […]

  • Exercise 1.2

    simple x y z = x * (y + z) simple (a – b) a b => (a – b) * (a + b) => (a * a) + (a * b) – (b * a) – (b * b) {- distributive law -} => (a * a) – (b * b) => a2 – […]

  • Exercise 1.1

    simple x y z = x * (y + z) simple (simple 2 3 4) 5 6 => simple (2 * (3 + 4)) 5 6 => simple (2 * 7) 5 6 => simple 14 5 6 => 14 * (5 + 6) => 14 * 11 => 154