【发布时间】:2019-04-05 22:08:51
【问题描述】:
如果我从 GHCi 终端执行 myZipWith (+) (Point [1,2,3]) (Point [4,5,6]) 一切正常,但如果我尝试从一个简单的函数执行它,则会出现错误。
为什么即使代码相同,直接从终端执行却不一样?
这是我的代码:
data Point p = Point [p]
deriving (Show)
wtf :: [p]
wtf = myZipWith (+) (Point [1,2,3]) (Point [4,5,6])
myZip :: Point p -> Point p -> [(p, p)]
myZip (Point []) _ = []
myZip _ (Point []) = []
myZip (Point (a:as)) (Point (b:bs)) = (a, b) : myZip (Point as) (Point bs)
myZipWith :: (p -> p -> p) -> Point p -> Point p -> [p]
myZipWith f (Point p1) (Point p2) = [ f (fst x) (snd x) | x <- (myZip (Point p1) (Point p2)) ]
错误代码:
No instance for (Num p) arising from a use of `+'
Possible fix:
add (Num p) to the context of
the type signature for:
wtf :: forall p. [p]
* In the first argument of `myZipWith', namely `(+)'
In the expression:
myZipWith (+) (Point [1, 2, 3]) (Point [4, 5, 6])
In an equation for `wtf':
wtf = myZipWith (+) (Point [1, 2, 3]) (Point [4, 5, 6])
【问题讨论】: