From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Kjetil Matheussen Newsgroups: gmane.lisp.guile.user Subject: Re: guile studio Date: Sat, 19 Jun 2021 14:13:57 +0200 Message-ID: References: Reply-To: k.s.matheussen@notam02.no 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="24955"; 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 15:03:51 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 1luadb-0006P7-8c for guile-user@m.gmane-mx.org; Sat, 19 Jun 2021 15:03:51 +0200 Original-Received: from localhost ([::1]:56262 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luada-0006XE-9y for guile-user@m.gmane-mx.org; Sat, 19 Jun 2021 09:03:50 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59182) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luZrY-00050E-DP for guile-user@gnu.org; Sat, 19 Jun 2021 08:14:12 -0400 Original-Received: from mail-qk1-x72d.google.com ([2607:f8b0:4864:20::72d]:40645) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luZrW-0006an-IX for guile-user@gnu.org; Sat, 19 Jun 2021 08:14:12 -0400 Original-Received: by mail-qk1-x72d.google.com with SMTP id q64so11629069qke.7 for ; Sat, 19 Jun 2021 05:14:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:reply-to:from:date:message-id :subject:to; bh=SdvhXgUvOAweDCBcS13DS/PQb6Z20BxNXaroUUonHS8=; b=r5uZV9QixHF+hao2vaB04IVMoG1beBj3E9ZNpTV3eXVswZcKfJqKOH63AS3T9ieHFY x/RhiV3S4iosLwOL7Pq0RLU1gCx4xwbsCAQgNtlHcWJXpYeiW4KUQNWk2HyUbe11NNco 290mpcWidPstwN1AsB0hJneOlzziz95BdTed9UDK+piP8zMkjWmO28Ux57AhjvTly57I Ihtw4V3ewNSzmHF4kEj8i1tU4t+TT8/u1EZjpgzsXXjCLwVzRFhmeCrMyM9zTiYpkM4W EWkRVhQ3g2N6zBARn9cRzePfttNmHLOW62MmKpKVZX9DjjYKWkRdagCXosYg4NvVq2LV LA3A== 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:reply-to :from:date:message-id:subject:to; bh=SdvhXgUvOAweDCBcS13DS/PQb6Z20BxNXaroUUonHS8=; b=cx9J62CW42D8UaEhB7etsNPgQFp+pwT0bqpDCEGnKNdm/inhfkvf3hYPo6zYvZ5c9h 6vGK4559A1SrGaLXXRhVd0BCpIeIqyrxnHLe3dQf3O3hvap64E1iH5zkBYT/eiPvh9Rw fFs8lde9Bz51pm+WPM2aOCMSjF3aK3EYLauXxc1SJTOr1f1bGCvbmT3f/zSc+btNeiWi c7Fov5Lv6ZnxziJUcATtO8+NOBYDrfTLvI4ntEabxt24HF7mzSEy4tQS53Z8no3rpb1k bwy6tg0WmH/uYwa47sJbid+7dmDPXWGc9Fd7rm3tINykNSuC07NgKlRw7tTxIcsJ795w 4mSA== X-Gm-Message-State: AOAM533tZ2r6Oa3ryK5o0kOMu7Z6hXRp3Wm9V8F3XYvUWKCJkOymbXa+ Uk88N42g6hC81e7iWVq0Jfi/bOPol4Vl9XJIEITZcW64J3U= X-Google-Smtp-Source: ABdhPJwSB13J+C6xLGpcfbg0bTMl232uhr6XsoleMJGVQ6bSY5ryVYCbb5AiFjWFKqlimyGLEYRT0zCk+CP1cmqwaQU= X-Received: by 2002:a25:aac7:: with SMTP id t65mr18866328ybi.501.1624104848843; Sat, 19 Jun 2021 05:14:08 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::72d; envelope-from=k.s.matheussen@gmail.com; helo=mail-qk1-x72d.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, 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-Mailman-Approved-At: Sat, 19 Jun 2021 09:03:29 -0400 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:17612 Archived-At: > From: jerry > > 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) > For minor calculations, I would say this is fine. It's not hard to understand what this function does. However, the more complicated a function is, the harder this style will be to read, compared to a recursive style (example 3). So in general I would recommend never to use fold, but that's probably somewhat a matter of taste. Personally I never use fold. > (define ans 0) > (for i N > (if (or (= 0 (modulo i 3)) (= 0 (modulo i 5))) (set! ans (+ ans i)))) > (print ans) > Same here. For minor calculations, this is fine. In fact, this way is probably much easier to read than the first example. However, if you start using 'set!' on more than one variable, things can get very messy. Regarding performance, this might be both faster or slower depending on the scheme implementation. > (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)) ))) > Please note that this way is also functional. This way is also more efficient than example #1 since the program can do tail call optimization and doesn't have to allocate lists. In addition, if you had formatted this version properly I would have consider this version to be easier to read than example #1. Also note that if N is not too big (so that tail call optimization doesn't matter), the following version would be even simpler: (define ans (let loop ((i 1)) (cond ((>= i N) 0) ((or (= 0 (modulo i 3)) (= 0 (modulo i 5))) (+ i (loop (1+ i)))) (else (loop (1+ i)))))) Training yourself to read and write functions this way is probably a good exercise. After a while you are able to easily make very advanced functions by using recursive functions. Also note that using less number of lines in a function does not make the function easier to read.