Carlos Romero
Carlos Romero

Reputation: 181

Trying to understand zip

I am attempting to understand how to use zip in Haskell. I've been learning Haskell recently and am trying to create a list of tuples from two separate lists

I have the following:

createList :: [Char] -> [Char] -> [(Char,Char)]    
createList xs ys = zip(xs,ys)

I understand zip is supposed to create a list of tuples given two lists, but I get the following error:

Couldn't match expected type ‘[a0]’
              with actual type ‘([Char], [Char])’

Can anyone explain to me where I am stumbling?

Upvotes: 0

Views: 65

Answers (2)

muhuk
muhuk

Reputation: 16085

If you remove the parenthesis around zip call, your code should work:

createList :: [Char] -> [Char] -> [(Char,Char)]

createList xs ys = zip xs ys

Explanation:

Full error I am getting when I run zip ([1, 2, 3], [4, 5, 6]) (notice the parens):

<interactive>:4:5:
    Couldn't match expected type ‘[a]’
                with actual type ‘([Integer], [Integer])’
    Relevant bindings include
      it :: [b] -> [(a, b)] (bound at <interactive>:4:1)
    In the first argument of ‘zip’, namely ‘([1, 2, 3], [4, 5, 6])’
    In the expression: zip ([1, 2, 3], [4, 5, 6])
    In an equation for ‘it’: it = zip ([1, 2, 3], [4, 5, 6])

Notice the part that says In the first argument of ‘zip’, namely ‘([1, 2, 3], [4, 5, 6])’. The parens are interpreted as tuple constructor. zip function expects a list as its first argument but we are passing it a tuple.

Upvotes: 3

Mark Seemann
Mark Seemann

Reputation: 233150

Haskell function calls don't use brackets or commas.

You can write the createList function as:

createList xs ys = zip xs ys

or simply

createList = zip

Thus, the createList function is redundant; it's just zip. The only potential use for the alias that I can think of is if you truly want to constrain the type as given.

Upvotes: 5

Related Questions