2012-01-26 50 views
1

我就遇到了這個錯誤,當我嘗試以下評價的最後一行:「沒有消息[拋出該異常類顯示java.lang.NullPointerException]」插入排序使用Clojure的

(defn my-insertion-sort [lst] 
    (loop [list lst result '()] 
    (if-not (seq? list) result 
      (recur (rest list) (my-insert (first list) result))))) 

(defn my-insert [n lst] 
    (cond (nil? lst) (list n) 
     (> (first lst) n) (conj lst n) 
     :else 
     (conj (my-insert n (rest lst)) (first lst)))) 

(my-insertion-sort '(2 1 3)) 

凡與「我 - 不順心插入排序「功能?

回答

2
(defn my-insertion-sort [lst] 
    (loop [list lst result '()] 
    (if (empty? list) result 
     (recur (rest list) (my-insert (first list) result))))) 

(defn my-insert [n lst] 
    (cond 
    (empty? lst) (list n) 
    (> (first lst) n) (conj lst n) 
    :else (conj (my-insert n (rest lst)) (first lst)))) 
+0

好的,這個工程。那麼,爲什麼要改變(nil?__)和(not(seq?__))爲(空)? – lkahtz

+0

我將其歸納爲另一個問題:http://stackoverflow.com/questions/9025124/what-is-the-difference-between-seq-and-seq – lkahtz

+1

您可以嘗試(nil?())和(empty?( ))。你用過(rest lst),和(rest())是(),而不是零。 – unionx