计划作业问题,需要帮助
好吧,我的作业问题之一是获取一个列表列表并将每个子列表的汽车作为列表返回。我把它放在可以打印值的地方,但它不是一个列表。老实说,我不知道如何输出列表。这就是我得到的:
(define (car-print alist)
(if (null? alist)
(newline)
(begin
(write (car (car alist))) (display " ")
(car-print(cdr alist)))))
这是一种廉价的方法,也许对实际解决这个问题的一些帮助将不胜感激。不一定是完整的答案,而是实现目标的步骤。谢谢。
Okay one of my homework problems is to take a list of lists and return the car of each sublist as a list. I have it to where I can print out the values, but it's not a list. To be honest, I have no clue how to output lists. Here's what I got:
(define (car-print alist)
(if (null? alist)
(newline)
(begin
(write (car (car alist))) (display " ")
(car-print(cdr alist)))))
This is a cheap way to do it, perhaps some help on actually solving this problem would be much appreciated. Not necessarily the full answer but steps to get there. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的计划有点生疏了。我手头只有一个 dr-scheme 解释器......
您需要使用您的列表并仅返回每个子列表的第一个元素。
要创建列表,您必须使用 cons 指令,并且不需要输出它,您需要将其作为函数结果返回(但您应该已经知道它)。
如果您需要打印结果,您应该将计算与输出分开,并在第二步中解决打印问题。
为了构建列表,您有几个构造,最基本的是缺点,它需要一个通用元素(也是一个列表)并将其添加到一个列表(也是空元素)
现在开始你的作业。通常我不会发布作业代码,但是这次我认为您距离解决方案只有一个提示,因此
我可能使用 cond,而不是 if 因为我认为它可以更好地格式化您的代码。
他的结构很简单:一个子句列表,其中测试条件(或 else)为 car,如果条件满足则执行的操作为 cdr。
如果操作解析为值((null? alist) null),则函数将返回该值作为返回值。如果触发递归,则函数在递归完成时返回。
该解决方案有两个版本,您应该使用步进器/调试器来研究它们的差异。
顺便说一句,我使用 drscheme 来测试代码,它是一个很棒的免费(lgpl)软件。与其他方案环境存在细微差别,但代码应该是非常基本的代码,因此它应该在任何地方运行都不会出现问题。
My Scheme is a bit rusty. And I have just a dr-scheme interpreter at hand...
You need to consume your list and return just the first element of every sublist.
To create a list you have to use the cons directive, and you don't need to output it, you need to return it as function result (but you should already know it).
If you need to print the result you should separate the computation from the output and resolve the printing issues in a second step.
For building a list you have several construct, the most basic is cons, it takes a generic element (also a list) and prepend it to a list (the null one, also)
Now to your homework. Usually I don't post homework code but, this time I think you are just an hint away from the solution, so there is a possible one
I used cond, instead of if 'cause I think it permits a nicer formatting of your code.
His structure is simple: a list of clauses, with the test condition (or else) as car and action to perform if the condition is satisfied as cdr.
If action resolve to a value ((null? alist) null) the function return with that value as return value. If recursion is triggered then the function return when the recursion finish.
There are two version of the solution, you should use the stepper/debugger to investigate their differences.
By the way I used drscheme to test the code, it is a wonderful piece of free (lgpl) software. Minor differences are with other scheme environments but the code should be a very basic one and therefore it should run without issues everywhere.
您应该使用
map
函数:You should use
map
function:要设计一个功能,您需要知道您的工具是什么。对于列表,您需要:
您想要编写一个函数 firstl 来使用列表列表并返回一个列表。一些例子是:
最后一个例子应该给你第一个线索:如果参数是一个空列表,则返回一个空列表。
To design a function, you need to know what your tools are. For lists, you have:
You want to write a function firstl that consumes a list of lists and returns a list. Some examples are:
The last example should give you your first clue: if the argument is an empty list, return an empty list.