1

I ran into this error when I try to evaluate the last line below: "No message. [Thrown class 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))

Where goes wrong with "my-insertion-sort" function?

1 Answer 1

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))))
Sign up to request clarification or add additional context in comments.

3 Comments

okay, this works. So what is the why change (nil? __) and (not (seq? __)) to (empty ?) make this difference?
I summarize this into another question: stackoverflow.com/questions/9025124/…
You can try (nil? ()) and (empty? ()). You used (rest lst), and (rest ()) is (), not nil.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.