unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* guile style
@ 2021-06-19  0:55 jerry
  2021-06-19 10:25 ` Tim Van den Langenbergh
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: jerry @ 2021-06-19  0:55 UTC (permalink / raw)
  To: guile-devel-confirm+bec536c361a61d32a192d9a45ad6dea4f892df58,
	guile-user

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)) )))



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-06-20  2:21 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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).