user520621
user520621

Reputation:

Clojure compiler error

I'm working on a clojure program that includes these lines of code:

(defn makeStruct 
 "Take a line of input and return a starbucks-struct"
 [input]
 (let[(apply struct storeinfo (clojure.string/split input #","))]
 )

)

And am getting this compiler error:

Exception in thread "main" java.lang.IllegalArgumentException: let requires an even number of forms in binding vector (clojureHW.clj:24)

I am very new to clojure and am not entirely sure what I am doing, but in this case input is a string and I am splitting it into a vector to initialize my struct. Am I using the syntax of let incorrectly?

Upvotes: 0

Views: 4055

Answers (3)

Alessandra Sierra
Alessandra Sierra

Reputation: 10897

The "binding" side of a let expression may only have symbols and destructuring forms, such as vectors and maps. The binding side of let can't evaluate an expression like (apply struct storeinfo).

Upvotes: 0

nickik
nickik

Reputation: 5881

I think you still have misunderstanding on the lisp way handling return values and bindings.

Everything in a pair of parens an expression returns a value that value can be uses by a in a other expression or be bound to a symbol.

You have this

(apply struct storeinfo (clojure.string/split input #","))

This returnes on value because there is only one expression. Its quite simple just count the outher most parens.

And since you have nothing else in the let you have a odd number of forms (expression) in let.

Upvotes: 0

Michiel Borkent
Michiel Borkent

Reputation: 34860

let requires an even numbers of forms, because it binds values to locals:

(let [x 10,
      y (+ x 20)]
   ; do something with x and y here
   (* x y))

Please read the documentation here: http://clojure.org/special_forms#Special%20Forms--(let%20%5Bbindings*%20%5D%20exprs*)

Upvotes: 5

Related Questions