This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

word to list

6 posts · 5909 views

Hi,

is there a mode to transform a word into a list, e.g. (word) -> (w o r d)?

Thanx

filfil

Re: word to list

The way you write it seems like its a list with one elemet which is symbol.
(defun struct->list (l) 
    (coerce (string (car l)) 'list))
If your argument really is a string, the only thing you need to do is
(coerce "string" 'list)
You get a lot of more pointers about strings in CL here: http://cl-cookbook.sourceforge.net/strings.html
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p

Re: word to list

Thank you :-)

with your function, the result is thus:
> (struct->list '(word))
(#\W #\O #\R #\D)
I've read this http://cl-cookbook.sourceforge.net/strings.html and it's quite useful. It's a good thing converting characters into strings...
> (setf a (struct->list '(word)))
(#\W #\O #\R #\D)
> (mapcar #'string a)
("W" "O" "R" "D")
...but I'd like get a "pure" letter list, without #\ or "", like this:
(W O R D)
and I didn't find the way. Does anyone help me?

Thank you

filfil

Re: word to list

Use intern:
(mapcar #'intern '("A" "B" "C"))
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: word to list

In MacLisp (one of the predecessor languages of Common Lisp) there existed two functions:
  • EXPLODE - split a symbol-name into a list of single-character symbols
  • IMPLODE - concatenate a list of single-character symbols into a symbol-name
(defun explode (object)
  (loop for char across (prin1-to-string object)
        collect (intern (string char))))

(defun implode (list)
  (read-from-string (coerce (mapcar #'character list) 'string)))
This has the following effect:
(explode 'hello)        => (H E L L O)
(implode '(h e l l o))  => HELLO
This was necessary because MacLisp had no STRING data type. In Common Lisp it's better to use SYMBOL-NAME and INTERN to convert between symbol-names and strings and do the splitting and concatenating via string functions.

See What is the equivalent of EXPLODE and IMPLODE in Common Lisp?

- edgar

Re: word to list

INTERN :!:

Thank you very much, Goheeca,
and Edgar for your Lisp cultural notes