#### How do I resolve this error in Haskell coding with vector

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 ]

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]

1 个回复

1. 您忘记在函数的开头写括号：

vecF (Vector2 (a,b)) ys = ...
2. 列表理解的“收益”部分中的方括号没有构成元组的方括号：

[ vecMagnitude (Vector2 (a+(-1)*a', b+(-1)*b') ) | ... ]
3. 在您的签名您允许的第一个参数是任何vav一个VectorSpace类型， a一个Floating类型，所以你不能只是增加一个构造函数，如：

vecF (Vector2 (a, b)) = ...
4. （不能直接解决）因为ysva的列表，所以不能仅从ys枚举为：

[ ... | (a', b') <- ys ]

vecF :: Floating a => Vector2 a -> [(a, a)] -> [a]
vecF (Vector2 (a,b)) ys = [ vecMagnitude ( Vector2 (a-a',b-b')) |(a',b') <- ys ]

class VectorSpace v where
-- ...
vecDiff :: Num a => v a -> v a -> v a

vecF :: (Floating a, VectorSpace v) => v a -> [v a] -> [a]
vecF v0 vs = [ vecMagnitude (vecDiff v0 v1) | v1 <- vs ]
1 如何解决此编码错误

2014-05-13 13:56:17 2 125   vhdl
3 如何解决此错误？

2013-01-12 16:49:02 3 83   php

2020-01-03 12:42:05 1 56   types

2012-06-02 23:22:07 1 1399   unboxing
7 如何解决Java越界编码错误？

9 如何通过编码解决这个二项式方程？

2020-09-24 15:10:25 2 46   python