The linear library exports an instance of Num a => Num (V3 a) , so you can just do
> point * 2 V3 2 4 6
If you use GHCi, you can see what this means for 2 :: V3 Int :
> 2 :: V3 Int V3 2 2 2
So the fromInteger implementation for V3 will look like
fromInteger n = V3 n' n' n' where n' = fromInteger n
That means you can do things like
> point + 2 V3 3 4 5 > point - 2 V3 (-1) 0 1 > abs point V3 1 2 3 > signum point V3 1 1 1 > negate point V3 (-1) (-2) (-3)
V3 also implements Fractional , so you should be able to use / and co. when your point contains Fractional values. However, using fmap is more general, you can convert your V3 Int to V3 String , for example:
> fmap show point V3 "1" "2" "3"
The fmap function allows you to apply a function of the type a -> b to V3 a to get V3 b without any restrictions on the type of output (necessarily so). Using fmap not wrong; it is just not as readable as using ordinary arithmetic operators. Most Haskellers have no reading problems, but fmap is a very general tool that appears for almost every type.
source share