From d665c637c9877a120e8affd020fb754874250361 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sun, 2 Feb 2014 16:01:59 -0500 Subject: Solve Problem 4. --- problem-4.scm | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 problem-4.scm diff --git a/problem-4.scm b/problem-4.scm new file mode 100644 index 0000000..3622b5e --- /dev/null +++ b/problem-4.scm @@ -0,0 +1,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)) -- cgit v1.2.3