计划作业问题,需要帮助

发布于 2024-08-27 01:03:13 字数 332 浏览 6 评论 0原文

好吧,我的作业问题之一是获取一个列表列表并将每个子列表的汽车作为列表返回。我把它放在可以打印值的地方,但它不是一个列表。老实说,我不知道如何输出列表。这就是我得到的:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒废 2024-09-03 01:03:13

我的计划有点生疏了。我手头只有一个 dr-scheme 解释器......

您需要使用您的列表并仅返回每个子列表的第一个元素。

要创建列表,您必须使用 cons 指令,并且不需要输出它,您需要将其作为函数结果返回(但您应该已经知道它)。

如果您需要打印结果,您应该将计算与输出分开,并在第二步中解决打印问题。

为了构建列表,您有几个构造,最基本的是缺点,它需要一个通用元素(也是一个列表)并将其添加到一个列表(也是空元素)

(cons 1) => error: cons need two parameters
(cons 1 null) => (list 1)
(cons 1 2) => error: need a value (first parameter) and a list (the second one)
(cons 1 (cons 2 null) => (list 1 2)

现在开始你的作业。通常我不会发布作业代码,但是这次我认为您距离解决方案只有一个提示,因此

(define (car-list alist)
  (cond
    ((null? alist) null)
    (else (cons (car(car alist)) (car-list (cdr alist))))
  )
)
; tail recursion version usage: (car-acc-list alist null)
(define (car-acc-list alist acc)
  (cond
    ((null? alist) acc)
    (else (car-acc-list (cdr alist) (cons (car(car alist)) acc)))
  )
)

我可能使用 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)

(cons 1) => error: cons need two parameters
(cons 1 null) => (list 1)
(cons 1 2) => error: need a value (first parameter) and a list (the second one)
(cons 1 (cons 2 null) => (list 1 2)

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

(define (car-list alist)
  (cond
    ((null? alist) null)
    (else (cons (car(car alist)) (car-list (cdr alist))))
  )
)
; tail recursion version usage: (car-acc-list alist null)
(define (car-acc-list alist acc)
  (cond
    ((null? alist) acc)
    (else (car-acc-list (cdr alist) (cons (car(car alist)) acc)))
  )
)

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.

酷炫老祖宗 2024-09-03 01:03:13

您应该使用 map 函数:

> (define lst '((1 2 3) (4 5 6) (7 8 9)))
;Value: lst
> (map car lst)
;Value: (1 4 7)

You should use map function:

> (define lst '((1 2 3) (4 5 6) (7 8 9)))
;Value: lst
> (map car lst)
;Value: (1 4 7)
海未深 2024-09-03 01:03:13

要设计一个功能,您需要知道您的工具是什么。对于列表,您需要:

cons : n l -> (n l)
Construct a list out of an element and a list, placing the element n
at the head of the list

car : l -> n
Return the head of the list

cdr : l -> l
Return the tail of the list

您想要编写一个函数 firstl 来使用列表列表并返回一个列表。一些例子是:

(firstl (list (list 1 2 3) (list 9 2 3) (list 4 7 3))) -> (1 9 4)
(firstl (list (list 1 2 3))) -> (1)
(firstl '()) -> ()

最后一个例子应该给你第一个线索:如果参数是一个空列表,则返回一个空列表。

(define (firstl lol)     ; list-of-lists (no laughing!)
  (if (null? lol)
    '()
    ....    ; more stuff goes here for the recursive "else" clause. Hint: use cons
))

To design a function, you need to know what your tools are. For lists, you have:

cons : n l -> (n l)
Construct a list out of an element and a list, placing the element n
at the head of the list

car : l -> n
Return the head of the list

cdr : l -> l
Return the tail of the list

You want to write a function firstl that consumes a list of lists and returns a list. Some examples are:

(firstl (list (list 1 2 3) (list 9 2 3) (list 4 7 3))) -> (1 9 4)
(firstl (list (list 1 2 3))) -> (1)
(firstl '()) -> ()

The last example should give you your first clue: if the argument is an empty list, return an empty list.

(define (firstl lol)     ; list-of-lists (no laughing!)
  (if (null? lol)
    '()
    ....    ; more stuff goes here for the recursive "else" clause. Hint: use cons
))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文