tags:

views:

83

answers:

3

I want to define a function that accepts &rest parameters and delegates them to another function.

(html "blah" "foo" baz) => "<html>blahfoobaz</html>"

I did not find a better way than this one:

(defun html (&rest values)
  (concatenate 'string 
               "<html>" 
               (reduce #'(lambda (a b)
                           (concatenate 'string a b))
                       values :initial-value "") 
               "</html>"))

But this looks somewhat glumbsy to me, since line 4 does no more than concatenating the &rest parameter "values". I tried (concatenate 'string "<html>" (values-list values) "</html>") but this does not seem to work (SBCL). Could someone give me an advice?

Kind regards

+2  A: 
(defun html (&rest values) 
  (apply #'concatenate 'string values))
Michiel Borkent
Please note the question was changed after I gave this answer.
Michiel Borkent
+3  A: 
Svante
+1 for the humorous and Lispy way of pointing to CL-WHO :-)
Vijay Mathew
A: 

Thank you Michiel. This works perfectly. But how does CL know, that this has to be reduced to

apply [#'concatenate 'string] [values]

and not to

apply [#'concatenate] ['string values]

Patrick
That would have been better as a comment to Michiel's answer rather than as an answer in and of itself. As for this question, "it doesn't matter". CONCATENATE expects an initial sequence/type (in this case STRING) and sequences to concatenate. APPLY takes a function designator, individual arguments and a final list of arguments.
Vatine
Patrick: I agree with Vatine (on posting a comment and apply). Also see http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/fun_apply.html
Michiel Borkent
`Apply` is the operator, `#'concatenate` the first argument, `'string` the second argument, and `values` the third argument. There are no precedence considerations in Lisp.
Svante