【发布时间】:2020-08-09 05:22:01
【问题描述】:
我是 Clojure 的新手。我想我正在尝试通过程序解决这个问题,并且在 Clojure 中必须有更好(更实用)的方法来做到这一点......
-main 函数可以接收可变数量的“args”。我想将它们打印出来,但要避免 IndexOutOfBoundsException。我以为我可以模仿 Java 并使用 case fall-through 来最小化所涉及的代码,但这没有用:
(defn -main [& args]
(println "There are" (str (count args)) "input arguments.")
(println "Here are args:" (str args))
(let [x (count args)]
(case x
(> x 0) (do
(print "Here is the first arg: ")
(println (nth args 0)))
(> x 1) (do
(print "Here is the 2nd arg: ")
(println (nth args 1)))
(> x 2) (do
(print "Here is the 3rd arg: ")
(println (nth args 2))))))
【问题讨论】:
标签: clojure switch-statement case indexoutofboundsexception