guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: guile style


From: Christopher Lam
Subject: Re: guile style
Date: Sat, 19 Jun 2021 19:20:06 +0800

Agree set! is not a desirable form. It is not consistently optimisable. I
cannot find the reference in the manual.

Also consider the first form: you're building a list in 3 passes -- call
iota to generate a list, call filter to navigate the list again, then fold
to accumulate your answer. Therefore it's O(3N).

The preferred form is definitely from the little schemer.

On Sat, 19 Jun 2021, 8:56 am jerry, <jdinardo@nycap.rr.com> 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)) )))
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]