Haskell Lists - zip 2

I want to write a function that concatenates two lists. I have the following code:

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

      

The problem is that when I compile the code, I get a lot of errors

Could not match expected type [b] -> t0 to actual type a

The xs function is applied to one argument

Any ideas what I am doing wrong? Sorry if this might seem like a silly question.

+3


source to share


2 answers


You need to add (x,y)

to the return value:

zip' (x:xs) (y:ys) = (x,y):zip' xs ys

      



Otherwise it doesn't make sense syntactically since you don't cons

(:) when x, y you matched the return value.

Fiddle

+11


source


Any ideas what I am doing wrong?

Code

(x,y) zip'(xs ys)

      



means: take a function (x,y)

and apply it to two arguments, the first one is zip'

and the second is the result of the function being applied xs

to ys

.

Since it is (x,y)

not a function, this generates a type error. Also, xs

it is not a function, so another type of error will be thrown by its application.

+7


source







All Articles