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.bugs Subject: bug#17825: broken set! in iteration Date: Sat, 21 Jun 2014 17:19:52 -0400 Message-ID: <87vbruvu2v.fsf@yeeloong.lan> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1403385690 31227 80.91.229.3 (21 Jun 2014 21:21:30 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 21 Jun 2014 21:21:30 +0000 (UTC) Cc: 17825@debbugs.gnu.org, request@debbugs.gnu.org To: =?UTF-8?Q?Al=C3=ADrio?= Eyng Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Sat Jun 21 23:21:22 2014 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WySiv-0004Hd-AN for guile-bugs@m.gmane.org; Sat, 21 Jun 2014 23:21:21 +0200 Original-Received: from localhost ([::1]:46506 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WySiu-00037l-Jy for guile-bugs@m.gmane.org; Sat, 21 Jun 2014 17:21:20 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:38438) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WySik-00036n-NU for bug-guile@gnu.org; Sat, 21 Jun 2014 17:21:18 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WySic-0000o0-WA for bug-guile@gnu.org; Sat, 21 Jun 2014 17:21:10 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:37057) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WySic-0000ns-Tn for bug-guile@gnu.org; Sat, 21 Jun 2014 17:21:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1WySic-0004F7-5l for bug-guile@gnu.org; Sat, 21 Jun 2014 17:21:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Mark H Weaver Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Sat, 21 Jun 2014 21:21:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 17825 X-GNU-PR-Package: guile X-GNU-PR-Keywords: Original-Received: via spool by 17825-submit@debbugs.gnu.org id=B17825.140338564416276 (code B ref 17825); Sat, 21 Jun 2014 21:21:02 +0000 Original-Received: (at 17825) by debbugs.gnu.org; 21 Jun 2014 21:20:44 +0000 Original-Received: from localhost ([127.0.0.1]:56440 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1WySiJ-0004EN-Ll for submit@debbugs.gnu.org; Sat, 21 Jun 2014 17:20:43 -0400 Original-Received: from world.peace.net ([96.39.62.75]:47159 ident=hope6) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1WySiF-0004E5-G1; Sat, 21 Jun 2014 17:20:40 -0400 Original-Received: from 209-6-91-212.c3-0.smr-ubr1.sbo-smr.ma.cable.rcn.com ([209.6.91.212] helo=yeeloong.lan) by world.peace.net with esmtpsa (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1WySi7-0003yN-Mc; Sat, 21 Jun 2014 17:20:31 -0400 In-Reply-To: ("=?UTF-8?Q?Al=C3=ADrio?= Eyng"'s message of "Sat, 21 Jun 2014 01:25:56 +0000") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Original-Sender: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.bugs:7504 Archived-At: tags 17825 notabug close 17825 thanks Al=C3=ADrio Eyng writes: > I can't see why the second code doesn't work like the first: > > (use-modules (srfi srfi-1)) > (define D '(((3 4)))) > (let ((r 1)) > (set! D (append D '(()))) > (display D)(newline) > (set-car! (drop D r) (car (drop D (- r 1))))) > (let ((r 2)) > (set! D (append D '(()))) > (display D)(newline) > (set-car! (drop D r) (car (drop D (- r 1))))) > output: > (((3 4)) ()) > (((3 4)) ((3 4)) ()) > > (use-modules (srfi srfi-1)) > (define D '(((3 4)))) > (map (lambda (r) > (set! D (append D '(()))) > (display D)(newline) > (set-car! (drop D r) (car (drop D (- r 1))))) > '(1 2)) > output: > (((3 4)) ()) > (((3 4)) ((3 4)) ((3 4))) This code mutates literal lists, which is not allowed. Specifically, this code calls 'set-car!' on pairs that come from a literal list, namely '(()). The optimizer assumes that this will never happen, and generates code based on that assumption. So, you need to change '(()) to (list '()). Also, if you will ever mutate the pairs in '(((3 4))), you'll need to change that as well. Another problem is that 'map' does not specify the order in which the procedure is applied to the elements of the input list. We have 'map-in-order', but both of those also build a list of the results, which is not needed here. Here, you should be using 'for-each', which guarantees the order of iteration and does not build a list of the results. The following code works as expected: --8<---------------cut here---------------start------------->8--- (use-modules (srfi srfi-1)) (define D '(((3 4)))) (for-each (lambda (r) (set! D (append D (list '()))) (display D)(newline) (set-car! (drop D r) (car (drop D (- r 1))))) '(1 2)) --8<---------------cut here---------------end--------------->8--- and outputs: --8<---------------cut here---------------start------------->8--- (((3 4)) ()) (((3 4)) ((3 4)) ()) --8<---------------cut here---------------end--------------->8--- I'm closing this bug, but if you think there's still a bug here, feel free to reopen it. Thanks! Mark