0

I am attempting to implement the zipWith function via the zip and map functions, but I am getting an error that reads: "error: parse error on input '::' My code is below and and I am unsure of what I have done wrong

zipWith` :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith` f x y = zip x $ map f y
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
Wallace Mogerty
  • 335
  • 1
  • 5
  • 16
  • 2
    The backtick, `\``, is not a valid character for a variable name. Also, why is `enter` there? Is that a typo or do you actually have the word `enter` in your source? It doesn't belong. – Thomas M. DuBuisson Oct 30 '17 at 02:41

2 Answers2

9

You have to use ' symbol and not ` ; then, to combine the function you need to use uncurry:

zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' f xs ys  = map (uncurry f) (zip xs ys)

why is that, well the type of zip is:

zip :: [a] -> [b] -> [(a, b)]

but the function f is f :: (a -> b -> c), so, with the help of uncurry,

uncurry :: (a -> b -> c) -> (a, b) -> c

you can map the function f into the [(a, b)], transforming it into [c].

Will Ness
  • 70,110
  • 9
  • 98
  • 181
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
  • 1
    For maximal inscrutability: `(<$> zip) <$> (<$>) <$> (<$>) <$> uncurry` – dfeuer May 02 '18 at 15:04
  • 1
    I don't know why you edited the question a couple hours ago, but it invalidated the answers (including your own!) so I rolled that back. – dfeuer May 02 '18 at 15:37
  • @dfeuer nice one, inscrutable at all but interesting as well! Thanks for the comments, and you're right also with the roll back – developer_hatch May 02 '18 at 16:51
  • @dfeuer care to explain? Would be amusing to learn. Might also be enlightening. – hyiltiz Dec 31 '19 at 09:43
2

As Damian points out, zipWith` doesn't work with the trailing backtick -- the backtick has a special meaning in Haskell. Rename it to zipWith'.

zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]

Then of course you have to actually write the solution. With explicit recursion you've got

zipWith' _ _ []          = []
zipWith' _ [] _          = []
zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys

but using map and zip you could apply it like this:

zipWith' f xs ys = map (\(x,y) -> f x y) . zip xs $ ys

or more easily-read:

zipWith' f xs ys = map (\(x,y) -> f x y) zipped
    where zipped = zip xs ys
Adam Smith
  • 52,157
  • 12
  • 73
  • 112