在clojure中,如何使用iterate函数构建延迟序列 [英] In clojure, how to build lazy sequence using iterate function

查看:241
本文介绍了在clojure中,如何使用iterate函数构建延迟序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

clojure 文档提供了以下示例:

 (取10(iterate(partial + 2)0))

(def powers-of-two 1))
(取10的幂次方)

(def fib(map first(iterate(fn [[ab]] [b(+ ab)]) ])))
(take 10 fib)

任何人都可以解释clojure的迭代语法功能更详细?我很困惑与所有的使用。为什么有两个括号在(fn [[ab]] [b(+ ab)])?



另一个例子可以找到

 (defn iter [[xy]] 
(vector y(+ xy)))

(nth(iterate iter [0 1])10000)


解决方案

iterate 接受一个函数 f 和初始值 x 并生成延迟序列。 seq中的第一个元素是 x 。每个后续元素通过调用 f 与上一个元素计算。



示例1:

 (iterate(partial + 2)0)

这将生成一个序列,从0开始,其中每个元素都是添加了2的前一个元素。即:

  0 
(+ 2 0); => 2
(+ 2 2); => 4
(+ 2 4); => 6
; etc

seq中的每个元素都会传递给(partial + 2) / code>



pre> (iterate(partial * 2)1)

生成从1开始的序列,其中每个元素是前一个元素乘以2. Ie:

  1 
(* 2 1); => 2
(* 2 2); => 4
(* 2 4); => $ 8 $ b(* 2 8); => 16
; etc

同样,你可以看到每个元素如何生成下一个元素。



示例3:

  [[ab]] [b(+ ab)])[1 1])$ ​​b $ b  

code>(fn [[ab]] ...) a b 。 / p>

函数返回一个包含 b a b (即上一对中的第二个值和上一对中的两个值的总和)。



考虑到这一点,这个 iterate 调用生成:

  [1 1] 
[1(+ 1 1)]; => [1 2]
[2(+ 1 2)]; => [2 3]
[3(+ 2 3)]; => [3 5]
[5(+ 3 5)]; => [5 8]
; etc

然后(map first ...)获取每对中的第一个值,这将为您提供斐波纳契序列。


The clojure document gives the following examples:

(take 10 (iterate (partial + 2) 0))

(def powers-of-two (iterate (partial * 2) 1))
(take 10 powers-of-two)

(def fib (map first (iterate (fn [[a b]] [b (+ a b)]) [1 1])))
(take 10 fib)

Anyone can explain the syntax of clojure's iterate function in more detail? I am very confused with all the usage. Why two brackets are there in (fn [[a b]] [b (+ a b)])?

Another example can be found here:

(defn iter [[x y]]
  (vector y (+ x y)))

(nth (iterate iter [0 1]) 10000)

解决方案

iterate takes a function f and an initial value x and produces a lazy sequence. The first element in the seq is x. Each subsequent element is computed by calling f with the previous element.

Example 1:

(iterate (partial + 2) 0)

This generates a sequence, starting at 0, where each element is the previous element with 2 added to it. I.e.:

0
(+ 2 0) ; => 2
(+ 2 2) ; => 4
(+ 2 4) ; => 6
; etc

Each element in the seq is passed to (partial + 2) when generating the following element.

Example 2:

(iterate (partial * 2) 1)

This generates a sequence, starting at 1, where each element is the previous element multiplied by 2. I.e.:

1
(* 2 1) ; => 2
(* 2 2) ; => 4
(* 2 4) ; => 8
(* 2 8) ; => 16
; etc

Again, you can see how each element feeds into the generation of the next one.

Example 3:

(iterate (fn [[a b]] [b (+ a b)]) [1 1])

Firstly, (fn [[a b]] ...) is a way to destructure a value into parts. In this case, the function accepts a two-element vector and unpacks it into the local variables a and b.

The function returns a two-element vector containing b and the sum of a and b (i.e. the second value in the previous pair and the sum of both values in the previous pair).

With this in mind, this iterate call generates:

[1 1]
[1 (+ 1 1)] ; => [1 2]
[2 (+ 1 2)] ; => [2 3]
[3 (+ 2 3)] ; => [3 5]
[5 (+ 3 5)] ; => [5 8]
; etc

Then (map first ...) grabs the first value in each pair, which gives you your Fibonacci sequence.

这篇关于在clojure中,如何使用iterate函数构建延迟序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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