Here is how I would approach this<br><br>(require (lib "40.ss" "srfi"))<br>(require (lib "13.ss" "srfi"))<br><br>(define (stream-list-merge s1 s2 pred)<br> (cond ((stream-null? s1) s2)
<br> ((stream-null? s2) s1)<br> (else<br> (let ((s1car (stream-car s1))<br> (s2car (stream-car s2)))<br> (cond ((pred s1car s2car)<br> (stream-cons s1car (stream-list-merge (stream-cdr s1) s2 pred)))
<br> ((pred s2car s1car)<br> (stream-cons s2car (stream-list-merge s1 (stream-cdr s2) pred)))<br> (else<br> (stream-cons s1car<br> (stream-list-merge (stream-cdr s1)
<br> (stream-cdr s2) pred))))))))<br><br>(define (join-series s1 s2 f order)<br> (let ((e1 (stream-car s1))<br> (e2 (stream-car s2)))<br> (stream-cons (f e1 e2)<br>
(stream-delay <br> (stream-list-merge <br> (stream-map (lambda (e) (f e e2)) (stream-cdr s1))<br> (join-series s1 (stream-cdr s2) f order)<br> order)))))
<br><br>(define (stream-down-from n)<br>
(if (zero? n) stream-null<br>
(stream-cons n (stream-down-from (- n 1)))))<br><br>(define (palindrome? n)<br> (let ((s (number->string n)))<br> (string=? s (string-reverse s))))<br><br>(stream-car (stream-filter palindrome? (join-series (stream-down-from 999) (stream-down-from 999) * >)))
<br><br>Which looks overly complicated, but join-series and stream-list-merge are tools that I had written for other projects. <br><br>All of which leads me to ask two questions. Can someone help me to understand why I need the stream-delay in join-series? I would have thought that stream-cons delayed its second argument.
<br>Also, does anyone what happened to the quasi-proposed srfi-41?<br><br><br><br><br><div><span class="gmail_quote">On 6/17/07, <b class="gmail_sendername">Jens Axel S©ªgaard</b> <<a href="mailto:jensaxel@soegaard.net">
jensaxel@soegaard.net</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Grant Rettke wrote:<br><br>> It works, and it makes sense, but it is sort of tricky/ugly to read in
<br>> that it logically has an inner and outer loop.<br>><br>> (define psum<br>> (¥ë ()<br>> (let ([top 999] [bot 1])<br>> (let loop ([x top] [y top] [max 0])<br>> (let* ([prod (* x y)]
<br>> [str (number->string prod)]<br>> [ispal (string=? str (srfi13:string-reverse str))]<br>> [newmax (if (and ispal (> prod max)) prod max)])<br>> (cond [(> newmax (* x x)) newmax]
<br>> [(= x y bot) newmax]<br>> [(> y bot) (loop x (sub1 y) newmax)]<br>> [else (loop (sub1 x) top newmax)]))))))<br><br>One way is to nest two named loops, and copy the max value from
<br>the outer loop to the inner loop:<br><br>(define (psum)<br> (let ([top 999] [bot 1])<br> (let loopx ([x top] [max 0])<br> (if (= x bot) max<br> (loopx (- x 1)<br> (let loopy ([y x] [max max])
<br> (if (= y bot) max<br> (let* ([prod (* x y)]<br> [str (number->string prod)]<br> [ispal (string=? str<br> (srfi13:string-reverse str))]
<br> [newmax (if (and ispal (> prod max))<br> prod max)])<br> (loopy (- y 1) newmax)))))))))<br><br>But, erm, I think your version is prettier.
<br><br>You might want to look at either (lib "for.ss") or srfi-42.<br>With srfi-42 it becomes:<br><br>(require (lib "42.ss" "srfi"))<br><br>(define (palindromic? s)<br> (equal? (string->list s)
<br> (reverse (string->list s))))<br><br>(max-ec (: x 999 99 -1) ; loop from 999 to 100 with step -1<br> (: y x 99 -1) ; loop from x ti 100 with step -1<br> (if (palindromic? (number->string (* x y))))
<br> (* x y))<br><br>--<br>Jens Axel S©ªgaard<br><br>_________________________________________________<br> For list-related administrative tasks:<br> <a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme">
http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br></blockquote></div><br>