Jump to content

sum-list-items*


Recommended Posts

very simple, i used something like this for my work... but is there somthing like this in OM?

greetings 

andré

 

 

(defun sum-list-items* (somelists &key (each-step nil))
  (let ((lista (car somelists)) (firstlist (car somelists)))
    (progn
      (setf somelists (loop for x in (rest somelists)
                        collect (setf lista (loop 
                                              for i in lista
                                              for j in x
                                              collect (+ i j)))))
      (if (equal each-step t)
        (append (list firstlist) somelists)
        (car (last somelists))))))
  

(sum-list-items* '((1 0 0 1) (1 0 0 0) (0 0 1 1)))
(sum-list-items* '((1 0 0 1) (1 0 0 0) (0 0 1 1)) :each-step t)


(sum-list-items* '((1 0 8 1) (2 0 0 0) (0 -1 3 1)))
(sum-list-items* '((1 0 8 1) (2 0 0 0) (0 -1 3 1)) :each-step t)

 

Link to comment
Share on other sites

(get-count '(a b c d e))
=> 5

Examples:

(setf seql '((2/16 1/16 -1/16) (1/16 2/16 -1/16) (1/4) (1/1)))
(setf seqp '((c4 d4 e4 f4 g4 a4) (bb4 ab4 gb4 e4 db4 c4)))
(setf seqb '((0 1 0 0 1 0 0 1) (1 0 0 1 1 0 0 1)))

The :length keyword, counts how many length notes or length rests are in a sequence.

(get-count seql :length :note)
=> (2 2 1 1)

The sum of all length-notes in the sequence:

(get-count seql :length :note :sum t)
=> 6

The ordinary count:

(get-count seqp)
=> (6 6)

The item keyword, counts a specific item in the sequence:

(get-count seql :item 1/4)
=> (0 0 1 0)

(get-count seqp :item 'bb4)
=> (0 1)

(get-count seqp :sum t)
=> 12

(get-count seqb)
=> (8 8)

(get-count seqb :item 1 :sum t)
=> 7

 

Link to comment
Share on other sites

new version, for LISTS with different-lengths => compensated

 

;;; SUB

(defun compensate-list-lengths (somelists &key (value 0))
  (let ((maxlength (find-max (mapcar 'length somelists))))
    (loop for i in somelists
      when (< (length i) maxlength)
      collect (append i (loop repeat (- maxlength (length i))
                          collect value))
      else collect i)))

                     
;;; MAIN 
                     
(defun sum-list-items (somelists &key (each-step nil))
  (let ((somelists (compensate-list-lengths  somelists))
        (lista (car somelists)) (firstlist (car somelists)))
    (progn
      (setf somelists (loop for x in (rest somelists)
                        collect (setf lista (loop 
                                              for i in lista
                                              for j in x
                                              collect (+ i j)))))
      (if (equal each-step t)
        (append (list firstlist) somelists)
        (car (last somelists))))))

;;;
                     
(sum-list-items '((1 0 4 4 4 4 4 4 0 1) (81 0 0 0) (0 0 1 1 99 200)))
=> (82 0 5 5 103 204 4 4 0 1)
        
                     

 

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