[plt-scheme] Project Euler was Hash Table implementation

From: David Einstein (deinst at gmail.com)
Date: Fri Aug 3 21:08:46 EDT 2007

I do not know if you know, but there are a lot of stream utilities in
the initial proposal of srfi-40, that were going to be moved to
srfi-41, but I assume that the author lost interest before that
happened.

On 8/3/07, Mark Engelberg <mark.engelberg at gmail.com> wrote:
> 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)))
>   (cond
>    [(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)
>                         (hash-table-for-each
>                          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 :) .
>
> --Mark
>


Posted on the users mailing list.