Group Admins

  • Profile picture of Karim
  • Profile picture of Jean


Public Group active 1 day, 6 hours ago

User group for OpenMusic and computer-aided composition. Visit the Forum for discussions.

Lisp bubble sort function

Author 3 Subscribed Users |
David Gerard Matthews
David Gerard Matthews


I’m trying to implement a bubble sort in OM 6.12 using some Lisp code I came across here:

Here’s the code:

(defun bubble-sort (lst)
(loop repeat (1- (length lst)) do
(loop for ls on lst while (rest ls) do
(when (> (first ls) (second ls))
(rotatef (first ls) (second ls)))))

When I evaluate the resulting function in OM, I just get the input list as a result. What am I doing wrong here?

October 4, 2017 at 21:17 #23979
Profile photo of Karim

Dear David,

This is because of the rotatef macro. It works indeed, but the algorithm will sort all your list once.
So i tweaked it in order to have step by step sorting which i guess waht you really wanted. (Included in the zip, lisp file to put in patches + patch)

By the way this is really an amusing algoruthm. THanx for sharing

October 4, 2017 at 23:27 #23980
Profile photo of Jean

Hi David, I think your code is also OK and should work in OM (see attached picture). Do you have a different result ?

  1. Capture-d’écran-2017-10-05-à-11.51.07


  2. Capture-d’écran-2017-10-05-à-11.51.07


October 5, 2017 at 11:00 #23989
David Gerard Matthews
David Gerard Matthews

Hello Jean and Karim,

Karim’s tweaks solved my problem. Jean, the function would evaluate in OM, but (as Karim observed), it would only run once and just return the input list as an output. Thanks!

P.S.: Karim, glad you enjoyed the algorithm!

October 5, 2017 at 14:18 #24001

You must be logged in to reply to this topic.

Log in now