如何遍历Clojure中生成器函数生成的树?

时间:2011-08-06 16:47:40

标签: clojure

以下clojure代码尝试生成树并遍历它:

(def rules [[-1 0 0 0] [1 -1 0 0] [1 1 -1 0] [1 1 1 -1]])

(def initial-state {:expected 0.0
                :total 4
                :sheets [1 1 1 1]
                :probs [1.0 1.0 1.0 1.0]})

(defn children-of [{total :total sheets :sheets probs :probs}]
  (for [n (range 4) :let [si (sheets n)] :when (> si 0)]
     {:expected (if (= total 1) (probs n) 0.0)
      :total (+ total (dec n))
      :sheets (vector (map + sheets (rules n)))
      :probs (vector (map #(* % (/ si total)) probs))}))

(defn all-paths [root]
  ( ?? ... ?? )

(doseq [c (all-paths initial-state)]
  (println c))

children-of函数返回一个0到4个映射的seq,具体取决于:sheets向量中的非零值的数量。

我第一次尝试编写all-paths函数是这样的:

(defn all-paths [root]
  (lazy-seq (cons root
              (for [c (children-of root)]
                (all-paths c)))))

但这引发了一些java.lang.ClassCastException告诉我clojure.lang.LazySeq cannot be cast to java.lang.Number

那么,关于如何编写all-paths函数的任何想法?

更新:Stacktrace ---------------------

(Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassCastException: clojur
e.lang.LazySeq cannot be cast to java.lang.Number (problem_151.clj:0)
        at clojure.lang.Compiler.eval(Compiler.java:5440)
        at clojure.lang.Compiler.load(Compiler.java:5857)
        at clojure.lang.Compiler.loadFile(Compiler.java:5820)
        at clojure.main$load_script.invoke(main.clj:221)
        at clojure.main$script_opt.invoke(main.clj:273)
        at clojure.main$main.doInvoke(main.clj:354)
        at clojure.lang.RestFn.invoke(RestFn.java:408)
        at clojure.lang.Var.invoke(Var.java:365)
        at clojure.lang.AFn.applyToHelper(AFn.java:161)
        at clojure.lang.Var.applyTo(Var.java:482)
        at clojure.main.main(main.java:37)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassCastException: clojure.lang.LazySeq ca
nnot be cast to java.lang.Number
        at clojure.lang.LazySeq.sval(LazySeq.java:47)
        at clojure.lang.LazySeq.seq(LazySeq.java:56)
        at clojure.lang.Cons.next(Cons.java:39)
        at clojure.lang.RT.next(RT.java:560)
        at clojure.core$next.invoke(core.clj:61)
        at clojure.core$nthnext.invoke(core.clj:3399)
        at clojure.core$print_sequential.invoke(core_print.clj:55)
        at clojure.core$fn__4853.invoke(core_print.clj:138)
        at clojure.lang.MultiFn.invoke(MultiFn.java:167)
        at clojure.core$pr_on.invoke(core.clj:2812)
        at clojure.core$pr.invoke(core.clj:2824)
        at clojure.lang.AFn.applyToHelper(AFn.java:161)
        at clojure.lang.RestFn.applyTo(RestFn.java:132)
        at clojure.core$apply.invoke(core.clj:540)
        at clojure.core$prn.doInvoke(core.clj:2852)
        at clojure.lang.RestFn.applyTo(RestFn.java:137)
        at clojure.core$apply.invoke(core.clj:540)
        at clojure.core$println.doInvoke(core.clj:2870)
        at clojure.lang.RestFn.invoke(RestFn.java:408)
        at user$eval44.invoke(problem_151.clj:21)
        at clojure.lang.Compiler.eval(Compiler.java:5424)
        ... 10 more
Caused by: java.lang.RuntimeException: java.lang.ClassCastException: clojure.lang.LazySeq cannot be cast to java.lang.Nu
mber
        at clojure.lang.LazySeq.sval(LazySeq.java:47)
        at clojure.lang.LazySeq.seq(LazySeq.java:56)
        at clojure.lang.RT.seq(RT.java:450)
        at clojure.core$seq.invoke(core.clj:122)
        at user$all_paths$fn__23$iter__24__28$fn__29.invoke(problem_151.clj:17)
        at clojure.lang.LazySeq.sval(LazySeq.java:42)
        ... 30 more
Caused by: java.lang.ClassCastException: clojure.lang.LazySeq cannot be cast to java.lang.Number
        at clojure.lang.Numbers.gt(Numbers.java:198)
        at user$children_of$iter__4__8$fn__9$fn__10.invoke(problem_151.clj:9)
        at user$children_of$iter__4__8$fn__9.invoke(problem_151.clj:9)
        at clojure.lang.LazySeq.sval(LazySeq.java:42)
        ... 35 more

1 个答案:

答案 0 :(得分:3)

Caused by: java.lang.ClassCastException: clojure.lang.LazySeq cannot be cast to java.lang.Number
        at clojure.lang.Numbers.gt(Numbers.java:198)
        at user$children_of$iter__4__8$fn__9$fn__10.invoke(problem_151.clj:9)
        at user$children_of$iter__4__8$fn__9.invoke(problem_151.clj:9)
        at clojure.lang.LazySeq.sval(LazySeq.java:42)
        ... 35 more

真正的问题出在第9行:>调用期待一个数字但是得到一个序列。

它似乎在查看sheets键的某个编号元素,那么sheets如何成为一个懒惰的序列?您将+映射到序列并转换为矢量,对吗?

不,那不是你在做什么。这是vectorvec

之间的差异
user> (let [xs [1 2 3 4] ys (repeat 10)]
        ((juxt vec vector) (map + xs)))
[[1 2 3 4] [(1 2 3 4)]]

也就是说,(vec foo)返回foo的向量表示,而(vector foo)返回一个单元素向量,其中包含foo本身作为元素。