0

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?

3 Answers 3

5

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*)

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

5 Comments

Yes I understand that and have already been over that documentation page. I count two parts to the let statement (apply struct storeinfo) and (split input #",") What's wrong with it?
(apply struct storeinfo) is not a symbol, like x or y in the example I gave you. Btw, you have (apply struct storeinfo (clojure.string/split input #",")), not (apply struct storeinfo) and (split input #",").
The way I understood it, (apply struct storeinfo) would return a storeinfo struct that would then be split by the following statement, (split input #",") Is let the wrong keyword/operation to use here?
Yes. Maybe you mean something like -> or ->>, see for example blog.fogus.me/2009/09/04/understanding-the-clojure-macro. But if (apply struct storeinfo) is the first form that will be executed, I would expect storeinfo to be something you have defined before or a parameter of makeStruct.
Hey. If you're still watching this I posted another question about a clojure compiler error. No pressure if you can't answer. Thanks for your help.
0

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.

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.