Multiple function calls within a function allowed in Clojure?

I'm trying to take certain values from city hashmaps to implement into my distance function here:

Distance Function

but every time I do it says Unsupported binding form: (latReturn record1)

Here are my latReturn and longReturn functions:

(defn latReturn [line]
    (def x line)
    (def y (string/split x #","))
    (def my-map { :city (y 0)
               :lat (y 1)
               :long (y 2)
               :country (y 3)
               :iso2 (y 4)
               :admin (y 5)
               :capital (y 6)
               :pop (y 7)
               :popproper (y 8)
    })
    (read-string (my-map :lat))
)

(defn longReturn [line]
    (def x line)
    (def y (string/split x #","))
    (def my-map { :city (y 0)
               :lat (y 1)
               :long (y 2)
               :country (y 3)
               :iso2 (y 4)
               :admin (y 5)
               :capital (y 6)
               :pop (y 7)
               :popproper (y 8)
    })
    (read-string (my-map :long))
)

What am I doing wrong here?

1 answer

  • answered 2019-11-14 09:32 Stefan Kamphausen

    Your distance function has a wrong binding in your call to let. Please read some introduction Clojure material to see how let is supposed to be used.

    Essentially, it looks like this:

    (let [binding1 value1
          binding2 value2]
       (some-things-to-do-with binding1 binding2))
    

    But you have a thing in parens as a binding:

    (let [...
          (latReturn record1) (Math/toRadians ...)
          ...]
       ...)
    

    You can't assign to (latReturn record1) because that's a function call.

    As others pointed out already, don't use def inside a def or defn. It's always wrong (or rather it's always wrong until you learned enough Clojure to know the very, very rare cases where it may be right, and even then it usually isn't).