方案流问题partSums
这周我有新的作业要做,我应该编写一个函数 partSums
,它可以添加原始流中的元素来构建新的流,例如:
(element0,element0+element1,element0+element1+element2...)
结果应该有一个 0
代码> 开头。
在此示例中,我假设我们有一个名为 integers
的函数来生成类似于 Haskell [1..]
中的流,因此请使用 partSums
它应该看起来像这样:
(partSums integers)
> '(1, 3, 6, 10, 15...)
在我的理解中,它是这样的:
1 2 3 4 5 6 7 8..
1 2 3 4 5 6 7..
1 2 3 4 5 6..
1 2 3 4 5..
1 2 3 4..
1 2 .
+ .
1 3 6 10 15 21 .....
添加我已经完成的2个流:
(define (add-streams s1 s2)
(cond ((empty-stream? s1) s2)
((empty-stream? s2) s1)
(else (cons-stream
(+ (head s1)(head s2))
(add-streams (tail s1) (tail s2))))))
并且我还有函数head
,tail
,cons-流
,它们是用于流的car
、cdr
、cons
。
谁能帮我完成这个 partSums
吗?
预先感谢
Bearzk
this week I got new homework to do, I should write a function partSums
, which could add the elements in the original stream to build new stream like:
(element0,element0+element1,element0+element1+element2...)
and the result should have a 0
at the beginning.
in this example I assumed we have a function called integers
to produce a stream like in Haskell [1..]
,so use the partSums
on it should look like this:
(partSums integers)
> '(1, 3, 6, 10, 15...)
in my understanding, it's like this:
1 2 3 4 5 6 7 8..
1 2 3 4 5 6 7..
1 2 3 4 5 6..
1 2 3 4 5..
1 2 3 4..
1 2 .
+ .
1 3 6 10 15 21 .....
to add 2 streams I have done:
(define (add-streams s1 s2)
(cond ((empty-stream? s1) s2)
((empty-stream? s2) s1)
(else (cons-stream
(+ (head s1)(head s2))
(add-streams (tail s1) (tail s2))))))
and I also have functions head
, tail
, cons-stream
, they are car
,cdr
, cons
for stream.
can anyone help me finish this partSums
?
thanks in advance
bearzk
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HtDP-bot 说:
HtDP-bot says:
您的方法似乎是创建一堆移位流并将它们相加。如果您知道想要获取多少个元素,那么这可能会起作用,但是如果元素的数量不确定,那么当然您无法创建无限数量的这些流!
另一种方法是维护您在每个点添加的流列表,将其称为“当前流”,以“(列表整数)”开头。然后,在每一步
(cons integers current-streams)
中,使用(apply + (map head current-streams))
获取下一个部分和,并使用递归>current-streams
更改为(map tail current-streams)
。这样,您只需在真正需要时添加系列,而不是尝试预先创建无限数量的流。但这是一种资源密集型方法,因为您需要跟踪的流数量只会不断增加。如果您可以对固定数量的流(最好是两个,以及您编写的函数!)求和以获得您想要的输出,那就太好了。请注意,在输出的每个步骤中,上一步已经为您计算出了您需要的大部分部分总和...如果您能找到某种使用流来利用这一点的方法。尝试在您已经定义的
integers
序列的帮助下,写下partSums
序列的连续元素之间的递归关系,并看看这是否会导致您可能的结果不同的流媒体方法...Your approach appears to be to create a bunch of shifted streams and add them up. This would conceivably work if you knew how many elements you want to take, but if the number of elements is indeterminate, you can't create an infinite number of these streams, of course!
Another approach would be to maintain a list of streams you're adding from at each point, call it
current-streams
, starting with(list integers)
. Then at each step you(cons integers current-streams)
, obtain the next partial sum with(apply + (map head current-streams))
, and recurse withcurrent-streams
changing to(map tail current-streams)
. This way you only add series as you really need them, rather than trying to create an infinite number of streams up-front. But this is a resource-intensive approach, since the number of streams you need to track will just keep growing and growing.It would be nice if you could sum up a fixed number of streams (ideally two, with the function you wrote!) to get the output you wanted. Note that at each step through the output, the previous step has most of the partial sum you need already calculated for you... if you can find some way of using streams to take advantage of that. Try writing down the recurrence relationship between successive elements of the
partSums
sequence, with the help of theintegers
sequence you've already defined, and see if that leads you to a possibly different streaming approach...打印
后
定义
this
prints
after defining