如何在clojure中使用嵌套

时间:2016-11-22 00:07:17

标签: clojure

我有一个列表列表((1,2,3,4)(2,3,4,5)(3,4,5,6)),我想在clojure中实现for循环。我想首先得到第一个列表,即(1,2,3,4),然后再次实现for循环以获取每个元素。

谢谢

3 个答案:

答案 0 :(得分:1)

  1. Clojure的for 不是循环结构。
  2. 生成必要嵌套的组合 mapfiltertake-while来电。这些操作在序列上进行。
  3. 它总是产生懒惰序列
  4. 从你的问题,我推断你理解(1),但不做任何(2)或(3)。如果您想有效地使用Clojure,请理解他们的术语,您的问题将自行解决。

答案 1 :(得分:0)

如果您想循环浏览所有数字,就像它们在一个列表中一样,您可以flatten列表:

> (flatten `((1,2,3,4)(2,3,4,5)(3,4,5,6)))
(1 2 3 4 2 3 4 5 3 4 5 6)

之后,要对所有数字执行操作,您可以使用任何包含数字列表的函数。以下是使用reduce+的示例:

> (reduce + (flatten `((1,2,3,4)(2,3,4,5)(3,4,5,6))))
42

答案 2 :(得分:-4)

for表达式用于将集合转换为另一个集合,即返回值。 doseq表达式意味着有副作用,并始终返回nil

以下是一些示例代码&结果。请注意,我通常更喜欢forv而不是for,因为forv不是懒惰的,并且始终会在向量中返回结果。

(ns xyz
  (:require  [tupelo.core :as t] ))
(t/refer-tupelo)

(def data
 '[ (1,2,3,4)
    (2,3,4,5)
    (3,4,5,6) ] )

(newline)
(println "forv demo")
(spyx (forv [i [1 2 3]]
  (spyx i )))

(newline)
(println "doseq demo")
(spyx (doseq [i [1 2 3]]
  (spyx i )))

(println "-----------------------------------------------------------------------------")
(newline)
(println "for 1d")
(println "final result ="
  (forv [sub-list data]
    (spyx sub-list )))

(newline)
(println "for 2d")
(println "final result ="
  (forv [sub-list  data
         int-val   sub-list]
    (spyx int-val )))
(newline)

(newline)
(println "for 2d-2")
(println "final result ="
  (forv [sub-list  data]
    (forv [int-val   sub-list]
      (spyx int-val ))))
(newline)

(println "-----------------------------------------------------------------------------")
(newline)
(println "doseq 1d")
(doseq [sub-list data]
  (println  "sub-list =" sub-list ))

(newline)
(println "doseq 2d")
(doseq [sub-list  data]
  (doseq [int-val sub-list]
    (spyx int-val )))

(newline)
(println "doseq 2d-2")
(doseq [sub-list  data
        int-val   sub-list]
    (spyx int-val ))

第一个显示两个循环数据,但for / forv返回一个新集合,而doseq返回nil并且仅用于副作用。< / p>

forv demo
i => 1
i => 2
i => 3
(forv [i [1 2 3]] (spyx i)) => [1 2 3]

doseq demo
i => 1
i => 2
i => 3
(doseq [i [1 2 3]] (spyx i)) => nil

第二部分显示for如何用于1-d或2-d循环。此外,单个forv语句可以进行二维循环,也可以嵌套forv语句。注意3种情况下最终结果的嵌套差异。

-----------------------------------------------------------------------------

for 1d
sub-list => (1 2 3 4)
sub-list => (2 3 4 5)
sub-list => (3 4 5 6)
final result = [(1 2 3 4) (2 3 4 5) (3 4 5 6)]

for 2d
int-val => 1
int-val => 2
int-val => 3
int-val => 4
int-val => 2
int-val => 3
int-val => 4
int-val => 5
int-val => 3
int-val => 4
int-val => 5
int-val => 6
final result = [1 2 3 4 2 3 4 5 3 4 5 6]


for 2d-2
int-val => 1
int-val => 2
int-val => 3
int-val => 4
int-val => 2
int-val => 3
int-val => 4
int-val => 5
int-val => 3
int-val => 4
int-val => 5
int-val => 6
final result = [[1 2 3 4] [2 3 4 5] [3 4 5 6]]

-----------------------------------------------------------------------------

doseq语法类似于for,但只有print等副作用对外界可见。 doseq始终返回nil。这也意味着返回值与forv没有嵌套差异。

doseq 1d
sub-list = (1 2 3 4)
sub-list = (2 3 4 5)
sub-list = (3 4 5 6)

doseq 2d
int-val => 1
int-val => 2
int-val => 3
int-val => 4
int-val => 2
int-val => 3
int-val => 4
int-val => 5
int-val => 3
int-val => 4
int-val => 5
int-val => 6

doseq 2d-2
int-val => 1
int-val => 2
int-val => 3
int-val => 4
int-val => 2
int-val => 3
int-val => 4
int-val => 5
int-val => 3
int-val => 4
int-val => 5
int-val => 6

请注意,project.clj需要包含以下spyx才能使用

:依赖[     [tupelo&#34; 0.9.10&#34;]