From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: ludo@gnu.org (Ludovic =?utf-8?Q?Court=C3=A8s?=) Newsgroups: gmane.lisp.guile.devel Subject: Re: for-each et al Date: Sun, 02 Mar 2014 22:27:52 +0100 Message-ID: <87r46ks33b.fsf@gnu.org> References: <87txbgr3wx.fsf@pobox.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1393795693 25063 80.91.229.3 (2 Mar 2014 21:28:13 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 2 Mar 2014 21:28:13 +0000 (UTC) To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sun Mar 02 22:28:21 2014 Return-path: Envelope-to: guile-devel@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 1WKDvp-0005kG-75 for guile-devel@m.gmane.org; Sun, 02 Mar 2014 22:28:21 +0100 Original-Received: from localhost ([::1]:36685 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WKDvo-0001aJ-Fq for guile-devel@m.gmane.org; Sun, 02 Mar 2014 16:28:20 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:36146) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WKDvg-0001SR-7Y for guile-devel@gnu.org; Sun, 02 Mar 2014 16:28:18 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WKDva-0000aq-24 for guile-devel@gnu.org; Sun, 02 Mar 2014 16:28:12 -0500 Original-Received: from plane.gmane.org ([80.91.229.3]:56840) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WKDvZ-0000al-RZ for guile-devel@gnu.org; Sun, 02 Mar 2014 16:28:05 -0500 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1WKDvX-0005c0-LA for guile-devel@gnu.org; Sun, 02 Mar 2014 22:28:03 +0100 Original-Received: from reverse-83.fdn.fr ([80.67.176.83]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 02 Mar 2014 22:28:03 +0100 Original-Received: from ludo by reverse-83.fdn.fr with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 02 Mar 2014 22:28:03 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 84 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: reverse-83.fdn.fr X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 12 =?utf-8?Q?Vent=C3=B4se?= an 222 de la =?utf-8?Q?R?= =?utf-8?Q?=C3=A9volution?= X-PGP-Key-ID: 0xEA52ECF4 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 83C4 F8E5 10A3 3B4C 5BEA D15D 77DD 95E2 EA52 ECF4 X-OS: x86_64-unknown-linux-gnu User-Agent: Gnus/5.130007 (Ma Gnus v0.7) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:ELt7pFK1fHLxBDmwk1Gv2UySWEs= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 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:16943 Archived-At: Hello! Welcome back to email. ;-) Andy Wingo skribis: > The current boot-9 for-each for one list argument is this nastiness: > > (define for-each > (case-lambda > ((f l) > (let for-each1 ((hare l) (tortoise l)) > (if (pair? hare) > (begin > (f (car hare)) > (let ((hare (cdr hare))) > (if (pair? hare) > (begin > (when (eq? tortoise hare) > (scm-error 'wrong-type-arg "for-each" "Circular list: ~S" > (list l) #f)) > (f (car hare)) > (for-each1 (cdr hare) (cdr tortoise))) > (for-each1 hare tortoise)))) > (if (not (null? hare)) > (scm-error 'wrong-type-arg "for-each" "Not a list: ~S" > (list l) #f))))) > ...)) > > Terrible. And it's slower than this: > > (lambda (f l) > (unless (list? l) > (scm-error 'wrong-type-arg "for-each" "Not a list: ~S" (list l) #f)) > (let for-each1 ((l l)) > (unless (null? l) > (f (car l)) > (for-each1 (cdr l))))) > > So, pop quiz: what's the difference between the two? I think an important difference you didn’t mention is that ‘list?’ does its list traversal in C. > Of course, there are different levels at which this problem can be > solved. I think moving towards deprecation and removal of mutable pairs > is probably a good idea, though it's really complicated and not really > the point of this mail. OTOH I think it's reasonable to ask for a > consensus opinion on this implementation of "for-each": > > (lambda (f l) > (unless (list? l) > (scm-error 'wrong-type-arg "for-each" "Not a list: ~S" (list l) #f)) > (let for-each1 ((l l)) > (unless (null? l) > (f (car l)) > (for-each1 (cdr l))))) > > Is this a reasonable implementation? Are we OK with the possibility for > infinite-loops or exceptions accessing the car/cdr of what might not be > a pair? Alternately if we change the test to pair? are we OK with the > possibility of the loop possibly ending silently before its time? How > do we deal with this kind of bug -- what's our perspective? I’m OK with this implementation. I’ve never found myself iterating over a list and modifying it concurrently. > My proposal would be that eventually, I don't know how, but Guile should > remove all use of mutable pairs in its own code. There's not much > set-car!/set-cdr! in Scheme so it's not that big of a deal. Therefore > in light of that long-term goal, we should write library code as if > pairs were immutable, and therefore the test-then-iterate example is > fine code. > > WDYT? I’m all in favor of immutable pairs. However, I expect that a lot of code out there relies on it. Moving set-car! and set-cdr! to a different module like in R6 may be a good first (or second) step. Thanks, Ludo’.