word to list

Discussion of Common Lisp

word to list

Postby filfil » Mon Aug 06, 2012 3:24 pm

Hi,

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

Thanx

filfil
filfil
 
Posts: 10
Joined: Mon Aug 06, 2012 3:21 pm

Re: word to list

Postby sylwester » Tue Aug 07, 2012 12:56 am

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
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p
sylwester
 
Posts: 83
Joined: Mon Jul 11, 2011 2:53 pm

Re: word to list

Postby filfil » Tue Aug 07, 2012 3:41 am

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
filfil
 
Posts: 10
Joined: Mon Aug 06, 2012 3:21 pm

Re: word to list

Postby Goheeca » Tue Aug 07, 2012 4:23 am

Use intern:
Code: Select all
(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).
User avatar
Goheeca
 
Posts: 202
Joined: Thu May 10, 2012 12:54 pm

Re: word to list

Postby edgar-rft » Tue Aug 07, 2012 8:41 am

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
edgar-rft
 
Posts: 155
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: word to list

Postby filfil » Tue Aug 07, 2012 11:39 am

INTERN :!:

Thank you very much, Goheeca,
and Edgar for your Lisp cultural notes
filfil
 
Posts: 10
Joined: Mon Aug 06, 2012 3:21 pm


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 2 guests

cron