Jump to content

Matching length of lists


Recommended Posts

Is there a simple way to repeat a list until it is the length of another list? 

 

Basically do the same thing that make-omn does but for other parameters. 

 

For example if I have a list of numbers '(4 3 2) and pitches '(a4 b4 c4 d4 e4), I would like to repeat the numbers until there are exactly the same number of elements in each list. 

 

Thanks!

Link to comment
Share on other sites

Great! Thank you!!

 

So can you help me with this function? I basically want all three elements to be spanned into the longest of the three. How would you suggest I do this?

 

(defun pattern-maker (chords durations repetitions)
(loop 
  :for i in chords
  :for j in durations
  :for k in repetitions
  :collect (make-omn 
            :length (gen-repeat k j)
            :pitch (gen-repeat k i))))

 

Thanks!

Link to comment
Share on other sites

Ok - 

 

So as a simple example - 

 

(setf chords '(c4e4g4 e4g4c5))

(setf durations '(q e q e s))
                   
(setf repetitions '(7 1 6 2 5 3 4 4))
                   
(length (span  chords repetitions))

(defun pattern-maker (chords durations repetitions)
(loop 
  :for i in chords
  :for j in durations
  :for k in repetitions
  :collect (make-omn 
            :length (gen-repeat k j)
            :pitch (gen-repeat k i))))

 

Would give the following 

 

image.thumb.png.73e59fca162164422aa901588dce0a2d.png

 

I got this by running: 

 

(pattern-maker (span  repetitions chords) 
               (span  repetitions durations)
               repetitions)

 

Which is fine - but if "repetitions" was shorter than "chords" I would need to span everything to chords. 

 

So I guess I need a condition and define a variable that chooses the largest list of the three and span onto that list. I don't know how to do that 🙂

 

Many thanks!

 

Link to comment
Share on other sites

(setf num '(4 3 2 2 3 4 5)) 
(setf pitch '(a4 b4 c4 d4 e4))

(if (< (length num) (length pitch)) pitch num) 
=>(4 3 2 2 3 4 5)
;;;;;;
(setf num '(4 3 2)) 
(setf pitch '(a4 b4 c4 d4 e4))

(if (< (length num) (length pitch)) pitch num) 
=>(a4 b4 c4 d4 e4)

hth

 

Edit: I think you need cond to compare the length of three lists with each other:

(setf durations '(q e q e s))

(setf chords '(c4e4g4 e4g4c5)) 

(setf repetitions '(7 1 6 2 5 3 4 4))

(cond ((> (length chords) (and (length durations) (length repetitions))) chords)
      ((> (length durations) (and (length chords) (length repetitions))) durations)
      ((> (length repetitions) (and (length chords) (length durations))) repetitions))

 

Link to comment
Share on other sites

This seems to work - let me know if there's a more elegant way to do this

 

(defun pattern-maker (chords durations repetitions)
  (let ((longest 

       (cond ((> (length chords) (and (length durations) (length repetitions))) chords)
      ((> (length durations) (and (length chords) (length repetitions))) durations)
      ((> (length repetitions) (and (length chords) (length durations))) repetitions))

))
  (loop 
  :for i in (span  longest chords)
  :for j in (span  longest durations)
  :for k in (span  longest repetitions) 
  :collect (make-omn 
            :length (gen-repeat k j)
            :pitch (gen-repeat k i)))))
 

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