Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!mcvax!ukc!its63b!aiva!jeff From: jeff@aiva.ed.ac.uk (Jeff Dalton) Newsgroups: comp.lang.lisp Subject: Re: Implode, explode and Common Lisp Message-ID: <138@aiva.ed.ac.uk> Date: Thu, 6-Aug-87 16:34:54 EDT Article-I.D.: aiva.138 Posted: Thu Aug 6 16:34:54 1987 Date-Received: Sat, 8-Aug-87 16:58:03 EDT References: <1840@chalmers.UUCP> Reply-To: jeff@uk.ac.ed.aiva (Jeff Dalton) Distribution: world Organization: Dept. of AI, Univ. of Edinburgh, UK Lines: 95 In article <1840@chalmers.UUCP> sundin@chalmers.UUCP (Ulf Sundin) writes: > I intend to translate a rather large Franz Lisp program > to Common Lisp. However, some parts of the progeram relies > heavily on the use of 'implode' and 'explode' for which > no equivalents are defined in Common Lisp. I wrote the following code a while ago and have used it with some success. ;;; Implode, explode, and friends ;;; ;;; Jeff Dalton, AIAI, University of Edinburgh ;;; ;;; In Franz, a character can be represented by either (1) a symbol with ;;; that character as the first character of its print name, or (2) the ;;; (ascii) value of that character as a fixnum. So we have to handle ;;; all of these. Actually, we accept only symbols with one character ;;; in their print name. ;;; ;;; Coercion and conversion in Common Lisp can be a pain because it is ;;; not always clear which function will do the trick. For example, ;;; (coerce #\a 'string) is an error; you have to use (string #\a). ;;; For some reason, the powers of 'string' and 'coerce' are reversed ;;; for sequences: 'string' applied to a sequence of characters gives ;;; an error, but 'coerce' will in fact coerce it. ;;; ;;; Note: we use the '->' convention for naming conversion functions ;;; (a la T). ;;; conversions to Franz forms (defun char->fixnum (ch) (char-code ch)) ;ignore bits and font (defun char->symbol (ch) (intern (string ch))) ;assumes *package* is OK ;;; conversions from Franz forms (defun franz-char->char (fch) ;; works for all the cases we need, and for single-char strings. (coerce fch 'character)) ;;; the Franz functions (defun explode (x) (map 'list #'char->symbol (prin1-to-string x))) (defun aexplode (x) (explode x)) (defun explodec (x) (map 'list #'char->symbol (princ-to-string x))) (defun aexplodec (x) (explodec x)) (defun exploden (x) (map 'list #'char->fixnum (princ-to-string x))) (defun aexploden (x) (exploden x)) (defun implode (x) (intern (map 'string #'franz-char->char x))) (defun maknam (x) (make-symbol (map 'string #'franz-char->char x))) (defun printlist (x) (explode x)) ;not actually in Franz? (defun readlist (x) ;seldom used? (read-from-string (map 'string #'franz-char->char x))) ;;;; Other Character and String operations (defun getchar (symbol-or-string n) (char->symbol (schar (string symbol-or-string) (1- n)))) (defun nthchar (x n) (getchar x n)) (defun getcharn (symbol-or-string n) (char->fixnum (schar (string symbol-or-string) (1- n)))) (defun concat (x y &rest more-args) (intern (apply #'concatenate 'string (string x) (string y) (mapcar #'string more-args)))) Jeff Dalton, JANET: J.Dalton@uk.ac.ed AI Applications Institute, ARPA: J.Dalton%uk.ac.ed@cs.ucl.ac.uk Edinburgh University. UUCP: ...!ukc!ed.ac.uk!J.Dalton