-1

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
Damian Lattenero
  • 15,181
  • 3
  • 34
  • 70
Wallace Mogerty
  • 333
  • 1
  • 4
  • 15
  • 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
  • 69,019
  • 8
  • 93
  • 175
Damian Lattenero
  • 15,181
  • 3
  • 34
  • 70
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
  • 48,602
  • 11
  • 68
  • 105