列表中不是Incommon的输出元素

时间:2015-04-08 18:45:46

标签: scheme

我创建了一个函数,它应该返回两个列表没有共同点的元素。目前,他们正在输出传递给它的确切内容。有关如何解决此问题的任何建议吗?

(define (findDifference lst1 lst2)
    (if (null? lst1) lst2
    (cons (car lst1) (findDifference (cdr lst1) lst2))))

(findDifference '(2 3 4 (2 3) 2 (4 5)) '(2 4 (4 5))

当前输出:(2 3 4 (2 3) 2 (4 5) 2 4 (4 5)) 期望的输出:(3 (2 3))

2 个答案:

答案 0 :(得分:2)

您要求两个列表中的symmetric difference。试试这个:

(define (diff list1 list2)
  (union (complement list1 list2)
         (complement list2 list1)))

使用以下帮助程序:

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

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

另请注意,如果您使用的是Racket,则可以使用内置的set-symmetric-difference程序获得相同的效果。例如:

(diff '(2 3 4 (2 3) 2 (4 5)) '(2 4 (4 5)))
=> '(3 (2 3))

答案 1 :(得分:1)

因为看起来像家庭作业而且我不想破坏乐趣,所以这里是蛮力算法,遗漏了一些东西。如果你真的被困住了,我会给你全部资源。

(define (sym-diff xs ys)
  ;; Since we have the helper function we can determine all the elements that are in the first list, 
  ;; but not in the second list.
  ;; Then we can pass this intermediate result to the second call to sym-diff-helper. 
  ;;This will return us all the elements that are in the second list but not the first.
  (let ((in-first-not-second ...))
    (sym-diff-helper ys xs in-first-not-second)))

;; This function will return all the elements from the first list that are not in the second list!
(define (sym-diff-helper xs ys acc)
  (cond
    ;; If the first list is empty we have checked it.
    (...
     acc)
    ;; If the first list is not empty yet, check if the first element 
    ;; is in the second list.
    ;; If so, discard it and continue with the rest of the list.
    ((member ... ...)
     (sym-diff-helper ... ... ...)
    ;; If the first element of the first list is not in the second list, 
    ;; add it to the accumulator and continue with the rest of the list.
    (else
     (sym-diff-helper ... ... ...)))

(sym-diff-helper '(1 2 3) '(2 3 4) '())
;; == (1)
(sym-diff-helper '(1 2 (3 4) 5) '(2 3 4) '())  
;; == (5 (3 4) 1)


(sym-diff '(2 3 4 (2 3) 2 (4 5)) '(2 4 (4 5)))
;; == ((2 3) 3)

请注意,我选择使用member。还有一些其他搜索功能,但在这种情况下它们并不适合。因此,我把它留在那里。有关搜索功能的更多信息,请访问:http://docs.racket-lang.org/reference/pairs.html#%28part..List.Searching%29