From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: jerry Newsgroups: gmane.lisp.guile.user Subject: Re: guile style Date: Sat, 19 Jun 2021 07:31:53 -0400 Message-ID: <13e464b0-565e-29b1-8e08-3411c31da26a@nycap.rr.com> References: <7aeef132-6bd7-c178-5786-c0a3d6b3edc8@nycap.rr.com> <1707718.159HRY3vo4@terra> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19750"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sat Jun 19 13:32:39 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 1luZDL-00052L-QR for guile-user@m.gmane-mx.org; Sat, 19 Jun 2021 13:32:39 +0200 Original-Received: from localhost ([::1]:56936 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luZDK-0004B5-Og for guile-user@m.gmane-mx.org; Sat, 19 Jun 2021 07:32:38 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:53498) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luZCj-0004AS-T6 for guile-user@gnu.org; Sat, 19 Jun 2021 07:32:02 -0400 Original-Received: from impout004aa.msg.chrl.nc.charter.net ([47.43.20.28]:42263 helo=impout004.msg.chrl.nc.charter.net) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luZCi-0002x4-6X for guile-user@gnu.org; Sat, 19 Jun 2021 07:32:01 -0400 Original-Received: from [192.168.1.8] ([67.241.26.17]) by cmsmtp with ESMTPA id uZCblYyVVNcdsuZCglNzC6; Sat, 19 Jun 2021 11:31:58 +0000 Authentication-Results: nycap.rr.com; none X-Authority-Analysis: v=2.4 cv=Pfg6Ogtd c=1 sm=1 tr=0 ts=60cdd5ae a=6kFUCgT7isY2CrseCyFLdg==:117 a=6kFUCgT7isY2CrseCyFLdg==:17 a=IkcTkHD0fZMA:10 a=KnthfLIQZ2K8SoCy0iMA:9 a=QEXdDO2ut3YA:10 In-Reply-To: <1707718.159HRY3vo4@terra> Content-Language: en-US X-CMAE-Envelope: MS4xfF3g/ManaJW0mSs5QT0+gNZ9jCvbdJQzAjB2oZhH2GmuRIper+m8hJhDm7/OG5uEHaQWgIBRBEsb6yraqaIlelnPcuwz+f21gUR4I9S/4cU7XA3axgYH sxDa2S7olv5YmEXLCqAX/P+pc7/VntKxfNFyeEEGrpJYjqa56EM7FVcKLEGG0bzT1X/xOdsiz7T8pw== Received-SPF: softfail client-ip=47.43.20.28; envelope-from=jdinardo@nycap.rr.com; helo=impout004.msg.chrl.nc.charter.net X-Spam_score_int: -13 X-Spam_score: -1.4 X-Spam_bar: - X-Spam_report: (-1.4 / 5.0 requ) BAYES_00=-1.9, NICE_REPLY_A=-0.202, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_SOFTFAIL=0.665 autolearn=no 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:17610 Archived-At: On 6/19/21 6:25 AM, 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 > > > I like the functional style best for problems like this which lend themselves to a functional solution. I took up Haskell a couple of years ago and I did the first 80 project Euler problems with it. About 10 percent of the problems would have been solved much easier with imperative style (at least for me). That is why I started learning lisp. I found I liked scheme better than common lisp. What I was really wondering is when, if ever do schemers use imperative style. Is there a book or article that would illustrate this?