[plt-scheme] v4 questions

From: Doug Orleans (dougorleans at gmail.com)
Date: Sun Mar 30 13:16:05 EDT 2008

Mark Engelberg writes:
 > I've been looking through the preview reference:
 > http://pre.plt-scheme.org/docs/html/reference/index.html for v4.0, and
 > have a few comments/questions.
 > 1. Why no streams?  Wouldn't it be nice to have a solid stream
 > implementation with namings that are consistent with the PLTScheme
 > list functions, and integrated into the comprehension syntax, i.e.,
 > for/stream ?  Please consider this...

I kind of agree, but this would also be pretty simple to do as a
PLaneT package.  Here's a start:

  (require srfi/40)
  (require scheme/control)

  (define-syntax (for/stream stx)
    (syntax-case stx ()
      ((_ (clause ...) body0 body1 ...)
       #`(let loop ((pos (prompt
			    (for/fold/derived #,stx () (clause ...)
			      (control k (cons k (begin body0 body1 ...))))
	   (if pos
	       (stream-cons (cdr pos) (loop (prompt ((car pos)))))

  (define (in-stream s)
     (lambda ()
       (values stream-car stream-cdr s stream-pair? void void))))

> (for/list ((x (in-stream (for/stream ((x '(1 2 3))) x)))) x)
(1 2 3)

It would be nice if "sequence?" returned true for streams, though.
I don't see any way to do that currently.

 > 4. I noticed that the built-in list functions and string functions are
 > a tiny bit beefier than v372, but still fall short from the more
 > complete SRFI implementations.  I hope that it will be extremely easy
 > to bring in the missing functions without conflict.  In v372 it's a
 > bit awkward to get a full suite of string and list functions because
 > of conflicts.

As of a week or two ago, you can do the following and not get any

  #lang scheme
  (require srfi/1)
  (require srfi/13)

In other words, extremely easy!

 > 6. It would be nice if assoc and its kin could take an optional
 > default lookup value, like hash-table-get.

Unlike hash-table-get, assoc returns the whole association, i.e. the
key/value pair, instead of just the value.  What you want is alist-get:

  (define (alist-get alist key (default #f))
    (let ((pair (assoc key alist)))
      (if pair (cdr pair) default)))

--dougorleans at gmail.com

Posted on the users mailing list.