方案流问题partSums

发布于 2024-11-17 00:18:18 字数 1091 浏览 4 评论 0原文

这周我有新的作业要做,我应该编写一个函数 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))))))

并且我还有函数headtailcons-流,它们是用于流的carcdrcons

谁能帮我完成这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白昼 2024-11-24 00:18:18

HtDP-bot 说:

  • 你能写下此功能的目的声明和合同吗?它消耗什么,生产什么?
  • 你能将你的例子转化为测试用例吗?写一个表达式,以及它应该产生的值。使用“等于?”来比较它们

HtDP-bot says:

  • Can you write down a purpose statement and contract for this function? What does it consume, and what does it produce?
  • Can you translate your examples into test cases? Write an expression, and the value it should produce. Compare them using "equal?"
南城旧梦 2024-11-24 00:18:18

您的方法似乎是创建一堆移位流并将它们相加。如果您知道想要获取多少个元素,那么这可能会起作用,但是如果元素的数量不确定,那么当然您无法创建无限数量的这些流!

另一种方法是维护您在每个点添加的流列表,将其称为“当前流”,以“(列表整数)”开头。然后,在每一步 (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 with current-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 the integers sequence you've already defined, and see if that leads you to a possibly different streaming approach...

枉心 2024-11-24 00:18:18
(define (partial-sums s)
  (let loop ((current 0) (s s))
    (if (empty-stream? s) '()
        (let ((v (+ current (head s))))
          (cons-stream v (loop v (tail s)))))))

打印

(partial-sums '(1 2 3 4 5 6 7 8 9))

(1 3 6 10 15 21 28 36 45)

定义

(define empty-stream? null?)
(define tail cdr)
(define head car)
(define cons-stream cons)
(define (partial-sums s)
  (let loop ((current 0) (s s))
    (if (empty-stream? s) '()
        (let ((v (+ current (head s))))
          (cons-stream v (loop v (tail s)))))))

this

(partial-sums '(1 2 3 4 5 6 7 8 9))

prints

(1 3 6 10 15 21 28 36 45)

after defining

(define empty-stream? null?)
(define tail cdr)
(define head car)
(define cons-stream cons)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文