简体   繁体   English

在Scheme中的列表中添加元素

[英]Adding an element to a list in Scheme

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. 我正在使用R5RS Scheme,我只想实现一个函数,该函数返回两个给定列表的交集,但是我不能这样做,因为我无法向列表中添加元素。 Here is my code. 这是我的代码。 How can I fix it? 我该如何解决? I'm really a beginner in Scheme - this is my first work using Scheme. 我真的是Scheme的初学者-这是我第一次使用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))

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

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)

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.) (如果您确实想使用它, do可能必须查询do的定义。)

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

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

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

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