I am trying to write zipWith function using zip and list comprehension. I need to zip the two lists after the function is applied. However I don't know where to use the list comprehension.
I tried doing two list comprehensions for each list.
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' f xs ys = zip [f x | x <- xs] [f y | y <- ys]
I am expecting the function to be identical to zipWith, however it is not loading and giving error:
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]