Question

Why is the following function not working in Clojure:

(defn tests
  [] 0
  [a b] 1)

It gives the following error: clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to resolve symbol: a in this context

Was it helpful?

Solution

Each needs to be surrounded by parentheses

(defn tests 
  ([] 0) 
  ([a b] 1))

OTHER TIPS

If you want more than 2x var's, use & more:

(defn maxz
  "Returns the greatest of the nums."
  ([x] x)
  ([x y] (if (> x y) x y))
  ([x y & more]
   (reduce maxz (maxz x y) more)))

(maxz 1 2 3 4 5 100 6)

=> 100

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top