在haskell中,[1,2,3,4,5,6,7] \\ [4,5,6]将返回[1,2,3,7]。现在我想使用clisp实现相同的函数。到目前为止,我发现set-difference工作正常:
(set-difference '(1 2 3 4 5 6 7) '(4 5 6))有没有其他的解决方案?
发布于 2011-09-19 15:12:21
我不太了解Common Lisp,所以下面是Ben粘贴的代码的Scheme实现:
(define (difference big small)
(fold delete big small))
(define (delete x lst)
(delete-by equal? x lst))
(define (delete-by equal? x lst)
(if (null? lst) '()
(receive (y ys) (car+cdr lst)
(if (equal? x y) ys
(cons y (delete-by equal? x ys))))))其中fold和car+cdr来自SRFI 1,receive来自SRFI 8。
如果我们允许自己使用SRFI 26的cut表单,那么我们就有了一个看起来更接近Haskell版本的解决方案(因为后者至少在两个地方使用了currying ):
(define difference (cut fold delete <...>))
(define delete (cut delete-by equal? <...>))
; Unchanged from the above version
(define (delete-by equal? x lst)
(if (null? lst) '()
(receive (y ys) (car+cdr lst)
(if (equal? x y) ys
(cons y (delete-by equal? x ys))))))https://stackoverflow.com/questions/7467367
复制相似问题