[racket-dev] Need a clarification on the implementation of stream-map

From: Daniel King (danking at ccs.neu.edu)
Date: Sun Jul 8 07:25:42 EDT 2012

Question 0:

In collects/racket/stream.rkt, `stream-map' is defined as:

  (define (stream-map f s)
    (unless (procedure? f) (raise-argument-error 'stream-map "procedure?" f))
    (unless (stream? s) (raise-argument-error 'stream-map "stream?" s))
    (let loop ([s s])
      (cond
       [(stream-empty? s) empty-stream]
       [else (stream-cons (call-with-values (lambda () (stream-first s)) f)
                          (loop (stream-rest s)))])))


I don't understand the difference between:

  (call-with-values (lambda () (stream-first s)) f)

and

  (f (stream-first s))

because the contract for `stream-first' is:

  (stream-first s) → any
  s : (and/c stream? (not/c stream-empty?))

Which seems to me to just return a single value. I was taking a second look at
my changes to `stream-map' which add support for multiple streams. I noticed
this unusual snippet as I rebased onto the latest version of plt/master.


Question 1:

Would it make more sense to simply update all the sequence procedures to handle
multiple sequences? Is there a circumstance where it doesn't make sense to
handle multiple sequences in a sequence procedure such as `sequence-map'?

I figure that the sequence procedures don't support multiple sequences is just
lack of interest. Do most people just use the `for' macros in these cases?

--
Dan King
College of Computer and Information Science
Northeastern University


Posted on the dev mailing list.