Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!usc!apple!sun-barr!newstop!sun!basselope!shiffman From: shiffman%basselope@Sun.COM (Hank Shiffman) Newsgroups: comp.lang.lisp Subject: Re: what does "destructively sorted" mean in KCL? Message-ID: <119605@sun.Eng.Sun.COM> Date: 4 Aug 89 20:08:47 GMT References: <3916@phri.UUCP> Sender: news@sun.Eng.Sun.COM Reply-To: shiffman@sun.UUCP (Hank Shiffman) Organization: Sun Microsystems, Mountain View Lines: 45 In article <3916@phri.UUCP> roy@phri.UUCP (Roy Smith) writes: > I'm trying to figure out how the sort function works in KCL, but I >think I'm getting hung up on what "Common Lisp: The Language" means when it >says in section 14.5: "The sequence is destructively sorted". I took >"destructively sorted" to mean "sorted in-place", but it looks like it >really means "trashed, but a sorted copy is returned". For example: > >>(setq foo '(1 3 9 4 -2 0 9)) >(1 3 9 4 -2 0 9) > >>foo >(1 3 9 4 -2 0 9) > >>(sort foo #'>) >(9 9 4 3 1 0 -2) > >>foo >(1 0 -2) > > Sort returns a sorted list as its value, but the argument gets >trashed. Is this really the way it's supposed to work? Do I have to do >(setq foo (sort foo #'>)) to get it to work? Yes, this is exactly the way it's supposed to work. Notice that in your example, FOO points to a CONS whose CAR is 1 both before and after the sort. When CLtl says that a function on a list operates destructively, it means that the CONS cells of the original list may be reused. FOO still points to the CONS cell which has a CAR of 1. It's just not the first CONS cell in the sorted list. Keep in mind that since SORT is a function, (sort foo #'>) causes sort to be handled the *value of FOO*, not the symbol itself. So SORT (as it's currently defined) couldn't put sorted list back into FOO even if it wanted to. -- Hank Shiffman (415) 336-4658 Marketing Technical Specialist Software Engineering Technologies ...!sun!shiffman Sun Microsystems, Inc. shiffman@Sun.com Zippy sez: Gee, I feel kind of LIGHT in the head now, knowing I can't make my satellite dish PAYMENTS!