1
(def andre {:owner "Andre" :type "car" :cur-speed "100" :license-plate "ABC"})
(def blastoise {:owner "Blastoise" :type "truck" :cur-speed "120" :license-plate "XYZ"})
(def car-tax[andre blastoise])
(defn calculate-car-tax [v]
    (for [v] (println (v)))
)

(calculate-car-tax(car-tax))

I'm getting this exception: java.lang.IllegalArgumentException: for requires an even number of forms in binding vector (cartax.cl:5)

in this line: (for [v] (println (v))) this v is passed via parameter

1 Answer 1

2

You likely want the following

(def andre {:owner "Andre" :type "car" :cur-speed "100" :license-plate "ABC"})
(def blastoise {:owner "Blastoise" :type "truck" :cur-speed "120" :license-plate "XYZ"})
(def car-tax [andre blastoise])
(defn calculate-car-tax [v]
    (for [element v] (println element))
)

(calculate-car-tax car-tax)

You need to use the for macro with a binding. That is you want something to range over your vector. The reason for "even number" is that you can range over multiple vectors at once! Also arguments are best left unparenthesized; that is, make sure to write

(calculate-car-tax car-tax)

and not

(calculate-car-tax(car-tax))

Here is a transcript:

user=> (def andre {:owner "Andre" :type "car" :cur-speed "100" :license-plate "ABC"})
#'user/andre
user=> (def blastoise {:owner "Blastoise" :type "truck" :cur-speed "120" :license-plate "XYZ"})
#'user/blastoise
user=> (def car-tax [andre blastoise])
#'user/car-tax
user=> (defn calculate-car-tax [v]
    (for [element v] (println element))
)
#'user/calculate-car-tax
user=> (calculate-car-tax car-tax)
({:owner Andre, :type car, :cur-speed 100, :license-plate ABC}
{:owner Blastoise, :type truck, :cur-speed 120, :license-plate XYZ}
nil nil)
Sign up to request clarification or add additional context in comments.

2 Comments

You're also using for to get side-effects, which is going to suddenly and mysteriously become a problem at some later stage in development of your program. Use instead doseq, which is like for except it is not lazy and does not return anything.
It's great that Ray's answer helped. Now give the man an upvote and accept his answer! :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.