summaryrefslogtreecommitdiff
path: root/problem-4.scm
blob: 3622b5e1a226db65e88273dc2be17b3083e0747b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(use-modules (srfi srfi-1)
             (srfi srfi-42))

(define (products min max)
  (list-ec (:range x min max) (:range y min max) (* x y)))

(define (palindrome? x)
  (define (test s)
    (cond ((or (string-null? s)
               (= (string-length s) 1))
           #t)
          ((equal? (string-ref s 0)
                   (string-ref s (1- (string-length s))))
           (test (substring s 1 (1- (string-length s)))))
          (else
           #f)))
  (let ((s (number->string x)))
    (test s)))

(define (palindrome-products min max)
  (filter palindrome? (products min max)))

(reduce max 0 (palindrome-products 100 1000))