From: Larry Putnam, Jr.
Subject: Lisp Question
Date: 
Message-ID: <3ri5nq$fgc$1@mhafc.production.compuserve.com>
I have a couple of questions about LISP:

First, is there a way to do copy-by-value in LISP?  Specifically, 
I don't want the new object to share anything with the old 
object.  I would like to be able to do the copy-by-value even for 
objects (user-defined) that have sequences that contain 
sequences.  Of course, I could write a copy-object procedure for 
each object but I was hoping for something more elegant.

Also, are there procedures to translate strings to integers or 
floats?  I could write something myself, but I imagine there is 
some standard procedure call for this.

thanks,
Greg

-- 
Larry Putnam, Jr. 
2000 Corporate Ridge
Suite 900
McLean, VA 22101 Phone 703 790-0055

From: Peter Knauber
Subject: Re: Lisp Question
Date: 
Message-ID: <3rm1j0$rm6@irz1.informatik.uni-kl.de>
In article <············@mhafc.production.compuserve.com>, Larry Putnam,
Jr. <········@CompuServe.COM> writes:
> 
> Also, are there procedures to translate strings to integers or 
> floats?  I could write something myself, but I imagine there is 
> some standard procedure call for this.

The function `read-from-string' should work.

-- Peter

---------------------------------------------------------
Peter Knauber, Universitaet Kaiserslautern
Tel.: 0631 205 2642, email: ·······@informatik.uni-kl.de
From: 55437-olivier clarisse(haim)463
Subject: Re: Lisp Question
Date: 
Message-ID: <DA4zvD.8o0@ssbunews.ih.att.com>
In article <············@mhafc.production.compuserve.com>, Larry Putnam, Jr. <········@CompuServe.COM> writes:
|> I have a couple of questions about LISP:
|> 
|> First, is there a way to do copy-by-value in LISP?  Specifically, 
|> I don't want the new object to share anything with the old 
|> object.  I would like to be able to do the copy-by-value even for 
|> objects (user-defined) that have sequences that contain 
|> sequences.  Of course, I could write a copy-object procedure for 
|> each object but I was hoping for something more elegant.
|> 
(apropos "COPY-" "COMMON-LISP")   ;Available COPY- things.

;;; For reasonable size objects that print readably
(defmethod copy-all (object &rest options)
  (read-from-string (apply #'write-to-string object :readably t options)))

(setq *print-level* 4)                 ;Limit print level.
(copy-all '#1=#(CLOS #1#) :circle t)   ;Circularities suspected.

;;; Then on user defined classes that need to print readably:
(defmethod print-object ((frog frog) stream)
  (cond (*print-readably*
         (write "#." :stream stream :escape nil :circle nil)
         (print-object `(make-instance 'frog :legs ,(frog-legs frog))
		       stream))
	(t
	 (call-next-method)))

;;; For more efficient copying, the designer of a class only knows
;;; how to copy it. Therefore COPY-OBJECT is defined on each class.
;;; Example for lists, sequences and frogs with no circularities:
(defmethod copy-object (object)
  object)

(defmethod copy-object ((object cons))
  (cons (copy-object (first object)) (copy-object (rest object))))

(defmethod copy-object ((seq sequence))
  (loop with cseq = (copy-seq seq)
	for c across cseq as n upfrom 0
	do (setf (elt cseq n) (copy-object c))
	finally (return cseq)))

(defmethod copy-object ((frog frog))
  (make-instance 'frog :legs (copy-object (frog-legs frog))))

|> Also, are there procedures to translate strings to integers or 
|> floats?  I could write something myself, but I imagine there is 
|> some standard procedure call for this.
|> 
;;; See READ-FROM-STRING and COERCE

(coerce (read-from-string "1234") 'float)
-- 
----------------
Olivier Clarisse	     "Languages are not unlike living organisms
Member of Technical Staff     can they adapt and improve to survive?"
AT&T Bell Laboratories
From: Richard Lynch
Subject: Re: Lisp Question
Date: 
Message-ID: <lynch-1406951657530001@129.105.99.135>
In article <············@mhafc.production.compuserve.com>, Larry Putnam,
Jr. <········@CompuServe.COM> wrote:

|Also, are there procedures to translate strings to integers or 

There is also a parse-integer, or somesuch, I think.

-- 
-- 
--
-- "TANSTAAFL"  Rich ·····@ils.nwu.edu
From: Barry Margolin
Subject: Re: Lisp Question
Date: 
Message-ID: <3rqenu$gda@tools.near.net>
In article <············@mhafc.production.compuserve.com> Larry Putnam, Jr. <········@CompuServe.COM> writes:
>First, is there a way to do copy-by-value in LISP?  Specifically, 
>I don't want the new object to share anything with the old 
>object.

Not even interned symbols?  Someone suggested a solution using
WRITE-TO-STRING and READ-FROM-STRING, but that will still share symbols.
-- 
Barry Margolin
BBN Planet Corporation, Cambridge, MA
······@{bbnplanet.com,near.net,nic.near.net}
Phone (617) 873-3126 - Fax (617) 873-5124
From: ········@iexist.flw.att.com
Subject: Re: Lisp Question
Date: 
Message-ID: <DAAJ4A.Bnp@ssbunews.ih.att.com>
|> >First, is there a way to do copy-by-value in LISP?  Specifically, 
|> >I don't want the new object to share anything with the old 
|> >object.
|> 
|> Not even interned symbols?  Someone suggested a solution using
|> WRITE-TO-STRING and READ-FROM-STRING, but that will still share symbols.

That's right and if you don't want to share symbols at all
you can use strings (if you did not want Lisp to be a symbolic
language) or use MAKE-SYMBOL.

Now should symbols created by MAKE-SYMBOL (e.g. GENSYM) be shared
by the copy of not? If they are not and one of the objects copied
contains an expanded interpreted lambda, the lambda from the copy
will probably not work [e.g. a compiler will complain that variable
#:G1234 is declared and not used and that several other variables
#:G1234, #:G1234 and #:G1234 are now assumed special...
	- It's amazing how good Lisp compilers can be.]

This points right back to the READ-FROM-STRING of WRITE-TO-STRING
solution posted earlier to make copies of objects that contain
any form of circulary (a symbol is an object so an interpreted
lambda with GENSYMs is an object with circularities too).
It is then safer to insure the COPY-ALL solution always has :CIRCLE T.

(defmethod copy-all (object)
  (with-standard-io-syntax	;Safer version
    (read-from-string (write-to-string object :circle t))))

And how about numbers? Should they be the same (EQ) or copied
(EQL or only EQUAL)? COPY-ALL does it right, just as the reader.

|> -- 
|> Barry Margolin
|> BBN Planet Corporation, Cambridge, MA
|> ······@{bbnplanet.com,near.net,nic.near.net}
|> Phone (617) 873-3126 - Fax (617) 873-5124
-- 
----------------
Olivier Clarisse	     "Languages are not unlike living organisms
Member of Technical Staff     can they adapt and improve to survive?"
AT&T Bell Laboratories