]> gitweb.michael.orlitzky.com - numerical-analysis.git/blob - src/TwoTuple.hs
Begin work on a simple 2-tuple type.
[numerical-analysis.git] / src / TwoTuple.hs
1 -- | Implement ordered pairs all over again for fun (and to make sure
2 -- that we can manipulate them algebraically).
3 --
4 module TwoTuple
5 where
6
7 data TwoTuple a = TwoTuple a a
8 deriving (Eq, Show)
9
10 instance Functor TwoTuple where
11 f `fmap` (TwoTuple x1 y1) = TwoTuple (f x1) (f y1)
12
13
14 instance Num a => Num (TwoTuple a) where
15 -- Standard componentwise addition.
16 (TwoTuple x1 y1) + (TwoTuple x2 y2) =
17 TwoTuple (x1 + x2) (y1 + y2)
18
19 -- Standard componentwise subtraction.
20 (TwoTuple x1 y1) - (TwoTuple x2 y2) =
21 TwoTuple (x1 - x2) (y1 - y2)
22
23 -- Left undefined to prevent mistakes. One sane definition
24 -- would be componentwise multiplication.
25 (*) _ _ = error "multiplication of vectors is undefined"
26
27 abs _ = error "absolute value of vectors is undefined"
28
29 signum _ = error "signum of vectors is undefined"
30
31 fromInteger x = TwoTuple (fromInteger x) (fromInteger x)