[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

combining atoms



 >Date: Wed, 5 May 1993 15:04:45 -0600
 >To: sdobbs@trivia.coginst.uwf.edu (Steve Dobbs)
 >From: bill@cambridge.apple.com (Bill St. Clair)
 >Subject: Re: combining atoms
 >Cc: info-mcl
 >
 >At  1:22 PM 5/5/93 -0500, Steve Dobbs wrote:
 >>Hello,
 >>        I am helping someone port some lisp code from franzlisp to mcl, and
 >>there is a function that we can find no counterpart for.  In Franzlisp,
 >>there is an 'explode' and an 'implode' function that we can use to combine
 >>two atoms into a single atom.  I have found nothing analagous to this in
 >>Steeles CLtL2, or in any MCL documentation.  Is there anything that I'm
 >>missing, or is there any utils out there that will do this?
 >
 >IMPLODE & EXPLODE are ancient Lisp functions. THey are NOT part of Common
 >Lisp, which doesn't need them. I don't remember whether EXPLODE returned
 >a list of characters, strings, or symbols, but the following should be close:
 >
 >(defun explode (symbol)
 >  (setq symbol (require-type symbol 'symbol))
 >  (map 'list 'identity (symbol-name symbol)))
 >
 >(defun implode (list &optional (package *package*))
 >  (intern (map 'string 'character list) package))
 >
 >

This is also answered in the LISP FAQ, the latest version of which is
available by anonymous ftp from the MKant repository at CMU, and on the
comp.lang.lisp newsgroup. We try to occasionally update the copy
at cambridge.apple.com:/pub/MCL2/contrib/lisp-faq.

Here's the relevant section:


----------
Subject: [2-3] What is the equivalent of EXPLODE and IMPLODE in Common Lisp?

Hopefully, the only reason you need to do this is as part of trying to port
some old MacLisp code to Common Lisp.  These functions predated the
inclusion of strings as a first-class data type in Lisp; symbols were used
as strings, and they ere EXPLODEd to allow the individual characters to be
manipulated in a list.

Probably the best approximations of these are:

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

An alternate definition of EXPLODE which uses MAP instead of LOOP is:

   (defun explode (object)
     (map 'list #'(lambda (char) 
                    (intern (string char)))
          (prin1-to-string object)))

The creation of N conses of garbage to process a string of N
characters is a hideously inefficient way of doing the job.  Rewrite
EXPLODE code with PRIN1-TO-STRING, or better STRING if the arguments
are symbols without funny characters.  For IMPLODE, try to make its
caller use strings and try to make the result usable as a string to
avoid having to call INTERN or READ-FROM-STRING.