From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Tim Van den Langenbergh Newsgroups: gmane.lisp.guile.user Subject: Re: guile style Date: Sat, 19 Jun 2021 12:25:13 +0200 Message-ID: <1707718.159HRY3vo4@terra> References: <7aeef132-6bd7-c178-5786-c0a3d6b3edc8@nycap.rr.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="37947"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sat Jun 19 12:25:52 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 1luYAi-0009n3-0a for guile-user@m.gmane-mx.org; Sat, 19 Jun 2021 12:25:52 +0200 Original-Received: from localhost ([::1]:51472 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luYAg-0001pR-P0 for guile-user@m.gmane-mx.org; Sat, 19 Jun 2021 06:25:50 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46812) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luYAN-0001p5-HU for guile-user@gnu.org; Sat, 19 Jun 2021 06:25:31 -0400 Original-Received: from mout.gmx.net ([212.227.15.15]:47697) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luYAF-0006vY-8Z for guile-user@gnu.org; Sat, 19 Jun 2021 06:25:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1624098314; bh=Hf5wb7eXe4wKK2dRqCnKD/MJjPdwTAZJtQePwKjkEjY=; h=X-UI-Sender-Class:From:To:Subject:Date:In-Reply-To:References; b=jcz1Nz/rhuRPlqBYlaNo7S8xDut+M+TgW+2ClytRPvNrcWnXcAN3vqwBn8+XiHnZO u01I2keDPy7TCUvI5tKJ+uZiDINLTneHafNE2JDl0aWHvr604IP+YAyZN9f0RdNhLe y/GwfIuNiDsvpLQdlOWRE1/4JJoWwfQpTwh0CHDw= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Original-Received: from terra.localnet ([94.227.125.226]) by mail.gmx.net (mrgmx005 [212.227.17.184]) with ESMTPSA (Nemesis) id 1MkYc0-1lRIl220FS-00lzQE for ; Sat, 19 Jun 2021 12:25:14 +0200 In-Reply-To: <7aeef132-6bd7-c178-5786-c0a3d6b3edc8@nycap.rr.com> X-Provags-ID: V03:K1:f2R+7pfaHvLLwSXdvKLLOGG2+4qeyNtwTTHmxdeifveGm2XfzpI tJZXETH8SOWdo3lUP7NNNDMk0bLk3hvgdgoALEslMyRfeVbI38nTDixnsNmTZjsEMOQ5OL3 pftwmcXH07/WGa1v2Y8GFyf0HEV4FwDsGX8oVMBvMSBckZP1+BUn/is169YT4qZAswWYHZ5 Om+Suz8L6WkPUgV8x1jBQ== X-UI-Out-Filterresults: notjunk:1;V03:K0:kV6s1a8s6XU=:81XWjgwHKF16ORsk8HDeWU ki46CUvkG7RhIlrhwSXQ8w6+ndiqSDPg3Ihw96eXU1FL79aPlsKZL3A90pnqHQ6xG7o2QZ6FG Ap/SZsjJqOEpCt+APrm6MWRZ7siwR+UIQ9zJ+2gFPSE1SYx2mQ6lWbCSldUhDTEOufVz34M8u Rf4iTva6Ismitm4uJMByItLct9AN8IiS2/LAWEQ+EY2tKY1cSYM52Q636I+dWBGRgBffo+VBz N52AUepFW/ejnBRQmFrIvIkztT/Gz6f4NAwy3E6kA9X0VmGWj/zmUEidY8MzGP6seX3IQ+6a8 7OOuTIlqxJQXU3Q9hAElb2T95cCfYiuiRV3gsWJ3+5ILJb+amo8rHFAMlAxSgQAYtdIWeru0a vkinF04nOseyRpyBrGIQ0O17Ax3fIZjp1BjPtkFC4z0o01CmvGyHXw+KM6Zw+bTcrEkd4jazD QnwxBuzuAEa35fTwmzVCCb0pPd1yE79gYpBMaC4P4FFHwYBhJ8rU6G21nmdCMPuNBFetBrCEF SToszfcMrhLmK5pxPCn3kN+wjxlkDX8VET+PY+m28tG+8WrH2xtcdbcSiomFk2BgS8R0Zepuc xdug7AiamDerwBXBXP/CrcKNwu035nLf4cBdnnnBHN4mJRA2B7C7oAZMiyNHXMTCvOsZoSqaU IMwM7APLDFGGcUdO/CCmguqpObmJ/qzexFnyXtnWxeJMunmGvd433UN9KB+j1GuxNE4TNWHJX pzcov2v4kQVt9AMJ4GJSR94vLqtU67yh+9WXEEYz9c+lk5tYxCBel1Piu3+nCEFBSpQIpstv Received-SPF: pass client-ip=212.227.15.15; envelope-from=tmt_vdl@gmx.com; helo=mout.gmx.net X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FROM=0.001, 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:17606 Archived-At: 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 (=3D 0 (modulo x 3)) (=3D 0 (modulo x 5)))) > (iota N)))) > (print ans) > > (define ans 0) > (for i N > (if (or (=3D 0 (modulo i 3)) (=3D 0 (modulo i 5))) (set! ans (+ ans i= )))) > (print ans) > > (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) (+ a= ns 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 se= cond variation poorly optimised. Personally I would use combine the first and t= hird 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