Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common Lisp -- List unpacking? (similar to Python)

In Python, assuming the following function is defined:

def function(a, b, c):
    ... do stuff with a, b, c ...

I am able to use the function using Python's sequence unpacking:

arguments = (1, 2, 3)
function(*arguments)

Does similar functionality exist in Common Lisp? So that if I have a function:

(defun function (a b c)
    ... do stuff with a, b, c ...

And if I have a list of 3 elements, I could easily use those 3 elements as parameters to the function?

The way I currently implement it is the following:

(destructuring-bind (a b c) (1 2 3)
    (function a b c))

Is there a better way?

like image 988
brildum Avatar asked Dec 15 '10 15:12

brildum


2 Answers

Use the apply function:

(apply #'function arguments)

Example:

CL-USER> (apply #'(lambda (a b c) (+ a b c)) '(1 2 3))
6   
like image 169
nominolo Avatar answered Nov 13 '22 03:11

nominolo


apply

like image 41
Katriel Avatar answered Nov 13 '22 03:11

Katriel