Page 1 of 1

word to list

Posted: Mon Aug 06, 2012 3:24 pm
by filfil
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

Posted: Tue Aug 07, 2012 12:56 am
by sylwester
The way you write it seems like its a list with one elemet which is symbol.

Code: Select all

(defun struct->list (l) 
    (coerce (string (car l)) 'list))
If your argument really is a string, the only thing you need to do is

Code: Select all

(coerce "string" 'list)
You get a lot of more pointers about strings in CL here: http://cl-cookbook.sourceforge.net/strings.html

Re: word to list

Posted: Tue Aug 07, 2012 3:41 am
by filfil
Thank you :-)

with your function, the result is thus:

Code: Select all

> (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...

Code: Select all

> (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:

Code: Select all

(W O R D)
and I didn't find the way. Does anyone help me?

Thank you

filfil

Re: word to list

Posted: Tue Aug 07, 2012 4:23 am
by Goheeca
Use intern:

Code: Select all

(mapcar #'intern '("A" "B" "C"))

Re: word to list

Posted: Tue Aug 07, 2012 8:41 am
by edgar-rft
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

Code: Select all

(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:

Code: Select all

(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

Posted: Tue Aug 07, 2012 11:39 am
by filfil
INTERN :!:

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