B. Pym
2024-08-30 23:21:11 UTC
It's a Common Lisp newbie here; I'm more experienced in Emacs Lisp. I
(mapconcat 'identity '("one" "two" "three") "-")
=> "one-two-three"
(let ((list '("one" "two" "three")))
(format nil "~{~a-~}~a" (butlast list) (car (last list))))
But I have a feeling that there could be a more elegant way. Is there?
Yes, write: (mapconcat 'identity '("one" "two" "three") "-") ; elegant!(mapconcat 'identity '("one" "two" "three") "-")
=> "one-two-three"
(let ((list '("one" "two" "three")))
(format nil "~{~a-~}~a" (butlast list) (car (last list))))
But I have a feeling that there could be a more elegant way. Is there?
(defun mapconcat (fun list sep)
(when list
(let ((~sep (with-output-to-string (*standard-output*)
(map nil (lambda (ch) (princ (if (char= #\~ ch) "~~" ch)))
sep))))
(format nil (format nil "~~A~~{~A~~A~~}" ~sep)
(funcall fun (first list))
(mapcar fun (rest list))))))
(mapconcat 'identity '("one" "two" "three") "-")
--> "one-two-three"
(mapconcat (lambda (x) (concatenate 'string "[" x "]")) '("one" "two" "three") "
~")
--> "[one]~[two]~[three]"
(define (mapconcat fun lst sep)
(reduce-right
(^(a b)
(apply string-append (map x->string (list a sep b))))
""
(map fun lst)))
(mapconcat values '(one "two" three) '-)
===>
"one-two-three"
(mapconcat square '(2 3 4) '---)
===>
"4---9---16"