English 中文(简体)
加入名单的明智方式是什么?
原标题:What s the canonical way to join strings in a list?

我想将<代码>(“USERID=XYZ”)“USERPWD=123”改为。 审判

(apply # concatenate  string  ("USERID=XYZ" "USERPWD=123"))

编号

但我不知道如何插入和安放; 以下职能行之有效,但似乎很复杂。

(defun join (list &optional (delim "&"))
    (with-output-to-string (s)
        (when list
            (format s "~A" (first list))
            (dolist (element (rest list))
               (format s "~A~A" delim element)))))
最佳回答

http://www.lispwork.com/documentation/HyperSpec/Body/22_cg.htm“rel=“noreferer”>FORMAT。

。 denote iteration, ~A denotes aesthetic publication, and ~^ (aka Tilde Circumflex in the docs) denotesprint the , only when which follows.

* (format nil "~{~A~^, ~}"  ( 1 2 3 4 ))

"1, 2, 3, 4"
* 
问题回答

这一解决办法使我们得以使用<条码>FORMAT,以形成一种扼杀,并具有一个可变的限定。 目的是不为这项职能的每一呼吁安排一个新的<>格式>。 一位良好的共同文献汇编员还可能想汇编一个固定的format string,当“format string ”在运行时间建造时被打败。 见宏观formatter

(defun %d (stream &rest args)
  "internal function, writing the dynamic value of the variable
DELIM to the output STREAM. To be called from inside JOIN."
  (declare (ignore args)
           (special delim))
  (princ delim stream))

(defun join (list delim)
  "creates a string, with the elements of list printed and each
element separated by DELIM"
  (declare (special delim))
  (format nil "~{~a~^~/%d/~:*~}" list))

解释:

"~{      iteration start
 ~a      print element
 ~^      exit iteration if no more elements
 ~/%d/   call function %d with one element
 ~:*     move one element backwards
 ~}"     end of iteration command

<>代码>%d只是内部功能,不应在join外打电话。 作为标识,它有<条码>%

rel=“nofollow noreferer”> :>code>foo,从format string

各种变量<代码>delim被宣布为特别的,因此可对被转让为%d功能的划界人具有价值。 由于我们可以将<条码>%d功能从FORMAT改为限定词,我们需要从其他地方——这里从<条码>中引入的有活力的具有约束力的动态中获取。

职能<代码>%d的唯一目的是写一个限定词——它忽视了<代码>format通过的论点——它只使用<代码>的<的论点。

当事人很晚,但reduce 罚款:

(reduce (lambda (acc x)
          (if (zerop (length acc))
              x
              (concatenate  string acc "&" x)))
        (list "name=slappy" "friends=none" "eats=dogpoo")
        :initial-value "")

假设一个插图和单一特性限制清单,以下人员应高效地开展工作,经常在短名单上援引:

(defun join (list &optional (delimiter #&))
  (with-output-to-string (stream)
    (join-to-stream stream list delimiter)))

(defun join-to-stream (stream list &optional (delimiter #&))
  (destructuring-bind (&optional first &rest rest) list
    (when first
      (write-string first stream)
      (when rest
        (write-char delimiter stream)
        (join-to-stream stream rest delimiter)))))




相关问题
How to make a list of arrays, not their symbols, in Lisp?

I m trying to make a function to get a delta between arrays, but right now just want to make a subset: get Nth element. (defvar p1 #(1 2)) (defvar p2 #(3 4)) (mapcar (lambda (x) (aref x 0)) (p1 ...

SBCL standard library documentation? [closed]

I want to learn and use SBCL because of its ease of learning and speed. (I ve been playing with Lisp 3 years ago, and now am refreshing it.) But how can I learn what s included in the standard library,...

returning a lambda function in clisp, then evaluating it

Suppose I have this wonderful function foo [92]> (defun foo () (lambda() 42)) FOO [93]> (foo) #<FUNCTION :LAMBDA NIL 42> [94]> Now, suppose I want to actually use foo and return 42. ...

A lisp function refinement

I ve done the Graham Common Lisp Chapter 5 Exercise 5, which requires a function that takes an object X and a vector V, and returns a list of all the objects that immediately precede X in V. It works ...

lambda-gtk negative pointer

I was trying to write my own put-pixel on (Gdk) pixbuf in Lisp. When I finally realized how I can operate on C pointers in CL, new obstacle came along - (gdk:pixbuf-get-pixels pb) returns me negative ...

Pointers in Lisp?

I ve started learning Lisp recently and wanted to write a program which uses gtk interface. I ve installed lambda-gtk bindings (on CMUCL). I want to have putpixel/getpixel ability on a pixbuf. But I ...

Is there a common lisp package naming convention?

I have created some of my own user packages and have run into a name clash. In Java, the naming convention is to use your domain name in the package name: e.g. import com.example.somepackage;. Are ...

热门标签