1 -- | Implement ordered pairs all over again for fun (and to make sure
2 -- that we can manipulate them algebraically).
10 data TwoTuple a = TwoTuple a a
13 instance Functor TwoTuple where
14 f `fmap` (TwoTuple x1 y1) = TwoTuple (f x1) (f y1)
16 instance (RealFloat a) => Vector (TwoTuple a) where
17 -- The standard Euclidean 2-norm. We need RealFloat for the square
19 norm (TwoTuple x1 y1) = fromRational $ toRational (sqrt(x1^2 + y1^2))
21 -- | It's not correct to use Num here, but I really don't want to have
22 -- to define my own addition and subtraction.
23 instance Num a => Num (TwoTuple a) where
24 -- Standard componentwise addition.
25 (TwoTuple x1 y1) + (TwoTuple x2 y2) =
26 TwoTuple (x1 + x2) (y1 + y2)
28 -- Standard componentwise subtraction.
29 (TwoTuple x1 y1) - (TwoTuple x2 y2) =
30 TwoTuple (x1 - x2) (y1 - y2)
32 -- Left undefined to prevent mistakes. One sane definition
33 -- would be componentwise multiplication.
34 (*) _ _ = error "multiplication of vectors is undefined"
36 abs _ = error "absolute value of vectors is undefined"
38 signum _ = error "signum of vectors is undefined"
40 fromInteger x = TwoTuple (fromInteger x) (fromInteger x)