开发者

Clojure compiler error

开发者 https://www.devze.com 2023-02-27 15:49 出处:网络
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\"

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 (clo开发者_如何学Pythonjure.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?


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号