如何做出累积序列? [英] How to make a cumulative sequence?

查看:178
本文介绍了如何做出累积序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个延迟序列,如下所示:

Say I have a lazy sequence like the following:

(def s (iterate inc 1))
(take 10 s)
=> (1 2 3 4 5 6 7 8 9 10)

现在, s 的累计总和如下:

Now, I want to generate a sequence of cumulative sum of s like the following:

=> (1 3 6 10 15 ...)

我该如何做?
我尝试使用 atom 并累加和(mutating)这是生成累积序列的唯一方法,还是有更好的方法这?

How can I do this? What I tried is to use atom and accumulate the sum to it(mutating) Is this the only way to generate cumulative sequence or is there a better way to do this?

注意:上面的累积和只是一个例子。源序列可以是其他序列。所以我不能使用公式: s(n)= n(n + 1)/ 2

NOTE: the above cumulative sum is only an example. The source sequence can be other sequence. So I can't use formula: s(n) = n(n+1)/2

推荐答案

(take 10 (reductions + s))
=> (1 3 6 10 15 21 28 36 45 55)

这篇关于如何做出累积序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆