使用clojure插入排序

时间:2012-01-26 19:59:23

标签: clojure

当我尝试评估下面的最后一行时遇到了这个错误:“没有消息。[抛出类java.lang.NullPointerException]”

(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))

“my-insertion-sort”功能出了什么问题?

1 个答案:

答案 0 :(得分: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))))