2

Context

As an exercise for myself (I'm learning clojure). I wanted to implement the Depth-first search algorithm.

How I did it

Using recursion

(def graph 
  {:s {:a 3 :d 4}
   :a {:s 3 :d 5 :b 4}
   :b {:a 4 :e 5 :c 4} 
   :c {:b 4} 
   :d {:s 4 :a 5 :e 2} 
   :e {:d 2 :b 5 :f 4} 
   :f {:e 4 :g 1}})

(def stack [[:s]])

(def goal :g)

(defn cost [Graph start goal]
  (goal (start Graph)))

(defn hasloop? [path]
  (not (= (count path) (count (set path)))))

(defn atgoal? [path]
  (= goal (last path)))

(defn solved? [stack]
  (some true? (map atgoal? stack)))

(defn addtopath [path node]
    (conj path node))

(defn pop* [stack]
    (last stack))


(defn findpath [stack]
    (if (not (solved? stack))
        (let [first* (pop* stack) l (last first*) ] 
                (findpath (drop-last 
                    (remove hasloop?  (lazy-cat
                                            (map #(addtopath first* %) 
                                            (keys (l graph))) stack)))))
        [(first stack)]))

How to use

(findpath stack)

Question

I'm really really interested in how this code can be improved. Both in readability, efficiency and performance.

4

1 回答 1

4

不要使用lazy-cat,如果你这样做,你的seq就会实现drop-last

Clojure 中的递归应该使用loop/recur来避免堆栈溢出。

不要let在一行中放置多个 s:

(let [first* (pop* stack)
      l      (last first*)]

使用(if-not而不是(if (not. 相同的(not=

使用小写的 var 名称(graph, not Graph)。保持类、记录和协议的大小写。

于 2013-01-28T19:32:30.470 回答