From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Israelsson Tampe Newsgroups: gmane.lisp.guile.user,gmane.lisp.guile.devel Subject: Fwd: guile style Date: Sat, 19 Jun 2021 13:24:40 +0200 Message-ID: References: <7aeef132-6bd7-c178-5786-c0a3d6b3edc8@nycap.rr.com> <1707718.159HRY3vo4@terra> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="28826"; mail-complaints-to="usenet@ciao.gmane.io" To: Guile User , guile-devel Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sat Jun 19 13:25:26 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 1luZ6L-0007K6-Oe for guile-user@m.gmane-mx.org; Sat, 19 Jun 2021 13:25:25 +0200 Original-Received: from localhost ([::1]:55288 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luZ6K-0002YC-Pi for guile-user@m.gmane-mx.org; Sat, 19 Jun 2021 07:25:24 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:52766) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luZ5t-0002Xq-1Z; Sat, 19 Jun 2021 07:24:57 -0400 Original-Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]:38857) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luZ5r-0007j4-7T; Sat, 19 Jun 2021 07:24:56 -0400 Original-Received: by mail-pl1-x631.google.com with SMTP id 69so6022159plc.5; Sat, 19 Jun 2021 04:24:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=A9iBeEwqoiGhi6pVJsNfBJfCchdy/W/jDzi1vErmyaQ=; b=j8HSPxPxZxgYbpi2uhETjfSGU1CoirfZEgoNVFglrDuurCiTzx9627wQSv/w+yY8tS v1Zeo537kG6Bu8E10OOaK4Fzpfs87AhQpufOMf2RkmCBt1SRu3yVgNLDUB99xsC+2KXT 75zlNrJ+m2CMO8MJ9GUz1enpj736zjTHxVWZW9NtqiYEL9gP0YipqbyQBQo52kUthFu9 yUhBRUdFWYuolVcevktUu2qIe8FWzWhKKp9aQGO+Bsml4XdzGNkhpvei8k+meVnClDQe W07zKrk+NAXhRUe0I/mZik7EY+VTggaDGllR41gCqKhSBm4m1eyib7nG9G6D0VgcmUvz FZFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=A9iBeEwqoiGhi6pVJsNfBJfCchdy/W/jDzi1vErmyaQ=; b=Mthr9szgWebG2KNDtZTMZLVMygpqcvWZwXDxGFSueBont/tc6ud9pcxAvQJb2Tl9F2 BStO6+/Bg1KkVsV3xYldU3g/i4zVhMiqa5RA29yYeuG3OQCdmxt1Mp/U+cYxKpaK3ml7 tILUhzIOzDiaGd0ac6Uk6RQNmsfpNdHORmCqpmXP3ZY9d4BraK5XCzvlKNeZKPJ5hC0f KCUBEnYKWgBAKjTcQ04OBb9d/b/SQ6iCZh/+7X+K1LOMBWy9GizZD8azn+wZgayQV3B/ GDRWgzr1BT6uCzzY0YxH8fJuhMBAkwO5XZ9YZ9iuWim8yrfRrMHePDx9pSvpo/wXjWpr p+bA== X-Gm-Message-State: AOAM533L192CTjm8hRzl/fJut3oDLMpk4la5Nwm6j2duuRiDfIlosGus gJqipOxRu8yu4El9zgzd9OO336LjN3Pi3WILST2leCCV X-Google-Smtp-Source: ABdhPJy9nUDvn/MneJXIjlky2bO5VInOW5Uxw/HIjXzZ5u295tYyOhCT5dmrHwrE4Sm0ia1oTbsBqolj0NXmPsn8rzc= X-Received: by 2002:a17:902:e891:b029:10e:8977:8d68 with SMTP id w17-20020a170902e891b029010e89778d68mr9148881plg.39.1624101891515; Sat, 19 Jun 2021 04:24:51 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::631; envelope-from=stefan.itampe@gmail.com; helo=mail-pl1-x631.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 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:17609 gmane.lisp.guile.devel:20785 Archived-At: ---------- Forwarded message --------- From: Stefan Israelsson Tampe Date: Sat, Jun 19, 2021 at 1:23 PM Subject: Re: guile style To: Tim Van den Langenbergh I'm a big fan of named let's which are a very general functional construct and tons of commentaries about the functional style misses that named let's exists and thinks that functional programming is just map, fold, filter etc. It beats me why when other languages go functional, they consistently do not implement named let. On Sat, Jun 19, 2021 at 12:25 PM Tim Van den Langenbergh wrote: > On Saturday, 19 June 2021 02:55:34 CEST jerry wrote: > > I am fairly new to guile and scheme. People tell me that I should use a > > functional style. > > > > I have 3 solutions for project euler problem #1. The first is > > functional, the second is imperative and the third is written in "Little > > Schemer" style. > > > > I was hoping other guile users would comment on preferences or the > > "correct way". Sorry in advance for any wrapping problems that may occur. > > > > #!/usr/local/bin/guile -s > > !# > > (use-modules (srfi srfi-1) (jpd stdio)) ;; for folds > > (define N 1000) > > > > (define ans > > (fold + 0 > > (filter > > (lambda (x) (or (= 0 (modulo x 3)) (= 0 (modulo x 5)))) > > (iota N)))) > > (print ans) > > > > (define ans 0) > > (for i N > > (if (or (= 0 (modulo i 3)) (= 0 (modulo i 5))) (set! ans (+ ans i)))) > > (print ans) > > > > (define ans > > (let loop ((i 1) (ans 0)) > > (cond > > ((>= i N) ans) > > ((or (= 0 (modulo i 3)) (= 0 (modulo i 5))) (loop (1+ i) (+ ans > i))) > > (else (loop (1+ i) ans)) ))) > > I'm not 100% sure about how Guile does it, but I know that some Scheme > implementations do some boxing for set! operations, which will make the > second > variation poorly optimised. Personally I would use combine the first and > third > answers by doing the divisible-by check during the fold, like this: > > (use-modules (srfi srfi-1)) > > (define (divisible-by? divident divisor) > ~~(zero? (modulo divident divisor))) > > (define N 1000) > > (define ans > ~~(fold (lambda (i res) > ~~~~~~~~~~(if (or (divisible-by? i 3) > ~~~~~~~~~~~~~~~~~~(divisible-by? i 5)) > ~~~~~~~~~~~~(+ i res) > ~~~~~~~~~~~~res)) > ~~~~~~~~0 > ~~~~~~~~(iota N))) > > Vale, > > -Tim > > > >