[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

set-difference



The MCL 2.0 set-difference function reverses the order of list1.

set-difference list1 list2 &key :test :test-not :key   [Function]

According to CLTL-2, pg 430, there is no guarantee on the order of
elements Common Lisp implementations will return. Lucid's Common
Lisp does not reverse the order.

i.e.

(set-difference '(a b c d e) '(d e))

returns (C B A) for MCL 2.0 & (A B C) for Lucid.

No surprise. I attempted to redefine set-difference within a
newly created package shadowing set-difference inherited from
Common Lisp. No luck.

;;;***************************************

(in-package :user)

(eval-when (eval compile load)
  (defpackage new-package (:shadowing-import-from "COMMON-LISP" set-difference))
  (in-package :new-package))

(defun set-difference (list1 list2 &key (test #'eq) test-not key)
  (reverse (COMMON-LISP:set-difference list1 list2
           :test test :test-not test-not :key key)))

(set-difference '(a b c d e) '(d e))

;;;****************************************

The listener's response.

;Loading #P"Hard Disk:MCL 2.0:set-difference.lisp"...
> Error: The function SET-DIFFERENCE is already defined in the CCL kernel.
> While executing: CCL::REDEFINE-KERNEL-FUNCTION
> Type Command-/ to continue, Command-. to abort.
> If continued: Replace the definition of SET-DIFFERENCE.
See the RestartsI menu item for further choices.
1 > 

;;;****************************************

Have I done something dumb? 

My workaround has been to define set_difference:

(defun set_difference (list1 list2 &key (test #'eq) test-not key)
  (reverse (COMMON-LISP:set-difference list1 list2
           :test test :test-not test-not :key key)))

but this seems to lack esthetic appeal.

Any suggestions?

Thanks, Sheldon