Haskell Zip Parse Error

时间秒杀一切 提交于 2019-12-27 04:48:31

问题


I am trying to use the zip function in Haskell to join two lists together. The lists could be defined and info gathered as follows:

 priority <- getLine    
 let priorityList = []
 priority : priorityList

 name<- getLine
 let nameList = []
 name : nameList

After gathering the info, the expected output would be priorityList = [1,2,3] & nameList = [test1, test2, test3]. However, this is unimportant for the purpose of the question, it can be assumed that the two lists are in the following format:

  priorityList = [1,2,3]
  nameList = [test1, test2, test3]

I need to combine the lists and print with the following function. However, i am getting the error 'parse error on input `zip''

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

回答1:


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)



回答2:


Using the above assistance, i have been able to construct a function (below) to achieve a successful ZIP (Excluding information gathering).

printList :: IO ()
printList = do
    let nameList = ["test1", "test2", "test3"]
    let prioirtyList = [1, 2, 3]
    putStrLn "Printed Combined List"
    print (zip nameList prioirtyList)

My output is as follows:

*Main> printList
 Printed Combined List
 [("test1",1),("test2",2),("test3",3)]


来源:https://stackoverflow.com/questions/15635012/haskell-zip-parse-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!