newtype Vector2 a = Vector2 (a,a)
deriving (Show,Eq)
instance VectorSpace Vector2 where
vecMagnitude (Vector2 (a,b)) = (a**2 + b**2)**(1/2)
vecF :: (Floating a, VectorSpace v) => v a -> [v a] -> [a]
vecF Vector2 (a,b) ys = [ vecMagnitude ( Vector2 (a+(-1)*a'),(b+(-1)*b') ) |(a',b') <- ys ]
So the above coding I have errors with is the vecF function, see the following error msg :
Couldn't match type ‘v’ with ‘(,) a0’
‘v’ is a rigid type variable bound by
the type signature for:
vecF :: forall a (v :: * -> *).
(Floating a, VectorSpace v) =>
v a -> [v a] -> [a]
at Assign_2_EC_test.hs:74:1-58
Expected type: v a
Actual type: (a0, a)
In the pattern: (a, b)
In an equation for ‘vecF’:
vecF (a, b) ys
= [vecMagnitude ((a + (- 1) * a'), (b + (- 1) * b')) |
(a', b') <- ys]
Relevant bindings include
vecF :: v a -> [v a] -> [a]
Any idea what was the error
There are basically four problems here: two can be solved easilyk, the others will require to change the signature, or implementing extra functions:
you forgot to write brackets in the head of the function:
vecF ys = ...
the brackets in the "yield" part of the list comprehension do not have brackets to make a tuple:
[ vecMagnitude (Vector2 ) | ... ]
in your signature you allow the first parameter to be any va
with v
a VectorSpace
type, and a
a Floating
type, so you can not just add a constructor like:
vecF = ...
(can not be solved in a straightforward way) since ys
is a list of va
s, you can not just enumerate from ys
as:
[ ... | ]
The last two problems can not be solved easily, we can specialize the signature such that it works, like:
vecF :: Floating a => a -> [] -> [a]
vecF (Vector2 (a,b)) ys = [ vecMagnitude ( Vector2 (a-a',b-b')) |(a',b') <- ys ]
but then we of course loose the flexibitily to use other VectorSpace
s, another option is for example to move vecF
to the class
definition and implement it in the instance
clause. Finally another option might be to make extra functions such that you do not need pattern matching to calculate the vecMaginute
, you can for example implement a function:
class VectorSpace v where
-- ...
vecDiff :: Num a => v a -> v a -> v a
and then you can implement this as:
vecF :: (Floating a, VectorSpace v) => v a -> [v a] -> [a]
vecF v0 vs = [ vecMagnitude (vecDiff v0 v1) | v1 <- vs ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.