如何编写一个接受两个列表并返回四个列表的方案函数

发布于 2024-07-17 03:19:04 字数 296 浏览 5 评论 0原文

我有 2 个元素列表 '(abc) '(dbf),并且想要在一个结果中查找差异、并集和交集。 那可能吗? 如何?

我编写了一个成员函数来检查第二个列表中是否存在第一个列表中的汽车,但我无法将成员扔到新列表中。

(define (checkResult lis1 lis2)
  (cond...........

))
(checkresult '( a b c) '(d b f))

我的结果应该是 (( ac) (df) (abcdf) (b))

I have 2 lists of elements '(a b c) '(d b f) and want to find differences, union, and intersection in one result. Is that possible? How?

I wrote a member function that checks if there is a car of the first list in the second list, but I can't throw a member to the new list.

(define (checkResult lis1 lis2)
  (cond...........

))
(checkresult '( a b c) '(d b f))

My result should be (( a c) (d f) (a b c d f) (b)).

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

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

发布评论

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

评论(4

久随 2024-07-24 03:19:04

就像其他人所说的那样,您需要做的就是创建单独的函数来计算两个集合的交集、并集和减法,并从 checkresult 中调用它们:

(define (checkresult a b)
  (list (subtract a b)
        (subtract b a)
        (union a b)
        (intersect a b)))

以下是一些示例并集、交集和减法函数:

(define (element? x lst)
  (cond ((null? lst) #f)
        ((eq? x (car lst)) #t)
        (#t (element? x (cdr lst)))))

(define (union a b)
  (cond ((null? b) a)
        ((element? (car b) a)
         (union a (cdr b)))
        (#t (union (cons (car b) a) (cdr b)))))

(define (intersect a b)
  (if (null? a) '()
      (let ((included (element? (car a) b)))
        (if (null? (cdr a))
            (if included a '())
            (if included
                (cons (car a) (intersect (cdr a) b))
                (intersect (cdr a) b))))))

(define (subtract a b)
  (cond ((null? a) '())
        ((element? (car a) b)
         (subtract (cdr a) b))
        (#t (cons (car a) (subtract (cdr a) b)))))

注意:因为这些是集合,顺序无关紧要,结果未排序。 此外,这些函数假设输入是集合,因此不会执行超出联合所需的任何重复检查。

Like others have said, all you need to do is create separate functions to compute the intersection, union, and subtraction of the two sets, and call them from checkresult:

(define (checkresult a b)
  (list (subtract a b)
        (subtract b a)
        (union a b)
        (intersect a b)))

Here are some example union, intersection, and subtraction functions:

(define (element? x lst)
  (cond ((null? lst) #f)
        ((eq? x (car lst)) #t)
        (#t (element? x (cdr lst)))))

(define (union a b)
  (cond ((null? b) a)
        ((element? (car b) a)
         (union a (cdr b)))
        (#t (union (cons (car b) a) (cdr b)))))

(define (intersect a b)
  (if (null? a) '()
      (let ((included (element? (car a) b)))
        (if (null? (cdr a))
            (if included a '())
            (if included
                (cons (car a) (intersect (cdr a) b))
                (intersect (cdr a) b))))))

(define (subtract a b)
  (cond ((null? a) '())
        ((element? (car a) b)
         (subtract (cdr a) b))
        (#t (cons (car a) (subtract (cdr a) b)))))

Note: since these are sets and order doesn't matter, the results are not sorted. Also, the functions assume that the inputs are sets, and therefore don't do any duplicate checking beyond what's required for union.

吃→可爱长大的 2024-07-24 03:19:04

当然有可能。 假设您有计算差异、并集交集等的函数:

 (define (checkResult lis1 list2)
   (list (difference lis1 lis2)
        (union ...

Sure it is possible. Assuming that you have function to compute the differences, union intersection etc:

 (define (checkResult lis1 list2)
   (list (difference lis1 lis2)
        (union ...
居里长安 2024-07-24 03:19:04

当然有可能。 这里有一些提示:

  1. 组合列表和空列表的结果是什么?
  2. 您不必一次完成所有操作。 一次取一块。

Sure it's possible. Here are a couple hints:

  1. what's the result of combining a list and an empty list?
  2. You don't have to do it all at once. Take a piece at a time.
自演自醉 2024-07-24 03:19:04

除了 Charlie Martin 和 tomjen 的答案之外,我还提出了以下来源:

并集交集和差分

可以找到不同函数的实现以及很好的解释。

On top of Charlie Martin's and tomjen's answers, I have come up with this source:

Union Intersection and Difference

Implementation of the distinct functions can be found with nice explanations.

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