# 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)```

##### 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```

##### Share on other sites

i think a different idea!?

in my code:

sum first item of all lists

sum second...

sum third...

...

but perhaps i'm wrong

##### 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)

```

## Join the conversation

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

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×
×

• Lessons