1

I'm about to pull out my hair on this one.

For some context, I'm implementing a simple algorithm in Clojure. assume the following data structures

(def inf Double/POSITIVE_INFINITY)
(def min-dist (atom {:1 {:1 0 :2 4} :2 {:1 4 :2 0 :3 5} :3 {:2 5 :3 0}}))
(def vertexes [:1 :2 :3])

The following code will crash after the first iteration:

(for [k vertexes i vertexes j vertexes]
  ((println (str " " i " " k " "j))
   (if (> (get-in @min-dist [i j] inf) (+ (get-in @min-dist [i k] inf) (get-in @min-dist [k j] inf)) )
     (do
       ;;do some stuff
       (println "bla"))
   )))

With the following output:

:1 :1 :1 NullPointerException
user.core/eval7683/iter--7675--7684/fn--7685/iter--7677--7686/fn--7687/iter--7679--7688/fn--7689/fn--7690 (form-init1244434853692676604.clj:2)

I do understand that I'm probably violating some references during the if here. But I'm new to clojure and have no idea on what I'm doing wrong here. Something to do with the triple-for over the same seq?

1 Answer 1

2

You have an extra pair of brackets on the second line. You probably want to add do there and it will work.

Sign up to request clarification or add additional context in comments.

1 Comment

this happens when you are learning Clojure, will gone with practice :)

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.