# [racket] spirit of Racket?

 From: Joe Gilray (jgilray at gmail.com) Date: Sun Mar 11 16:12:36 EDT 2012 Previous message: [racket] accessing serial communications on Racket/Windows?... Next message: [racket] spirit of Racket? Messages sorted by: [date] [thread] [subject] [author]

```Hi,  I'm redoing the ProjectEuler problems to learn Racket (great fun!).
Below are three solutions to PE#4.  Which is most in the spirit of Racket?
Of course, I'd love to see solutions that are more idiomatic too.  Is
there a better way that doesn't use "for" at all?

Thanks!
-Joe

; function that turns a positive integer into a list of digits (in reverse
order)
; invoke as (listize 234511) or (set-count (list->set (listize 112342)))
(define (listize num)
(if (= num 0)
'()
(cons (remainder num 10) (listize (quotient num 10)))))

; function that returns #t if the passed number is palindromic
; invoke as (palindromic? n)
(define (palindromic? n)
(if (equal? (listize n) (reverse (listize n))) #t #f))
)

; ProjectEuler problem #4
(define (euler4a)
(let ([max 100000])
(for ([i (build-list 899 (lambda (x) (+ 100 x)))])
(for ([j (build-list (- 999 i) (lambda (y) (+ i y)))])
(let ([prod (* i j)])
(when (palindromic? prod)
(begin
(when (> prod max) (set! max prod))
(printf "~a * ~a = ~a~n" i j prod))))))
(printf "Max palindromic product is ~a~n" max)))

; ProjectEuler problem #4 using for*
(define (euler4b)
(let ([max 100000])
(for* ([i (build-list 899 (lambda (x) (+ 100 x)))]
[j (build-list 899 (lambda (y) (+ 100 y)))]
#:when (and (>= j i) (palindromic? (* i j))))
(let ([prod (* i j)])
(when (> prod max) (set! max prod))
(printf "~a * ~a = ~a~n" i j prod)))
(printf "Max palindromic product is ~a~n" max)))

; ProjectEuler problem #4 - a mix of 4a and 4b
(define (euler4c)
(let ([max 100000])
(for* ([i (build-list 899 (lambda (x) (+ 100 x)))]
[j (build-list (- 999 i) (lambda (y) (+ i y)))]
#:when (palindromic? (* i j)))
(let ([prod (* i j)])
(when (> prod max) (set! max prod))
(printf "~a * ~a = ~a~n" i j prod)))
(printf "Max palindromic product is ~a~n" max)))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120311/d1d4cbe7/attachment.html>
```

 Posted on the users mailing list. Previous message: [racket] accessing serial communications on Racket/Windows?... Next message: [racket] spirit of Racket? Messages sorted by: [date] [thread] [subject] [author]