summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2014-02-02 16:01:59 -0500
committerDavid Thompson <dthompson2@worcester.edu>2014-02-02 16:01:59 -0500
commitd665c637c9877a120e8affd020fb754874250361 (patch)
treebc9b19eb236110bd207c19930a44cc10700fd3fe
parentf211fe46db8eba59efbdefad8fe2ec2861921bae (diff)
Solve Problem 4.
-rw-r--r--problem-4.scm23
1 files changed, 23 insertions, 0 deletions
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))