Haskell邮编解析错误 [英] Haskell Zip Parse Error
问题描述
我试图在Haskell中使用zip函数将两个列表连接在一起。
priority< - getLine $ b $ b let priorityList = []
priority:priorityList
name< - getLine
let nameList = []
name:nameList
收集信息后,预期的输出将是priorityList = [1,2,3]& nameList = [test1,test2,test3]。但是,这对于问题的目的并不重要,可以假定这两个列表的格式如下:
priorityList = [1,2,3]
nameList = [test1,test2,test3]
我需要将列表和打印结合起来使用以下功能。但是,我收到错误输入'zip'的解析错误'
printList :: IO()
printList = do putStrLn打印组合列表
zip [nameList] [priorityList]
printList :: IO()
printList = do putStrLn打印组合列表
zip [NameList] [PriorityList]
这段代码有很多错误。
你看到的解析错误是因为do块没有正确对齐。最后一行中的 zip
必须与之前的行上的 putStrLn
对齐。因此,要么
pre $ printList :: IO()
printList =做putStrLn打印组合列表
zip [NameList] [PriorityList]
或 但是这仍然行不通。 但只会在你直接从ghci提示符运行时打印结果。最好将它打印出来: 不会做你想做的事!因为 哦,但是它仍然无法工作。甚至不会编译。为什么是这样?因为变量名称必须以小写字母(或下划线)开头。你已经用大写字母开始了 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: 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: I need to combine the lists and print with the following function. However, i am getting the error 'parse error on input `zip''
There are many things wrong with this code. The parse error you are seeing is because the do block is not properly aligned. The or But that still won't work. but that will only print out the result when you run it directly from the ghci prompt. Better to print it out explicitly: But it still won't do what you want! Because 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
这篇关于Haskell邮编解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
<$
zip [NameList] [PriorityList]
printList :: IO()
printList = do
putStrLn打印组合列表 code> printList
被声明为IO动作,这意味着do块的最后一行也必须是IO动作...但是 zip code>产生一个列表。你可能意思是这样的:
$ p $ lt; code> printList :: IO [(String,Int)]
printList = do
putStrLn打印组合列表
返回(zip [NameList] [PriorityList])
printList :: IO()
printList = do
putStrLn印刷组合列表
print(zip [NameList] [PriorityList])
NameList
和 PriorityList
可能是列表。你想要压缩在一起。但是,这不是你给的 zip
:你给 zip
两个新的单元素列表。
printList :: IO()
printList = do $ b您无疑打算直接传递列表。 $ b putStrLn打印组合列表
print(zip NameList PriorityList)
NameList
和 PriorityList
。这就是为什么你的第一块代码显然无法工作的原因之一。
printList :: IO()
printList = do
putStrLn打印组合列表
print(zip nameList priorityList)
priority <- getLine
let priorityList = []
priority : priorityList
name<- getLine
let nameList = []
name : nameList
priorityList = [1,2,3]
nameList = [test1, test2, test3]
printList :: IO ()
printList = do putStrLn "Printed Combined List"
zip [nameList][priorityList]
printList :: IO ()
printList = do putStrLn "Printed Combined List"
zip [NameList][PriorityList]
zip
on the last line must line up with the putStrLn
on the line before. So eitherprintList :: IO ()
printList = do putStrLn "Printed Combined List"
zip [NameList][PriorityList]
printList :: IO ()
printList = do
putStrLn "Printed Combined List"
zip [NameList][PriorityList]
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])
printList :: IO ()
printList = do
putStrLn "Printed Combined List"
print (zip [NameList][PriorityList])
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)
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)