Category: Uncategorized

  • Exercise 9.7

    twice :: (a -> a) -> a -> a twice f = (\x -> f (f x)) twice twice applies the function 4 times: twice twice f = twice (\y -> f (f y)) = (\x -> (\y -> f (f y)) ((\y -> f (f y)) x)) = (\x -> (\y -> f (f […]

  • Exercise 9.6

    appendr = foldr (flip (++)) [] appendr [x,y,z] = foldr (flip (++)) [] [x,y,z] = flip (++) x (flip (++) y (flip (++) z [])) = flip (++) x (flip (++) y ([] ++ z)) = flip (++) x (([] ++ z) ++ y) = (([] ++ z) ++ y) ++ x Running time of […]

  • Exercise 9.5

    applyAll :: [(a -> a)] -> a -> a applyAll [] x = x applyAll (f:fs) x = f (applyAll fs x)

  • Exercise 9.4

    applyEach :: [(a -> b)] -> a -> [b] applyEach [] _ = [] applyEach (f:fs) x = f x : applyEach fs x

  • Exercise 9.3

    ys :: [Float -> Float]

  • Exercise 9.2

    flip f x y = f y x flip (flip f) x y = flip f y x = f x y ∴ flip (flip f) = f

  • Exercise 9.1

    (Polygon pts) `containsS` p = let leftOfList = map isLeftOfp (zip pts (tail pts ++ [head pts])) isLeftOfp = isLeftOf p in and leftOfList

  • Exercise 8.13

    First, the new definitions: data Region = UnitCircle | Polygon [Coordinate] | AffineTransform Matrix3x3 Region | Empty deriving Show type Vector3 = (Float, Float, Float) type Matrix3x3 = (Vector3, Vector3, Vector3) type Matrix2x2 = ((Float, Float), (Float, Float)) And some old ones that will suffice unchanged: type Coordinate = (Float, Float) type Ray = (Coordinate, […]

  • Exercise 8.12

    There are a couple of ways to do this (the basic problem being how to deal with concave polygons). One way is to convert the polygon to a convex hull and a list of triangles representing subtractions from the convex hull, then check containment within the hull and non-containment within the triangles. However, I present […]

  • Exercise 8.11

    See Exercises 2.4 and 5.1. Then: area” :: Shape -> Float area” (Polygon (v1:vs)) = if convex (Polygon (v1:vs)) then let areas = map ((v2, v3) -> triArea v1 v2 v3) (zip vs (tail vs)) in foldl (+) 0 areas else error “Non-convex polygon”