Reputation: 31
i'm new to common lisp and therefore my problem could be very easy, but i didn't find anything, maybe i used the wrong search terms.
i've got the following problem:
i have a function that does a special addition on an arbitrary number of parameters. the next step would be to apply that function to an arbitrary number of lists of same size, the result would be an list of that size.
it works if i call
(mapcar #'addition list1 list2 ...)
but if i have to define a function
(defun list-add (list &rest lists)
(mapcar #'addition list lists))
it won't work, because &rest lists now is a list of lists. the function addition needs to be called with all parameters as sequence, so a recursive call is not possible.
does anyone have a solution?
Upvotes: 3
Views: 732
Reputation: 139401
See APPLY.
Also note the value of CALL-ARGUMENTS-LIMIT.
The obvious solution would be:
(defun list-add (&rest lists)
(apply #'mapcar #'addition lists))
Upvotes: 5
Reputation: 626
I'm not sure if this is necessarily better or worse than the answers already supplied, but here's what I came up with:
(defun list-add (first-required-list &rest other-lists)
(let ((all-lists (cons first-required-list
other-lists)))
(reduce (lambda (left-list right-list)
(mapcar #'addition left-list right-list))
all-lists)))
Upvotes: 0
Reputation: 151
I'm not sure I got the question correctly but try
(defun list-add (list &rest lists)
(mapcar (lambda (l) (apply #'addition list l))
lists))
Upvotes: 0