【问题标题】:Dealing with &rest-parameters in common lisp在 common lisp 中处理 &rest-parameters
【发布时间】:2010-04-13 22:50:42
【问题描述】:

我想定义一个接受&rest参数并将它们委托给另一个函数的函数。

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

我没有找到比这个更好的方法:

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

但这在我看来有些笨拙,因为第 4 行只不过是连接 &rest 参数“值”。我试过(concatenate 'string "&lt;html&gt;" (values-list values) "&lt;/html&gt;"),但这似乎不起作用(SBCL)。有人可以给我一个建议吗?

亲切的问候

【问题讨论】:

    标签: common-lisp


    【解决方案1】:
    (defun html (&rest values) 
      (apply #'concatenate 'string values))
    

    【讨论】:

    • 请注意,在我给出这个答案后,问题已经改变了。
    【解决方案2】:

    原则上不会好很多,除非你使用format,但你可以使用CL-WHO库,它可以让你用Lisp编写HTML:

    (defun hello-page() (with-html-output-to-string (字符串) (:html (:head (:title "你好,世界!")) (:body (:h3 "你好,世界!") (:a :href "http://weitz.de/cl-who/" “CL-WHO 图书馆”)))))

    编辑:format 方式也许也应该显示:

    (defun html (&rest 值) (格式为零“~{~a~}”值))

    【讨论】:

      猜你喜欢
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 2010-10-17
      • 1970-01-01
      相关资源
      最近更新 更多