简体   繁体   English

如何保存跨不同函数的元组列表

[英]How can I save a list of tuples across different functions

I want to write a file that has information about when I can schedule an exam, the date(given a limit of days for all the exams) and which room(given a limited number of rooms) but the exam subject is to be defined according to a file which has all the, number id of the subjects, the year of the subjects and the name of the subjects, all in separate lines, and there can't be more than one exam in the same room.我想写一个文件,其中包含有关何时可以安排考试、日期(给定所有考试的天数限制)和哪个房间(给定房间数量有限)的信息,但考试主题将根据到一个包含所有科目编号 ID、科目年份和科目名称的文件,所有这些都在不同的行中,并且在同一个房间里不能有超过一个考试。

I would like store those 3 values for each subject in a list of tuple so when I print it out I just get a list of tuples.我想将每个主题的这 3 个值存储在元组列表中,所以当我打印出来时,我只得到一个元组列表。 I'm trying to tackle this in Haskell and so far I got to this point but, I can't seem to understand the errors this code is giving me.我试图在 Haskell 中解决这个问题,到目前为止我已经到了这一点,但是,我似乎无法理解这段代码给我的错误。

Sorry for the dumb question, kind of new to Haskell, also the variables and function names are not in English.抱歉这个愚蠢的问题,Haskell 的新问题,变量和 function 名称也不是英文的。

     atribuituple dias sala (li:lis) tupleList = do
                    if dias > 0 then
                         atribuituplesala dias sala last(words li) tupleList
                         atribuituple (dias-1) sala lis tupleList
                    else
                         return()
                    
      
     atribuituplesala dias sala uc tupleList = do
                    if  sala > 0 then
                         tupleList ++ (dias,sala,uc)
                         atribuituplesala dias (sala-1) uc tupleList
                    else
                         return()

     save_uc_dia dias sala = do
               ucs <- openFile "ucs.txt" ReadMode
               exames_uc_dia_sala_W <- openFile "exames_uc_dia_sala.txt" WriteMode
               let tupla = [] 
               atribuituple dias sala (lines ucs) tupla
               writeFile "exames_uc_dia_sala.txt" tupla
               hClose ucs
               hClose exames_uc_dia_sala_W

I was expecting to receive a list of tuples in a file called "exames_uc_dia_sala.txt" according to all the limitations the initial problem has given me.根据初始问题给我的所有限制,我期望在名为“exames_uc_dia_sala.txt”的文件中收到一个元组列表。

Imagine you're writing a Haskell program to create a list of integers counting from some starting integer n down to 1 .假设您正在编写一个 Haskell 程序来创建一个从 integer n开始计数到1的整数列表。 If you wrote it in a style similar to your atribuituplesala function, it would look something like this:如果您以类似于您的atribuituplesala function 的风格编写它,它将看起来像这样:

countdown n lst = do
  if n > 0 then
    lst ++ [n]
    countdown (n-1) lst
  else
    return ()

But there's a pretty big problem with this solution: as Haskell code, it doesn't make any sense.但是这个解决方案有一个很大的问题:作为 Haskell 代码,它没有任何意义。 Nothing about it is correct.没有什么是正确的。

The expression lst ++ [n] doesn't append n to lst .表达式lst ++ [n]不会将 append n转换为lst Placing two expressions on separate lines after the " then " keyword:在“ then ”关键字之后将两个表达式放在不同的行上:

 lst ++ [n]
 countdown (n-1) lst

does not "run" these expressions sequentially.不会按顺序“运行”这些表达式。 The final statement return () does not "stop processing" and return the final list.最终语句return ()并没有“停止处理”并返回最终列表。

Most of your assumptions about how Haskell works are wrong, and none of this code will work as you intend.您对 Haskell 如何工作的大部分假设都是错误的,并且这些代码都不会按您的预期工作。

(If it helps to understand where I'm coming from, and assuming you know a little Python, imagine a friend comes to you with their Python code to add up the numbers from 1 to 100, and presents you with something like this: (如果这有助于理解我的出发点,并且假设您对 Python 略有了解,请想象一位朋友带着他们的 Python 代码来找您,将 1 到 100 的数字相加,并向您展示如下内容:

x = 1
result = 0
if x < 100:
    x + 1
    sum(result,x)
    pass
else:
    return result

Even though you can sort of understand their thought process from this code, there's really no way to help them fix it, except by rewriting it for them from scratch.尽管您可以从这段代码中了解他们的思维过程,但实际上没有办法帮助他们修复它,除非从头开始为他们重写。 It's pretty obvious your friend hasn't learned even the absolute basics of Python -- how variable assignments, loops, and functions work -- and they absolutely must start working through some beginner tutorials and learning materials before tackling even a simple problem like this.)很明显,您的朋友甚至没有学习 Python 的绝对基础知识——变量赋值、循环和函数如何工作——他们绝对必须开始学习一些初学者教程和学习材料,然后才能解决像这样的简单问题。 )

So, in that spirit, I don't think you've yet learned the absolute basics of Haskell. You should work through some tutorials and learning materials before tackling a problem like this.因此,本着这种精神,我认为您还没有学习 Haskell 的绝对基础知识。在解决此类问题之前,您应该学习一些教程和学习材料。

The answers to this SO question provide some pointers to good resources. 这个 SO 问题的答案提供了一些指向良好资源的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM