From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Eric Abrahamsen Newsgroups: gmane.emacs.devel Subject: Re: Help with recursive destructive function Date: Sun, 06 May 2018 10:29:39 -0700 Message-ID: <87sh743d6k.fsf@ericabrahamsen.net> References: <87efiqzzd2.fsf@ericabrahamsen.net> <87bmdu3mtf.fsf@web.de> <87zi1e9kju.fsf@web.de> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1525627860 24719 195.159.176.226 (6 May 2018 17:31:00 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 6 May 2018 17:31:00 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: Stefan Monnier , emacs-devel@gnu.org To: Michael Heerdegen Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun May 06 19:30:55 2018 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fFNUs-0006KL-DW for ged-emacs-devel@m.gmane.org; Sun, 06 May 2018 19:30:54 +0200 Original-Received: from localhost ([::1]:42824 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fFNWz-0000P2-HP for ged-emacs-devel@m.gmane.org; Sun, 06 May 2018 13:33:05 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48889) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fFNWN-0000Ow-M9 for emacs-devel@gnu.org; Sun, 06 May 2018 13:32:28 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fFNWJ-0005h9-MS for emacs-devel@gnu.org; Sun, 06 May 2018 13:32:27 -0400 Original-Received: from mail.ericabrahamsen.net ([50.56.99.223]:44788) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fFNWJ-0005gh-Is for emacs-devel@gnu.org; Sun, 06 May 2018 13:32:23 -0400 Original-Received: from localhost (174-21-181-71.tukw.qwest.net [174.21.181.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) (Authenticated sender: eric@ericabrahamsen.net) by mail.ericabrahamsen.net (Postfix) with ESMTPSA id 7CF15C158F; Sun, 6 May 2018 17:32:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mail.ericabrahamsen.net; s=mail; t=1525627942; bh=lc+/Uc7+3clgKcy+uDoFCBuRDElBYLnx5cw4rv8Jxs8=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=ObXszQaQffSyc5P4nMcMtvW3HaUm+fRyM+xhKADfrwzqPJdgD+nzEn06JMTM2D5kL wxSOWR2dRwhyBTfmzLrV74pgKaAXzDxU4ioye8zub/oWMQBii6HVEJ3jkqZVdjNXzL pqECdZ8B97ZSyl912ZyBKFotNfPkbunjr/MFpNvA= In-Reply-To: <87zi1e9kju.fsf@web.de> (Michael Heerdegen's message of "Sat, 05 May 2018 17:41:41 +0200") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 50.56.99.223 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:225088 Archived-At: Michael Heerdegen writes: > I wrote: > >> I guess I would use an iterator: the definition would still looks >> recursive, but the execution isn't problematic any more if done right. Thanks to both of you! This was some good food for thought. I made Stefan's suggested change to my original function and it works fine. It still looks ugly to me because I'm doing the same test-and-set in three different places, but with sufficient poking I can probably get it all inside the same loop. All else being equal I prefer this more "basic" version, simply because I understand everything that's happening in it. I haven't used `cl-loop' before, but I assume it's not doing anything that (while (consp thing) ... (setq thing (cdr thing)) Isn't doing? Oh, but then you wouldn't be able to use cl-callf directly on thing. Recursion is an issue, but the original version recurses on car, not cdr, which I think (?) is much less of a problem. It went through your huge-list with no trouble (and faster than the iterative version). I suppose someone might have accumulated 801 levels of nested quotes in their Gnus registry (god, I hope not), but otherwise I'm not sure it's a worry. On the third hand, if "bulletproof" is the goal, maybe it's best not to risk it... > #+begin_src emacs-lisp > (iter-defun iter-tree-example (tree) > (cl-loop for thing in-ref tree by #'cdr do > (if (consp thing) > (iter-yield-from (iter-tree-example thing)) > (iter-yield > (ignore > (when (stringp thing) > (cl-callf upcase thing))))))) > Yield values don't have any purpose but I guess without yielding you > would get no CPS rewrite but a standard recursive function that would be > problematic with the HUGE-LIST. I guess this works because the calls to `iter-yield' and `iter-yield-from' fully return from the function? Also, what does "CPS rewrite" mean? Thanks again, Eric