From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ricardo Wurmus Newsgroups: gmane.lisp.guile.user Subject: Re: guile style Date: Sat, 19 Jun 2021 13:17:02 +0200 Message-ID: <8735tenlxt.fsf@elephly.net> References: <7aeef132-6bd7-c178-5786-c0a3d6b3edc8@nycap.rr.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32605"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.4.15; emacs 27.2 Cc: guile-devel-confirm+bec536c361a61d32a192d9a45ad6dea4f892df58@gnu.org, guile-user@gnu.org To: jerry Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sat Jun 19 13:17:41 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 1luYyp-0008Iz-P8 for guile-user@m.gmane-mx.org; Sat, 19 Jun 2021 13:17:40 +0200 Original-Received: from localhost ([::1]:53610 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luYyo-0000sm-L3 for guile-user@m.gmane-mx.org; Sat, 19 Jun 2021 07:17:38 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:52022) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luYyT-0000rv-7A; Sat, 19 Jun 2021 07:17:17 -0400 Original-Received: from sender4-of-o51.zoho.com ([136.143.188.51]:21119) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luYyR-00048U-1S; Sat, 19 Jun 2021 07:17:16 -0400 ARC-Seal: i=1; a=rsa-sha256; t=1624101427; cv=none; d=zohomail.com; s=zohoarc; b=cQK656i7iIlC72O4QHy8GE4YHVU0A9+PIPo3+M9uyaIfFWmBpbyfruBL4dVjQUD2lzWprjpPuQqgMMStdDLp7Xn7w1+kEQtTrx8gKGDIfDmgEarZXs99cejDXXMstjr0zs6lDs5QPrxWOZDd1sgfx7XG5V6VT9vcCMjJjQbQ1aA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1624101427; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=U9KkjKGsruDbfU7YSq+HIegmOuuzyDGKevUcH4p/VUw=; b=mB3/FT42sQ/P4czQg8XZwFMXtjEcv/GJIFSM7L0GRSaBVJQ0RnrDtD8szpLKqaN0Hf9gUEimXkyiEuQXg2XnNqkC1BZcJ3kylsVIs6NpfsdyBNjfiuAW11Lniy5S84XwD1CtDIEphhoWSBb7wj+KWXuwh/pJ6ZC0fRQx40SXT70= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=elephly.net; spf=pass smtp.mailfrom=rekado@elephly.net; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1624101427; s=zoho; d=elephly.net; i=rekado@elephly.net; h=References:From:To:Cc:Subject:In-reply-to:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=U9KkjKGsruDbfU7YSq+HIegmOuuzyDGKevUcH4p/VUw=; b=M0c+uxXcqCVe6zJO9eFIRdv6pci8qqf7zp8Z+7KuhoTvFs7ZJalSn+H4ReprduMB iobaTRqX04zPJ1XpCLpy0LRYwpC04R62tw2VvsaNUytgJewqxuXoaZY9FqyPbfGQzrK Qc/8mgyNBn4CQMyauqganGebiM5Qa/pQrK95FWvs= Original-Received: from localhost (p4fd5a0b3.dip0.t-ipconnect.de [79.213.160.179]) by mx.zohomail.com with SMTPS id 1624101426258890.550093842573; Sat, 19 Jun 2021 04:17:06 -0700 (PDT) In-reply-to: <7aeef132-6bd7-c178-5786-c0a3d6b3edc8@nycap.rr.com> X-URL: https://elephly.net X-PGP-Key: https://elephly.net/rekado.pubkey X-PGP-Fingerprint: BCA6 89B6 3655 3801 C3C6 2150 197A 5888 235F ACAC X-ZohoMailClient: External Received-SPF: pass client-ip=136.143.188.51; envelope-from=rekado@elephly.net; helo=sender4-of-o51.zoho.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, RCVD_IN_DNSWL_NONE=-0.0001, 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.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:17607 Archived-At: Hi Jerry, > I am fairly new to guile and scheme. People tell me that I=20 > 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=20 > the > "correct way". Sorry in advance for any wrapping problems that=20 > 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 (=3D 0 (modulo x 3)) (=3D 0 (modulo x 5)))) > (iota N)))) > (print ans) This is fine, though instead of (=3D 0 =E2=80=A6) you could use (zero? =E2= =80=A6). Using =E2=80=9Cfold=E2=80=9D is good because it is a common higher-order=20 abstraction, so it is easy to read and understand at a glance. > (define ans 0) > (for i N > (if (or (=3D 0 (modulo i 3)) (=3D 0 (modulo i 5))) (set! ans (+=20 > ans i)))) > (print ans) This is not idiomatic for two reasons: it uses SET! and a=20 single-branched IF. I have never before encounter FOR in Guile=20 code. A =E2=80=9Cnamed let=E2=80=9D (as in your next variant) is much more= =20 common. > (define ans > (let loop ((i 1) (ans 0)) > (cond > ((>=3D i N) ans) > ((or (=3D 0 (modulo i 3)) (=3D 0 (modulo i 5))) (loop (1+ i)=20 > (+ ans i))) > (else (loop (1+ i) ans)) ))) This is explicit, which is fine, but for routine tasks like=20 accumulation of results a fold is easier to understand at a=20 glance. --=20 Ricardo