From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.devel Subject: Re: Guile: What's wrong with this? Date: Fri, 06 Jan 2012 15:03:22 -0500 Message-ID: <8739bsbnud.fsf@netris.org> References: <4F027F35.5020001@gmail.com> <1325603029.22166.YahooMailNeo@web37906.mail.mud.yahoo.com> <4F032C41.3070300@gmail.com> <87mxa4ifux.fsf@gnu.org> <4F038BF4.1070200@gnu.org> <87obujzmmc.fsf@Kagami.home> <4F048972.5040803@gnu.org> <87lipnm8yx.fsf@Kagami.home> <4F04D01D.5050801@gnu.org> <8762grf28k.fsf@netris.org> <4F05DC47.1000202@gnu.org> <878vlldb4k.fsf@netris.org> <1325811764.22562.YahooMailNeo@web37903.mail.mud.yahoo.com> <87wr95bo9y.fsf@netris.org> <1325857075.77324.YahooMailNeo@web37903.mail.mud.yahoo.com> <877h14bsx0.fsf@netris.org> <4F074647.1020000@gnu.org> <87y5tkvduj.fsf@fencepost.gnu.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1325880244 26469 80.91.229.12 (6 Jan 2012 20:04:04 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 6 Jan 2012 20:04:04 +0000 (UTC) Cc: guile-devel@gnu.org To: David Kastrup Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Fri Jan 06 21:03:59 2012 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1RjG18-0006nZ-Pc for guile-devel@m.gmane.org; Fri, 06 Jan 2012 21:03:58 +0100 Original-Received: from localhost ([::1]:37164 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RjG18-0007Ss-8R for guile-devel@m.gmane.org; Fri, 06 Jan 2012 15:03:58 -0500 Original-Received: from eggs.gnu.org ([140.186.70.92]:54242) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RjG12-0007Sm-NU for guile-devel@gnu.org; Fri, 06 Jan 2012 15:03:57 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RjG11-0006vN-B3 for guile-devel@gnu.org; Fri, 06 Jan 2012 15:03:52 -0500 Original-Received: from world.peace.net ([96.39.62.75]:36515) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RjG11-0006tP-0b; Fri, 06 Jan 2012 15:03:51 -0500 Original-Received: from 209-6-91-212.c3-0.smr-ubr1.sbo-smr.ma.cable.rcn.com ([209.6.91.212] helo=yeeloong) by world.peace.net with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.69) (envelope-from ) id 1RjG0s-0001wk-PD; Fri, 06 Jan 2012 15:03:43 -0500 In-Reply-To: <87y5tkvduj.fsf@fencepost.gnu.org> (David Kastrup's message of "Fri, 06 Jan 2012 20:19:00 +0100") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.92 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 96.39.62.75 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:13368 Archived-At: David Kastrup writes: > Bruce Korb writes: > >>> sprintf(buf, "(define %s \"%s\")", "foo", my_str); >>> scm_eval_string(buf); >>> sprintf(buf, "(string-upcase! %s)", "foo") >>> // the string from my_str in "buf" is now scribbled over and completely gone >>> scm_eval_string(buf); >> >> Since I know the program I initially wrote (the define) is now gone, > > Why would a define be gone? I think what Bruce means here is that, in theory, the string object created in the above `define' might have held a reference to part of his buffer `buf'. And indeed, we do make a copy of that buffer. So why not make a mutable copy? The reason is that, even though we make a copy of the program as we read it (converting from the string representation of `buf' into our internal representation), we'd like to be able to use the program multiple times. When I speak of the "program text", I'm not referring to the string representation of the program, but rather the internal representation. If we allow the user to unwittingly modify the program, it might work once but fail thereafter, as in: (define ten-spaces-with-one-star-at (lambda (i) (define s " ") (string-set! s i #\*) s)) Now, some reasonable people might say "Why arbitrarily limit the user? He might know what he's doing, and he might really want to do this!" Scheme provides a nice way to do this too: (define ten-spaces-with-new-star-at (let ((s (make-string 10 #\space))) (lambda (i) (string-set! s i #\*) s))) I normally lean toward assuming that the user knows what he's doing, but in this case I think Scheme got it right. Accidentally modifying literals is a very common mistake, and is almost never a good idea. If you want to make a program with internal mutable state, Scheme provides free variables, as used in the example above. Mark