unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: jerry <jdinardo@nycap.rr.com>
To: guile-user@gnu.org
Subject: Re: guile style
Date: Sat, 19 Jun 2021 07:31:53 -0400	[thread overview]
Message-ID: <13e464b0-565e-29b1-8e08-3411c31da26a@nycap.rr.com> (raw)
In-Reply-To: <1707718.159HRY3vo4@terra>

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?



  parent reply	other threads:[~2021-06-19 11:31 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-19  0:55 guile style jerry
2021-06-19 10:25 ` Tim Van den Langenbergh
     [not found]   ` <CAGua6m1HSYGP6i60Qyj=sVAgeriOzZmb1abCtoxnTQQiOgyY-Q@mail.gmail.com>
2021-06-19 11:24     ` Fwd: " Stefan Israelsson Tampe
2021-06-19 11:31   ` jerry [this message]
2021-06-19 11:17 ` Ricardo Wurmus
2021-06-19 11:20 ` Christopher Lam
2021-06-19 12:16   ` jerry
2021-06-19 17:02     ` Zelphir Kaltstahl
2021-06-19 17:59     ` Linus Björnstam
2021-06-20  2:21       ` jerry

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=13e464b0-565e-29b1-8e08-3411c31da26a@nycap.rr.com \
    --to=jdinardo@nycap.rr.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).