我正在尝试使用 zip 和列表理解来编写 zipWith 函数。应用该功能后,我需要压缩这两个列表。但是我不知道在哪里使用列表理解。
我尝试为每个列表做两个列表推导。
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' f xs ys = zip [f x | x <- xs] [f y | y <- ys]
我希望该功能与 zipWith 相同,但是它没有加载并给出错误:
Occurs check: cannot construct the infinite type:
c ~ (b -> c, b -> c)
Expected type: [c]
Actual type: [(b -> c, b -> c)]
• In the expression: zip [f x | x <- xs] [f y | y <- ys]
In an equation for ‘zipWith'’:
zipWith' f xs ys = zip [f x | x <- xs] [f y | y <- ys]
• Relevant bindings include
ys :: [b] (bound at tutorial5.hs:166:15)
f :: a -> b -> c (bound at tutorial5.hs:166:10)
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
(bound at tutorial5.hs:166:1)
|
166 | zipWith' f xs ys = zip [f x | x <- xs] [f y | y <- ys]