M
Mirko
... faulty code deleted
Aaand one more fix (apply -> funcall) (This version at least produces
a close
facsimile of the desired output)
(defun reduce-tagged (function sequence &key
(key-tag #'first)
(key-datum #'rest))
"Use a binary operation, `function' to combine a sequence of tagged
elements. like-tagged elements are `reduce'd according to `function'
`sequence' is a sequence of tagged elements. reduce-m will reduce
like-tagged-elements.
If `key-tag' is supplied it is used to extract the element tag. If
`key-tag' is not supplied, the function `first' is used.
If `key-datum' is supplied, it is used to extract the element datum.
If `key-datum' is not supplied, the function `rest' is used.
"
(let ((hash (make-hash-table)))
(dolist (datum sequence)
(let ((tag (funcall key-tag datum))
(values (funcall key-datum datum)))
(multiple-value-bind (it present)
(gethash tag hash)
(declare (ignore it))
(if present
(setf (gethash tag hash)
(funcall function (gethash tag hash) values))
(setf (gethash tag hash) values)))))
(let (result)
(maphash #'(lambda(key value)
(push (list key value) result))
hash)
result)))