Using &rest parameters in Common Lisp

Discussion of Common Lisp

Using &rest parameters in Common Lisp

Postby zaxonus » Wed Feb 20, 2019 2:57 am

I am having some trouble using *&rest parameters* in a lisp program (with Common Lisp).

My knowledge of the syntax not being perfect, I may be doing something wrong.

I have two functions:

(defun func-one(&rest params) .....)

(defun func-two(param-a param-b &rest params) .....)

They are working but at some point, inside func-one I need to make a call to function-two, like this:

(func-two value-a value-b params)

I presume what I want is clear to a human reader, but my syntax is wrong because I get an error message like:

*** _ =: (1 2 3 4 5) is not a number

Since "&rest params" is meant to hold a list of numbers, I understand the message.
But how can I get the result I want?

(1 2 3 4 5) passed in the "&rest params" should be seen as : 1 2 3 4 5 by func-two
and not as one element which is a list (indeed not a number).

So, what is the right way to call func-two? Instead of what I did.
zaxonus
 
Posts: 1
Joined: Wed Feb 20, 2019 2:51 am

Re: Using &rest parameters in Common Lisp

Postby nuntius » Wed Feb 20, 2019 10:27 pm

You probably want something like this.

Code: Select all
(defun func-one (&rest params)
  (let ((value-a ...)
        (value-b ...)
    (apply 'func-two (cons value-a (cons value-b params)))
  ...))
User avatar
nuntius
 
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Using &rest parameters in Common Lisp

Postby sylwester » Sun Mar 24, 2019 3:42 pm

Code: Select all
(apply 'func-two (cons value-a (cons value-b params))


Isn't really needed since apply can be used like this:

Code: Select all
(apply #'func-two value-a value-b params)
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p
sylwester
 
Posts: 133
Joined: Mon Jul 11, 2011 2:53 pm

Re: Using &rest parameters in Common Lisp

Postby pjstirling » Sun Mar 24, 2019 4:33 pm

... and if we're gonna necro, then it's also worth pointing out that sharp quote will do the right thing in the face of LABELS/FLET
pjstirling
 
Posts: 164
Joined: Sun Nov 28, 2010 4:21 pm


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 12 guests