[英]SICP Streams (Scheme) - CDR into the implicit stream of ones
(define ones (cons-stream 1 ones))
(stream-cdr ones)
returns an infinite sequence of evaluated 1s - ie I get ;Value: #0={1 1 1 1 1 1 1
and so on... - not the symbolic {1 1...}
I would expect...返回评估 1 的无限序列 - 即我得到
;Value: #0={1 1 1 1 1 1 1
等等... - 不是我期望的符号{1 1...}
...
On the other end if I define ints
and cdr
into it,另一方面,如果我在其中定义
ints
和cdr
,
(define ints (cons-stream 1 (stream-map + ones ints)))
(stream-cdr ints)
I obtain the expected {1 2...}
我得到了预期的
{1 2...}
Can anyone explain me why?谁能解释我为什么? I expect the
stream-map
definition to be not too far from我希望
stream-map
定义不会太远
(define (mystream-map proc . argstreams)
(if (stream-null? (car argstreams))
the-empty-stream
(cons-stream
(apply proc (map stream-car argstreams))
(apply mystream-map (cons proc (map stream-cdr argstreams))))))
which I defined in ex 3.50 and returns the same result... but this seems to (via (map stream-cdr argstreams)
) stream-cdr
into ones, which I would expect to result in the infinite sequence I get above!我在 ex 3.50 中定义并返回相同的结果......但这似乎(通过
(map stream-cdr argstreams)
)将stream-cdr
转换为一个,我希望这会导致我在上面得到的无限序列!
Even though, if I understand correctly, due to cons-stream
being a macro the second part inside the stream-map
will only be lazily evaluated, at some point while I cdr
into the ints
stream I would expect to have to cdr
into ones
as well.. which instead doesn't seem to be happening: :/ Any help in understanding this would be very much appreciated!即使,如果我理解正确,由于
cons-stream
是一个宏, stream-map
中的第二部分只会被懒惰地评估,在某些时候,当我cdr
进入ints
ones
我希望必须将cdr
转换为好吧..这似乎没有发生::/任何有助于理解这一点的帮助将不胜感激!
(I also didn't override any scheme symbol and I'm running everything in MIT Scheme - Release 11.2 on OS X.) (我也没有覆盖任何方案符号,我在 MIT 方案中运行所有东西 - OS X 上的 11.2 版。)
(define ones (cons-stream 1 ones)) (stream-cdr ones)
returns an infinite sequence of evaluated
ones
.返回一个无限
ones
评估序列。
No it does not.不,不是的。
(stream-cdr ones) = (stream-cdr (cons-stream 1 ones)) = ones
. (stream-cdr ones) = (stream-cdr (cons-stream 1 ones)) = ones
。 It is just one ones
, not the sequence of ones
.它只是一个,而
ones
ones
序列。
And what is ones
?什么是
ones
?
Its stream-car
is 1
, and its stream-cdr
is ones
.它的
stream-car
是1
,它ones
stream-cdr
是 one 。
Whose stream-car
is 1
, and stream-cdr
is ones
again.谁
stream-car
是1
,而stream-cdr
又是ones
。
And so if you (stream-take n ones)
(with an obvious implementation of stream-take
) you will receive an n
-long list of 1
s, whatever the (non-negative) n
is.因此,如果您
(stream-take n ones)
(具有明显的stream-take
实现),您将收到一个n
长的1
列表,无论(非负) n
是什么。
In other words, repeated cdr
ing into ones
produces an unbounded sequence of 1
s.换句话说,重复
cdr
ing into one 会产生一个无界ones
1
序列。 Or symbolically, {1 1 1...}
indeed.或者象征性地,确实是
{1 1 1...}
。
after your edit it becomes clear the question is about REPL behavior.在您进行编辑后,很明显问题是关于 REPL 行为。 I only have Racket, where
我只有球拍,在哪里
(define ones (cons-stream 1 ones))
(define (onesF) (cons-stream 1 (onesF)))
(define ints (cons-stream 1 (stream-map + ones ints)))
(stream-cdr ones)
(stream-cdr (onesF))
(stream-cdr ints)
prints印刷
(mcons 1 #<promise>)
(mcons 1 #<promise>)
(mcons 2 #<promise>)
and, having defined并且,已经定义
(define (take n s)
(if (<= n 1)
(if (= n 1) ; prevent an excessive `force`
(list (stream-car s))
'())
(cons (stream-car s)
(take (- n 1)
(stream-cdr s)))))
we get我们得到
> (display (take 5 ones))
(1 1 1 1 1)
> (display (take 5 (onesF)))
(1 1 1 1 1)
> (display (take 5 ints))
(1 2 3 4 5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.