[comp.lang.scheme] Explode/Implode Query

betsey@cunixf.cc.columbia.edu (Elizabeth Fike) (04/23/91)

Today, in my Scheme class, the professor mentioned the functions
explode and implode, are found in PC-Scheme, but in no
other current versions of Scheme.  As these functions seem
extremely useful, I am curious as to why they are not found in
MIT Scheme version 7 (for example).  

Thanks in advance!

/betsey
<betsey@cunixf.cc.columbia.edu>

"I think that God's got a sick sense of humor
 And when I die, I expect to find him laughing" 
				--Depeche Mode, "Blasphemous Rumors"

jinx@zurich.ai.mit.edu (Guillermo J. Rozas) (04/23/91)

In article <1991Apr22.180734.21661@cunixf.cc.columbia.edu> betsey@cunixf.cc.columbia.edu (Elizabeth Fike) writes:

   Path: ai-lab!mintaka!think.com!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!cunixf.cc.columbia.edu!betsey
   From: betsey@cunixf.cc.columbia.edu (Elizabeth Fike)
   Newsgroups: comp.lang.scheme
   Date: 22 Apr 91 18:07:34 GMT
   Sender: betsey@cunixf.cc.columbia.edu (Elizabeth Fike)
   Reply-To: betsey@cunixf.cc.columbia.edu
   Organization: Columbia University
   Lines: 14

   Today, in my Scheme class, the professor mentioned the functions
   explode and implode, are found in PC-Scheme, but in no
   other current versions of Scheme.  As these functions seem
   extremely useful, I am curious as to why they are not found in
   MIT Scheme version 7 (for example).  

   Thanks in advance!

   /betsey
   <betsey@cunixf.cc.columbia.edu>

   "I think that God's got a sick sense of humor
    And when I die, I expect to find him laughing" 
				   --Depeche Mode, "Blasphemous Rumors"

IMPLODE and EXPLODE are leftovers from older systems (MacLisp) that
are referred to in SICP (Structure and Interpretation of Computer
Programs by Abelson, Sussman, and Sussman), and (I believe) PC-Scheme
included them in order to be compatible.

MIT Scheme has evolved since the time when SICP was published and no
longer provides them in the standard load.  They are provided in the
SICP compatibility package.

MIT Scheme currently provides more useful alternatives.  You may want
to play with the following procedures:

write-to-string
with-output-to-string
with-output-to-truncated-string
with-string-output-port
with-input-from-string
string->input-port

You can get the SICP compatibility package for MIT Scheme 7.1 from
altdorf.ai.mit.edu:archive/scheme-7.1/sicp.tar.Z by anonymous ftp.

raja@copper.ucs.indiana.edu (Raja Sooriamurthi) (04/23/91)

betsey@cunixf.cc.columbia.edu (Elizabeth Fike) writes:

>Today, in my Scheme class, the professor mentioned the functions
>explode and implode, are found in PC-Scheme, but in no
>other current versions of Scheme.  As these functions seem
>extremely useful, I am curious as to why they are not found in
>MIT Scheme version 7 (for example).  

Chez Scheme provides these functions under sicp mode (by doing
(support-sicp) )

But you can define these function yourself in any R3.99 scheme as
below:

; (explode 'apple) => (a p p l e)
; (implode '(a p p l e)) => apple
; (eq? 'apple (implode (explode 'apple))) => #t

(define explode
  (lambda (sym)
    (map string->symbol
	 (map string
	      (string->list (symbol->string sym))))))

(define implode
  (lambda (s*)
    (string->symbol
      (apply string-append
	     (map symbol->string s*)))))

-Raja


---
Raja Sooriamurthi                          |   Computer Science Department
raja@cs.indiana.edu	                   |       Indiana University

dorai@tone.rice.edu (Dorai Sitaram) (04/23/91)

In article <raja.672349553@copper> raja@copper.ucs.indiana.edu (Raja Sooriamurthi) writes:
>But you can define these function yourself in any R3.99 scheme as
>below:
>
>; (explode 'apple) => (a p p l e)
>; (implode '(a p p l e)) => apple
>; (eq? 'apple (implode (explode 'apple))) => #t
>
> [perfectly reasonable Scheme "interpretations" of explode and implode deleted]

You have "typed" explode and implode to go between symbols and
single-char symbols.  However, the way I remember it, the following
were quite legal.

(explode 'apple2) => (a p p l e 2)      ;;; not (... |2|)
(implode '(a p p l e 2)) => apple2
(explode 42) => (4 2)			;;; not (|4| |2|) 
(implode '(4 2)) => 42			;;; not |42| 

Indeed, I think I remember seeing explode even take on lists!!  E.g.,

(explode '(apple 2)) => (|(| a p p l e | | 2 |)|)

An unsatisfactory first attempt at getting something like this in Ch*z
-- or a Scheme with stringports -- would be

(define digit-or-single-char-symbol
  (let ([zero (char->integer #\0)])
    (lambda (c)
      (if (and (char>=? c #\0) (char<=? c #\9))
	  (- (char->integer c) zero)
	  (string->symbol (format "~a" c))))))

(define explode
  (lambda (x)
    (map digit-or-single-char-symbol
	 (string->list (format "~a" x)))))

(define implode
  (lambda (s)
    (let ([p (open-input-string
	      (apply string-append
		     (map (lambda (c) (format "~a" c)) s)))])
      (begin0 (read p)
	      (close-input-port p)))))

This is unsatisfactory, since you'll have to do some more work to
get the second half of the following working:

(explode '|apple 2|) => (a p p l e | | 2)
(implode '(a p p l e | | 2)) => |apple 2|	;;; not apple

--d