From: Monty
Subject: sort
Date: 
Message-ID: <7uhvmn$3jp$1@talia.mad.ttd.net>
I'm a begginer usin Golden Common Lisp. My version hasn't the predicate 
(SORT list #'predicateToCompare).

How can I do it?. I'm trying by using the mapcar but it's too much long, and very innefficient because I must go throught the whole list several times.

From: Marc Cavazza
Subject: Re: sort
Date: 
Message-ID: <38187BCD.C5C0C3DA@bradford.ac.uk>
Monty wrote:

> I'm a begginer usin Golden Common Lisp. My version hasn't the predicate
> (SORT list #'predicateToCompare).
>
> How can I do it?. I'm trying by using the mapcar but it's too much long, and very innefficient because I must go throught the whole list several times.

You'll find Lisp code for quicksort in Paul Graham's book

Marc
From: John Watton
Subject: Re: sort
Date: 
Message-ID: <7vaabb$ckh$1@nnrp1.deja.com>
In article <·················@bradford.ac.uk>,
  Marc Cavazza <·········@bradford.ac.uk> wrote:
> Monty wrote:
>
> > I'm a begginer usin Golden Common Lisp. My version hasn't the
predicate
> > (SORT list #'predicateToCompare).
> >
> > How can I do it?. I'm trying by using the mapcar but it's too much
long, and very innefficient because I must go throught the whole list
several times.
>
> You'll find Lisp code for quicksort in Paul Graham's book
>
> Marc
>
>

Graham's quicksort on page 165 of ANSI Common Lisp has a bug. Change the
last two > to >= to get:

(defmacro while (test &rest body)
  `(do ()
       ((not ,test))
     ,@body))

(defun quicksort (vec l r)
  (let ((i l)
        (j r)
        (p (svref vec (round (+ l r) 2))))    ; 1
    (while (<= i j)                           ; 2
      (while (< (svref vec i) p) (incf i))
      (while (> (svref vec j) p) (decf j))
      (when (<= i j)
        (rotatef (svref vec i) (svref vec j))
        (incf i)
        (decf j)))
    (if (>= (- j l) 1) (quicksort vec l j))    ; 3
    (if (>= (- r i) 1) (quicksort vec i r)))
  vec)

--
John Watton
Alcoa Inc.


Sent via Deja.com http://www.deja.com/
Before you buy.