Jump to content

Count Items In Sublists

Recommended Posts

(mapcar 'length '((c4 d4 e4) (f4 g4) (a4) (b4 c5)))


Applies a function (first argument) to each element of a list.


does the same as


(loop for subl in '((c4 d4 e4) (f4 g4) (a4) (b4 c5)) collect (length subl))

Link to comment
Share on other sites

  • 2 weeks later...

I just learned from your  post on another topic that there is a opusmodus-function "get-count". 

mapcar and loop works, but "get-count" is much more flexible for opusmodus.


So as I learned from you,  that is actually the right answer to the topic-question:  get-count .


Just want to mention it so when someone sees the topic will get the right answer and not my previous half answer.


(and there is also mclength to be found under opusmodus functions)

Link to comment
Share on other sites

(get-count '((c4 d4 e4) (f4 g4) (a4) (b4 c5)) ) would be the same as the mapcar or mclength . 

With :length option of get-count you can specify if you only want  to count notes or rests. 

In this case :length :note would give the same result.


There are good examples in the docs of get-count. 

You used it in your pedal related post:

(setf counts-notes (get-count (omn :pitch mat))). 


(get-count '((c4 d4 e4) (f4 g4) (a4) (b4 c5)) )
(mapcar 'length '((c4 d4 e4) (f4 g4) (a4) (b4 c5))).   ;That is basic from a LISP-view without knowing opusmodus.
(mclength '((c4 d4 e4) (f4 g4) (a4) (b4 c5)))

all return => (3 2 1 2)


Have a nice Sunday 


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.

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