通过参数获取 Clojure 向量

发布于 2024-12-07 16:54:40 字数 446 浏览 0 评论 0原文

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

我收到这个异常: java.lang.IllegalArgumentException:for 在这一行中需要绑定向量 (cartax.cl:5) 中的偶数个形式

:(for [v] (println (v))) 这个 v 通过参数传递

(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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗲 2024-12-14 16:54:40

您可能需要以下内容:

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

您需要将 for 宏与绑定一起使用。也就是说你想要一些东西在你的向量范围内。 “偶数”的原因是您可以一次覆盖多个向量!另外,参数最好不加括号;也就是说,请确保写下

(calculate-car-tax car-tax)

而不是

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

以下内容:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文