Ticket #18 (new defect)

Opened 6 years ago

stream-split-at

Reported by: dherman Owned by: dherman
Priority: major Milestone:
Component: dherman/stream.plt Keywords:
Cc: Version:
Racket Version:

Description

Steve Huwig wrote:

Hi,

`stream-split-at' in stream.plt 1 1 only returns one value, and
attempting to use that value results in errors. I looked at the source
and I think there's a simple oversight:

  (define (stream-split-at i stream)
    (stream-delay
;    ^^^^^^^^^^^^ 
     (cond
       [(zero? i)
        (values stream-null stream)]
       [(stream-null? stream)
        (values stream-null stream-null)]
       [else
        (let-values ([(prefix suffix) (stream-split-at (sub1 i)
(stream-cdr stream))])
          (values (stream-cons (stream-car stream) prefix) suffix))])))

I don't think stream-delay is correct here! At least, when I changed the
code to:

  (define (stream-split-at i stream)
     (cond
       [(zero? i)
        (values stream-null stream)]
       [(stream-null? stream)
        (values stream-null stream-null)]
       [else
        (let-values ([(prefix suffix) (stream-split-at (sub1 i)
(stream-cdr stream))])
          (values (stream-cons (stream-car stream) prefix) suffix))]))

the routine behaved as I expected.

Is this a bug or am I missing something basic? Do you have a good
example of using stream-split-at? With the original code, I tried:

(stream-split-at 3 (list->stream (list 1 2 3 4 5 6)))

#<struct:stream>

(stream-car (stream-split-at 3 (list->stream (list 1 2 3 4 5 6))))

. context (lexical binding) expected 2 values, received 1 value:
#<struct:stream>



which left me cold, but with the new code, I was able to do:

(stream-split-at 3 (list->stream (list 1 2 3 4 5)))

#<struct:stream>
#<struct:stream>

(let-values ([(front back) (stream-split-at 3 (list->stream (list 1 2

3 4 5)))])
(values (stream->list front) (stream->list back)))
(1 2 3)
(4 5)

which is about what I expected to need to do.

If this is a bug, then I think stream-partition and stream-span suffer
the same bug. If it's not a bug, then apparently the bug is in my brain
for not figuring out the right API.

Thanks,
Steve Huwig

P.S. stream-split-at reverses the order of split-at arguments from
SRFI-1, not sure if that's deliberate.

Note: See TracTickets for help on using tickets.