Jump to content

Setdiff of two lists


Recommended Posts

Beginner question.

I intend to get from c dorian scale all possible triads except those with pitch classes 0 1.

 

(setf scale (expand-tonality  '(c4 dorian)))
(setf scale-pcs (get-pcs scale))
(setf all-triads (pcs-sub-sets 3 scale-pcs))
(setf avoid (pcs-super-sets 3 '(0 1)))

 

Basically I was looking for a setdiff function for both lists all-triads and avoid.

 

Is there a function in OM for that?

 

I tried myself, but my lisp knowledge is still insufficient to do the job.

Any help?

 

Link to comment
Share on other sites

Correct 🙂 the set-difference function computes the difference between two sets represented as lists. It returns a new list containing elements from the first list (list1) that are not present in the second list (list2). This function is part of the Common Lisp standard and should behave consistently across compliant implementations.

Link to comment
Share on other sites

There is code (credited to Janusz) in Julio’s new OM book that achieves a similar goal. in the book, the goal is to get all the sets of cardinality 4 that don’t contain a (0 1 2) cluster as a subset. Interesting to see a different approach here. This version seems much simpler.

Link to comment
Share on other sites

Yes, indeed very interesting !

 

Here is Janusz´s code you mentioned:

 

(let ((out (pcs-cardinal 4 :forte)))
; search the supersets of Forte 3-1 (chromatic cluster)
  (loop for i in (pcs-super-sets 4 (pcs '3-1) :forte)
;remove supersets of Forte 3-1 (chromatic cluster)
    do (setf out (remove i out))
;filtered result
    finally (return out)))

 

Here is the same, with set-difference

 

(reverse (set-difference (pcs-cardinal 4 :forte) (pcs-super-sets 4 (pcs '3-1) :forte)))

 

All the best !

Julio

Link to comment
Share on other sites

This code demonstrates how to use the loop macro, an essential tool for many types of data manipulation.
 

(let ((out (pcs-cardinal 4 :forte)))
  (loop for i in (pcs-super-sets 4 (pcs '3-1) :forte)
        do (setf out (remove i out))
        finally (return out)))

 

Understanding it will enable you to solve a wide range of conditional manipulations in the future.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Terms of Use Privacy Policy