Re: [cn-clojure:2677] 新手求问一个问题

41 views
Skip to first unread message
Message has been deleted

ma ming

unread,
Jan 28, 2014, 5:48:58 AM1/28/14
to cn-cl...@googlegroups.com
看tree-seq的doc “Returns a lazy sequence of the nodes in a tree, via a depth-first walk”,这个fn返回的是一个lazyseq,代表client在迭代这个seq的时候将要遇到的值,你可以把((1 2 (3))(4))画成树形感受下


在 2014年1月27日 下午4:28,配配西 <zjh...@gmail.com>写道:

关于tree-seq的问题

今天在clojuredoc上看到了tree-seq
其中第一个例子是


user=> (tree-seq seq? identity '((1 2 (3)) (4)))

(((1 2 (3)) (4)) (1 2 (3)) 1 2 (3) 3 (4) 4)

然后看了下说明大致意思是只有在第一个branch?函数返回true的时候,才会对传入的参数执行第二个函数
在这里第一个函数式seq?第二个函数式identity

我的疑问是,按照上面的写法,应该是满足是个序列才会返回到新的惰性序列里,为什么叶子节点的值也会出现在返回的惰性序列中,也就是说按照他的意思不应该是这样的输出么


(((1 2 (3)) (4)) (1 2 (3)) (3) (4))

也就是说1 2 3 4这4个叶子节点的值在被传入的时候会让seq?返回false,也就不会执行identity,也就不会出现在返回的惰性序列中,可是为什么代码的确输出了叶子节点的值,后来我又试了一下


user=> (tree-seq seq? identity 1)

(1)

照道理1传入的话seq?返回的是false啊,怎么还会执行identity呢。

希望各位前辈指点一下,真的有一点费解

--
中文社区博客:http://blog.clojure.cn/
中文问答网站:http://ask.clojure.cn/
中文邮件列表:https://groups.google.com/d/forum/cn-clojure?hl=zh-CN
---
您收到此邮件是因为您订阅了 Google 网上论坛的“CN-Clojure”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 cn-clojure+...@googlegroups.com
要向此网上论坛发帖,请发送电子邮件至 cn-cl...@googlegroups.com
通过以下网址访问此论坛:http://groups.google.com/group/cn-clojure。
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。

zjhsdtc

unread,
Jan 29, 2014, 9:05:45 AM1/29/14
to cn-cl...@googlegroups.com

可是文档上同样说了

children must be a fn of one
arg that returns a sequence of the children. Will only be called on
nodes for which branch? returns true.

也就是当branch?返回的是false的时候children就不会被执行了,叶子结点1 2 3 4传入branch?也就是这个例子中的seq?返回的是false,那就不会执行children也就是这里的identity,也就不会将1 2 3 4放入返回的惰性序列中,我对这个有一些疑问,希望大大再解释一下啊,不知道是不是我哪里进入思维误区了



您收到此邮件是因为您订阅了 Google 网上论坛“CN-Clojure”中的主题。
要退订此主题,请访问 https://groups.google.com/d/topic/cn-clojure/f9kY7i3Di6U/unsubscribe。
要退订此论坛及其所有主题,请发送电子邮件到 cn-clojure+...@googlegroups.com

ma ming

unread,
Jan 30, 2014, 10:12:59 PM1/30/14
to cn-cl...@googlegroups.com
identity在这里的作用是产生children节点集合,如果前面的branch条件不成立(seq? 1),则不会将(identity node)即(identity 1) (identity 2)这些加入到结果中,而 1 23 4本身作为一个node,还是算在结果里的.

zjhsdtc

unread,
Feb 1, 2014, 9:06:45 AM2/1/14
to cn-cl...@googlegroups.com
哦,原来是这样,多谢大大指点!
Reply all
Reply to author
Forward
0 new messages