[Solved] Haskell Zip Parse Error


printList :: IO ()
printList = do putStrLn "Printed Combined List"
    zip [NameList][PriorityList]

There are many things wrong with this code.

The parse error you are seeing is because the do block is not properly aligned. The zip on the last line must line up with the putStrLn on the line before. So either

printList :: IO ()
printList = do putStrLn "Printed Combined List"
               zip [NameList][PriorityList]

or

printList :: IO ()
printList = do
    putStrLn "Printed Combined List"
    zip [NameList][PriorityList]

But that still won’t work. printList is declared to be an IO action, which means the final line of the do block must be an IO action also… but zip produces a list. You may have meant this:

printList :: IO [(String, Int)]
printList = do
    putStrLn "Printed Combined List"
    return (zip [NameList][PriorityList])

but that will only print out the result when you run it directly from the ghci prompt. Better to print it out explicitly:

printList :: IO ()
printList = do
    putStrLn "Printed Combined List"
    print (zip [NameList][PriorityList])

But it still won’t do what you want! Because NameList and PriorityList are, presumably, lists. That you want zipped together. But that’s not what you’re giving to zip: you’re giving zip two new single element lists. You no doubt intended just to pass the lists directly.

printList :: IO ()
printList = do
    putStrLn "Printed Combined List"
    print (zip NameList PriorityList)

Oh, but it still won’t work. Won’t even compile. And why is that? Because variable names must start with lower case letters (or an underscore). And you’ve started both NameList and PriorityList with capital letters. Which is one reason why your first block of code so obviously could not have worked.

printList :: IO ()
printList = do
    putStrLn "Printed Combined List"
    print (zip nameList priorityList)

solved Haskell Zip Parse Error