From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Zelphir Kaltstahl Newsgroups: gmane.lisp.guile.user Subject: Re: why I love scheme Date: Wed, 15 Dec 2021 09:38:41 +0000 Message-ID: <7faa8b38-4e28-9926-4842-9c0d005bae85@posteo.de> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="37369"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Guile User To: Stefan Israelsson Tampe Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Wed Dec 15 10:39:04 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mxQka-0009Wk-M6 for guile-user@m.gmane-mx.org; Wed, 15 Dec 2021 10:39:04 +0100 Original-Received: from localhost ([::1]:43922 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mxQkY-0007K9-Ng for guile-user@m.gmane-mx.org; Wed, 15 Dec 2021 04:39:02 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:53142) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mxQkN-0007Jk-7f for guile-user@gnu.org; Wed, 15 Dec 2021 04:38:51 -0500 Original-Received: from mout01.posteo.de ([185.67.36.65]:44335) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mxQkK-0006hJ-6y for guile-user@gnu.org; Wed, 15 Dec 2021 04:38:50 -0500 Original-Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id F0C1124002A for ; Wed, 15 Dec 2021 10:38:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1639561123; bh=Ie4ZFSHFVGv5GVxkdqjufH3l2vCln8dQDe6rC+6UiLI=; h=Subject:To:Cc:From:Date:From; b=oAGIlLANYBZqKGCuGHigzclRbDJTjoV4M6RIUUWA+5YQXb/ajt5Y02CZnfYx6B6j1 BRHisiWasPrwFzd2dsJDoj8KvN95IAC6ZPMuBBXYHKzwFZlpIMSBKH5UUCk3hijw4K OgGk7/5uOZO+zG0CyGY08u8/yPAIgDa95Wyzwxd2Pq0wmPH64uZYtbm8NXDDIq5Ds2 +f62y246vLB2yXc325JryMOUrQp8bXt3/RkYS05VNuUshvGZLqCnNUcsbRkhxb7OD+ 7H6VlJKVwP3WsyHNYKzDaQ4Bx+MSpxRICOm3hLMjGocsz2z7Z+uIEuh7/wGwLRwUrN IewjzX5mH8axw== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4JDVYZ2j0Hz9rxW; Wed, 15 Dec 2021 10:38:42 +0100 (CET) In-Reply-To: Content-Language: en-US Received-SPF: pass client-ip=185.67.36.65; envelope-from=zelphirkaltstahl@posteo.de; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17854 Archived-At: Hello Stefan! This translates a recursive tree function into a tail recursive function. However, in this case, I am not sure it is really worth doing, in comparison to the naive (+ first-branch other-branch) solution. The reason is, that instead of a call stack for multiple branches, you are only moving that stuff into a longer and longer continuation, which will be on the stack in each recursive call. However, I think you or other people on the list probably know more about this than I do and about how the approaches compare in terms of memory and time. Maybe the stack frames are more in size than the memory consumed by the overhead of the continuation, or the other way around. Regards, Zelphir On 12/15/21 12:44 AM, Stefan Israelsson Tampe wrote: > Maybe you think the below program is trivial, but I adore named let's so > much that I just cannot fathom that when people go functional they totally > miss this beauty > > > (define (count tree) > > ;; s = total sum up to now > > ;; t = tree of the type (car = child . cdr = siblings) > > ;; cont is the continuation, (cont 10) will continue > > ;; the calculation with the sum=10 see how we initiate > > ;; with a continuation that evaluates returns it's argument > > > (let loop ((s 0) (t tree) (cont (lambda (s) s))) > > (if (pair? t) > > (loop s (car t) (lambda (s) (loop s (cdr t) cont))) > > (cont (if (number? t) t 0)))) -- repositories: https://notabug.org/ZelphirKaltstahl