[plt-scheme] map-sequence using make-do-sequence and define-sequence-syntax

From: Doug Orleans (dougorleans at gmail.com)
Date: Sun Mar 16 18:22:14 EDT 2008

It seems like the docs for make-do-sequence are out of date.  Here's
what they currently say:

  The thunk results define the generated elements as follows:

    * The first result is a next-pos procedure that takes the current
      position and returns the next position.
    * The second result is a pos->element procedure that takes the
      current position and returns the value(s) for the current
      element. It is called only once per position.
    * The third result is the initial position.
    * The fourth result takes the current element value(s) and returns
      a true result if the sequence includes the value, and false if
      the sequence should end instead of including the value.
    * The fifth result is like the fourth result, but it determines a
      sequence end after the current element is already included in
      the sequence.
    * The sixth result is like the fourth result, but it takes both
      the current position and the current element value(s).

I think it should say something like this (please correct me if I'm
wrong):

  The thunk results define the generated elements as follows:

    * The first result is a pos->element procedure that takes the
      current position and returns the value(s) for the current
      element. It is called only once per position.
    * The second result is a next-pos procedure that takes the current
      position and returns the next position.
    * The third result is the initial position.
    * The fourth result takes the current position and returns a true
      result if the sequence includes the value(s) for the current
      position, and false if the sequence should end instead of
      including the value(s).
    * The fifth result is like the fourth result, but it takes the
      current element value(s) instead of the current position.
    * The sixth result is like the fourth result, but it takes both
      the current position and the current element values(s) and
      determines a sequence end after the current element is already
      included in the sequence.

Here's my implementation of map-sequence (is there a better name for
this? maybe in-mapped?):

  ;; Return a sequence in which each element is the result of fun
  ;; applied to the corresponding element of seq.
  (define (map-sequence fun seq)
    (make-do-sequence
     (lambda ()
       (let-values (((more? next) (sequence-generate seq)))
	 (values
	  (lambda (pos) (fun (next)))
	  (lambda (pos) pos)
	  (void)
	  (lambda (pos) (more?))
	  (lambda (val) #t)
	  (lambda (pos val) #t))))))

  > (for/list ((n (map-sequence (lambda (x) (* x x)) (in-range 5)))) n)
  (0 1 4 9 16)

I want to make a version with define-sequence-syntax, but I'm still
digesting those docs.  (The doc for :do-in is missing post-guard, by
the way.)

--dougorleans at gmail.com


Posted on the users mailing list.