unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Stefan Israelsson Tampe <stefan.itampe@gmail.com>
To: Guile User <guile-user@gnu.org>, guile-devel <guile-devel@gnu.org>
Subject: Fwd: guile style
Date: Sat, 19 Jun 2021 13:24:40 +0200	[thread overview]
Message-ID: <CAGua6m01jbc3AUHhEDhDL8vkHqSONs4nf3hW6-M2BFDrAnrdEA@mail.gmail.com> (raw)
In-Reply-To: <CAGua6m1HSYGP6i60Qyj=sVAgeriOzZmb1abCtoxnTQQiOgyY-Q@mail.gmail.com>

---------- Forwarded message ---------
From: Stefan Israelsson Tampe <stefan.itampe@gmail.com>
Date: Sat, Jun 19, 2021 at 1:23 PM
Subject: Re: guile style
To: Tim Van den Langenbergh <tmt_vdl@gmx.com>


I'm a big fan of named let's which are a very general functional construct
and tons of commentaries
about the functional style misses that named let's exists and thinks that
functional programming is just
map, fold, filter etc. It beats me why when other languages go functional,
they consistently do not
implement named let.

On Sat, Jun 19, 2021 at 12:25 PM Tim Van den Langenbergh <tmt_vdl@gmx.com>
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
>
>
>
>


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

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <CAGua6m1HSYGP6i60Qyj=sVAgeriOzZmb1abCtoxnTQQiOgyY-Q@mail.gmail.com>]

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=CAGua6m01jbc3AUHhEDhDL8vkHqSONs4nf3hW6-M2BFDrAnrdEA@mail.gmail.com \
    --to=stefan.itampe@gmail.com \
    --cc=guile-devel@gnu.org \
    --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).