Search code examples
clojure

Negating a parameter: "Unable to resolve symbol: -x in this context"


(defn my-fun [x]
    (println -x))

Executing this code, i get:

Unable to resolve symbol: -x in this context

Why can't i just invert x?


Solution

  • Dashes are valid symbols in names.

    To do a unary negation, you'd treat it like you would any other function:

    (println (- x))
    

    From the docs:

    If no ys are supplied, returns the negation of x . . .