在Scheme中向列表添加元素

发布于 2024-08-26 15:39:45 字数 486 浏览 8 评论 0原文

我正在使用 R5RS 方案,我只想实现一个返回两个给定列表的交集的函数,但我不能这样做,因为我无法将元素添加到列表中。这是我的代码。我该如何修复它?我真的是Scheme的初学者——这是我使用Scheme的第一份工作。

提前谢谢..

(define list3 '())
(define (E7 list1 list2)

        (cond
          ((null? list1)
          list3)
          ((member (car list1) list2) (append list3 (list (car list1))))

        )
  (cond
          ((null? list1)
          list3)
          ((not(null? list1)) (E7 (cdr list1) list2)

        )

     )


)
(E7 '(4 5) '(3 4))

I'm using R5RS Scheme and I just want to implement a function that returns the intersection of two given lists, but I can't do that because I cannot add an element to a list. Here is my code. How can I fix it? I'm really a beginner in Scheme - this is my first work using Scheme.

thx in advance..

(define list3 '())
(define (E7 list1 list2)

        (cond
          ((null? list1)
          list3)
          ((member (car list1) list2) (append list3 (list (car list1))))

        )
  (cond
          ((null? list1)
          list3)
          ((not(null? list1)) (E7 (cdr list1) list2)

        )

     )


)
(E7 '(4 5) '(3 4))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

风吹短裙飘 2024-09-02 15:39:45

这是一个递归版本,它执行交集而不是并集。

(define (intersect list1 list2)
  (cond ((null? list1)   list1)
        ((member (car list1) list2)   (cons (car list1) (intersect (cdr list1) list2)))
        (t   (intersect (cdr list1) list2))))

Here is a recursive version that does the intersection instead of the union.

(define (intersect list1 list2)
  (cond ((null? list1)   list1)
        ((member (car list1) list2)   (cons (car list1) (intersect (cdr list1) list2)))
        (t   (intersect (cdr list1) list2))))
春夜浅 2024-09-02 15:39:45

我想我看到了你的问题。有两种方法可以将元素添加到列表中。

第一种方法是实际添加它:(

(define (intersect list1 list2)
  (define newlist list2)
  (do ((iter1 list1 (cdr iter1)))
    (not (null? iter1))
    (if (not (member (car iter1) newlist))
        (set! newlist (cons (car iter1) newlist)))))

如果您确实想使用它,您可能必须查找 do 的定义。)

您可能会注意到这非常难看。那是因为没有人真正这样做。相反,您必须意识到调用函数也会创建一个新变量。试试这个:

(define (intersect list1 list2)
  (cond ((null? list1) list2)
        ((member (car list1) list2) (intersect (cdr list1) list2))
        (else (intersect (cdr list1) (cons (car list1) list2)))))

如果您熟悉算法,您会注意到我刚刚编写的代码非常慢,但它说明了这一点:在每种情况下,您都会做一些工作,然后再次调用您的函数。如果您无法理解其工作原理,请在您的示例上运行此函数:

(define (intersect list1 list2)
  (display list1) (display " ") (display list2) (newline)
  (cond ((null? list1) list2)
        ((member (car list1) list2) (intersect (cdr list1) list2))
        (else (intersect (cdr list1) (cons (car list1) list2)))))

I think I see your problem. There are two ways to add an element to a list.

The first way would be actually adding it:

(define (intersect list1 list2)
  (define newlist list2)
  (do ((iter1 list1 (cdr iter1)))
    (not (null? iter1))
    (if (not (member (car iter1) newlist))
        (set! newlist (cons (car iter1) newlist)))))

(You'll probably have to look up the definition of do if you really want to use this.)

You may notice that that's quite ugly. That's because no one actually does it this way. Instead, you have to realize that calling a function creates a new variable as well. Try this:

(define (intersect list1 list2)
  (cond ((null? list1) list2)
        ((member (car list1) list2) (intersect (cdr list1) list2))
        (else (intersect (cdr list1) (cons (car list1) list2)))))

If you're familiar with algorithms, you'll notice that the code I just wrote is quite slow, but it illustrates the point: in each case, you do a little bit of work and then call your function again. If you're having trouble seeing why this works, run this function instead on your example:

(define (intersect list1 list2)
  (display list1) (display " ") (display list2) (newline)
  (cond ((null? list1) list2)
        ((member (car list1) list2) (intersect (cdr list1) list2))
        (else (intersect (cdr list1) (cons (car list1) list2)))))
忘羡 2024-09-02 15:39:45

这是一些简单的 elisp:

(defun is (l1 l2)
  (let ((rtn))
    (mapc
      (lambda (e) 
        (if (member e l1)
          (push e rtn)))
      l2)
    rtn))

这与这些简单测试的内置交集的行为相同:

(is '(1 2 5) '(1 4 10 5))  => (5 1)
(intersection '(1 2 5) '(1 4 10 5)) => (5 1)

(is '(1 4 10 5) '(1 2 5)) => (5 1)
(intersection '(1 4 10 5) '(1 2 5)) => (5 1)

Here is some simplistic elisp:

(defun is (l1 l2)
  (let ((rtn))
    (mapc
      (lambda (e) 
        (if (member e l1)
          (push e rtn)))
      l2)
    rtn))

This behaves the same as the built-in intersection for these simple tests:

(is '(1 2 5) '(1 4 10 5))  => (5 1)
(intersection '(1 2 5) '(1 4 10 5)) => (5 1)

(is '(1 4 10 5) '(1 2 5)) => (5 1)
(intersection '(1 4 10 5) '(1 2 5)) => (5 1)
淡淡の花香 2024-09-02 15:39:45

您最好使用 srfi-1 中的设置操作

You're better off using set operations from srfi-1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文