【问题标题】:clojure.lang.Lazysez cannot be cast to clojure.lang.IFnclojure.lang.Lazysez 无法转换为 clojure.lang.IFn
【发布时间】:2013-09-25 05:09:18
【问题描述】:

嘿,我正在对一个八字谜题进行 A* 搜索,我有 2 个主要问题。

已修复:首先,当我在 for 循环中多态的第二部分的行上运行它时,我无法将 clojure.lang.Lazysez 强制转换为 clojure.lang.IFn,但我没有确定为什么。

这是我的代码:

(defn a-star

  ([board history]

     (if  (at-end? board) (println board)
    (

          (let [options (filter #(possible-move? % board) *moves*)
                move (into (pm/priority-map) (for [move options] [move (global-man-dis       (move-tile board move))]))]

              (for [pair move :let [next-move (key pair)]] 

                 (do 
                  (println (move-tile board next-move))
                  (a-star (move-tile board next-move) next-move (conj history board))
                 )
              )
          )
    )))



  ([board prev-move history]

     (if (or (at-end? board) (history-check history board)) (println board)
    (

      (let [options (get-queue board (dont-go-back prev-move))
            move (into (pm/priority-map) (for [move options] [move (global-man-dis     (move-tile board move))]))]

        (for [pair move :let [next-move (key pair)]] 

           (do 
            (println (move-tile board next-move))
            (a-star (move-tile board next-move) next-move (conj history board))
           )
         )
       )
     ))))



(defn -main [& args]
  (println "insert a list all numbers no spaces or letters")
  (def board (mapv (fn [^Character c] (Character/digit c 10)) (read-line)))
  ;(def testt [0 8 4 7 2 1 3 5 6])
  ;(def testt [1 2 3 5 4 6 8 0 7])
  (a-star board [])
  )

自发布以来尝试过:删除 let 语句周围的“else”括号,但现在什么也不返回 FIXED 删除了 else 括号并将 for 更改为 doseq,因为 for 是惰性的,在这种情况下不会输出任何内容。生病再单独问另一个问题

【问题讨论】:

    标签: loops clojure sequence


    【解决方案1】:

    a-star 中的一些基本情况会返回惰性序列。您将 a-star 的输出调用为函数,因为 let 语句周围有一对额外的括号。

    【讨论】:

    • 我有点困惑哪些额外的括号?我尝试删除整个 let 周围的“else”括号,但它现在什么都不返回
    • 查看 repl 中以下内容的区别:(let [a (constantly true)] a) ((let [a (constantly true)] a)) - 一个返回函数,另一个返回 true。
    • 不返回任何内容是指返回 nil 吗?如果是这样,那很可能是因为 print 语句返回 nil,并且您以 print 语句结束分支。
    • 它现在可以工作了,问题是 for 循环是惰性的。在它什么也没打印之前。我将其更改为doseq,现在它很好。我决定把这个清理干净,并在另一个帖子中问我的另一个问题
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2017-04-06
    • 1970-01-01
    相关资源
    最近更新 更多