Haskell邮编解析错误 [英] Haskell Zip Parse Error

查看:152
本文介绍了Haskell邮编解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在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]



<$
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])

但只会在你直接从ghci提示符运行时打印结果。最好将它打印出来:

  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)


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]

解决方案

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)

这篇关于Haskell邮编解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆