[plt-scheme] Project Euler was Hash Table implementation

From: Mark Engelberg (mark.engelberg at gmail.com)
Date: Fri Aug 3 20:36:42 EDT 2007

On 8/3/07, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
> Mark, why don't you try two different things:
>   1. use the srfis for lazy streams and comprehension to rewrite the
> solutions
>   2. use Lazy Scheme and hack together a comprehension macro.

Actually, I ended up getting satisfactory results by tweaking misc.ss
from the swindle directory.  I chose to do this because I find
swindle's comprehension syntax to be more comfortable than the srfi
version (probably because of its similarity to math/Haskell).

Here are the changes I made:

;;added for stream support
(require (lib "40.ss" "srfi"))

;;added stream comprehension
(defsubst* (stream-of expr clause ...)
    (collect => (_ stream-null (stream-cons expr _)) clause ...))

;;added stream case to collect-iterator so streams can be used in generators
(define* (collect-iterator seq)
  (define (out-of-range r) (lambda (x) (<= r x)))
   [(list? seq) (list seq cdr null? car)]
;;>   * stream: iterate over the stream's element;
;;> This is the new line I added.  Everything else is the same.
   [(stream? seq) (list seq stream-cdr stream-null? stream-car)]
   [(vector? seq) (list 0 add1 (out-of-range (vector-length seq))
                        (lambda (i) (vector-ref seq i)))]
   [(string? seq) (list 0 add1 (out-of-range (string-length seq))
                        (lambda (i) (string-ref seq i)))]
   [(integer? seq) (list 0 add1 (out-of-range seq) #f)]
   [(procedure? seq)
    (function->iterator seq)]
   [(hash-table? seq)
    (collect-iterator (lambda (yield)
                         seq (lambda (k v) (yield (cons k v))))))]
   [else (list seq identity #f #f)]))

I'm pretty happy with this.  The stream srfi library is pretty
minimalist, of course.  I had to add a lot of useful functions like
stream-takewhile and more.

So yes, it's cool that Scheme (and in particular, Eli's code) can be
easily extended in this way.  I would have been even happier if I
didn't need to do it myself :) .


Posted on the users mailing list.