user2159471
user2159471

Reputation: 333

Writing zip in terms of zipWith

I am learning Haskell through Chris Allen and Julie Moronuki's Haskell Programming book. Need help on one of the exercises for Zipping list. The exercise has 3 parts, first two are about writing our own implementation of zip and zipWith which i was able to.

myzip1 :: [a] -> [b] -> [(a,b)]
myzip1 _ [] = []
myzip1 [] _ = []
myzip1 (x:xs) (y:ys) = (x,y) : myzip1 xs ys

myZipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
myZipWith _ _ [] = []
myZipWith _ [] _ = []
myZipWith f (x:xs) (y:ys) = (f x y) : myZipWith f xs ys

The last part asks to write zip in terms of zipWith. I am not able to come up with a solution. Honestly, i am not able to grasp the question completely (writing one function in terms of second, when function application of second is not required as it has all arguments first would require) . Thanks for the help.

Upvotes: 1

Views: 758

Answers (1)

Cirdec
Cirdec

Reputation: 24156

The only difference between myzip1 and myZipWith is

myzip1      (x:xs) (y:ys) =   (x,y) : myzip1      xs ys
myZipWith f (x:xs) (y:ys) = (f x y) : myZipWith f xs ys

Perhaps myzip1 and myZipWith f are the same for some f.

myzip1 = myZipWith f

What would f be so that

(f x y) = (x, y)

Upvotes: 8

Related Questions