软讯网络 > 软件时空 > 软件相关 > 将List中的元素连接成字符串的几种方法
【标 题】:将List中的元素连接成字符串的几种方法
【关键字】:
List
【来 源】:http://www.cublog.cn/u/4614/showart.php?id=97009
将List中的元素连接成字符串的几种方法
较优良的方法:
(reduce #'(lambda (s1 s2) (concatenate 'string s1 "-" s2))
'("albert" "lee" "lisp"))
"albert-lee-lisp"
使用方法:
CL-USER 6 : 1 > (join '("albert" "lee" "silly" "programmer") "-")
"albert-lee-silly-programmer"
方法1: (这个方法比较“传统”,使用循环,不是太好)
(defun join (wlist sepchr)
(let ((string nil))
(dolist (sym wlist string)
(setf string
(concatenate 'string
string (if string sepchr nil) (string sym))))))
方法2:(这个通过 reduce ,lambda 比较巧妙)
(defun join (wlist sepchr)
(reduce #'(lambda(s1 s2) (concatenate 'string s1 sepchr s2))
wlist ))