Skip to content

Instantly share code, notes, and snippets.

@tompurl
Last active September 28, 2023 10:51
Show Gist options
  • Save tompurl/5174818 to your computer and use it in GitHub Desktop.
Save tompurl/5174818 to your computer and use it in GitHub Desktop.
Common Lisp function that converts a list into a string of concatenated characters.
(defun numlist-to-string (lst)
(when lst
(concatenate 'string
(write-to-string (car lst)) (numlist-to-string (cdr lst)))))
; Convert a list of numbers into a number
; (list 1 2 3 4) => 1234
(parse-integer (numlist-to-string (list 1 2 3 4)))
@MatthewRock
Copy link

I know it's old, but I guess the "better" version would be:

(defun list-to-string (lst)
    (format nil "~{~A~}" lst))

This ensures that the function always returns the string (although sometimes it might be empty), and works for arbitrary things to concatenate them.

I guess it would also be faster, since there would be no recursion.

@massimo-zaniboni
Copy link

This should be the standard way:

(coerce some-list-of-chars 'string)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment