Exception 非法参数异常-Clojure

Exception 非法参数异常-Clojure,exception,parameters,clojure,anonymous-function,Exception,Parameters,Clojure,Anonymous Function,我有一些Clojure代码,看起来非常简单,但却抛出了一个非法的ArgumentException。以下代码显示了我一直在编写的4个函数,以供参考。我的错误在第四节 "Determine candy amount for first parameter. Returns even integers only (odd #'s rounded up)." (defn fun1 [x y] (if (= (rem (+ (quot x 2) (quot y 2)) 2) 1) (+ (+ (quot

我有一些Clojure代码,看起来非常简单,但却抛出了一个非法的ArgumentException。以下代码显示了我一直在编写的4个函数,以供参考。我的错误在第四节

"Determine candy amount for first parameter. Returns even integers only (odd #'s rounded up)."
(defn fun1 [x y] (if (= (rem (+ (quot x 2) (quot y 2)) 2) 1) (+ (+ (quot x 2) (quot y 2)) 1) (+ (quot x 2) (quot y 2))))

"Play one round. Returns vector with new values."
(defn fun2 [[x y z t]] (vector (fun1 x z) (fun1 y x) (fun1 z y) (+ t 1)))

"Yield infinite sequence of turns."
(defn fun3 [[x y z t]] (if (= x y z) (vector x y z t) (iterate fun2 [x y z t])))

(defn fun4 [[x y z t]] (take-while #(not= %1 %2 %3) (fun3 [x y z t])))
第四个函数调用第三个函数,直到值xy和z不相等。代码编译正确,但我在REPL中得到以下序列:

Clojure 1.1.0
user=> (load-file "ass8.clj")
#'user/fun4
user=> (fun4 [10 10 10 1])
java.lang.IllegalArgumentException: Wrong number of args passed to: user$fun4--21$fn
(user=> (fun4 [[10 10 10 1]])
java.lang.IllegalArgumentException: Wrong number of args passed to: user$fun4--21$fn
(user=> (fun4 10 10 10 1)
java.lang.IllegalArgumentException: Wrong number of args passed to: user$fun4 (NO_SOURCE_FILE:0)
只有第一个表达式是正确的,但我要强调的是,我已经尝试了所有可能的组合。有人能解释一下这个神秘的错误吗?可能在您自己的Clojure环境中进行测试


作为旁注,fun3不应该在x=y=z时停止吗?现在它似乎给出了一个无限序列,所以if似乎是多余的。

我不确定你想做什么,但对我来说
(fun3[10 10 10 1])
的计算结果是
[10 10 10 1]


因为这只是一个普通的旧向量,你的匿名函数调用的第一个东西是当它等待3个参数时的数字10。

上面的注释你可以使用下面的代码(使用现有的Fun1代码)


你不需要fun3和fun4,take就像map一样工作,它一次从一个集合中获取一个值并检查谓词(即谓词将得到一个参数)。

在写我的响应时,我意识到根本问题是什么。基本上,当三个值(如10)相等时,“游戏”应该停止。要做到这一点,我希望fun4是一个执行过程,直到xy和z(fun3的返回值)相等。我想让函数3自己做这件事,但我不确定如何使用一个分解的向量返回值进行条件检查。
(defn fun2 [v] 
        (loop [[a b c d] v] 
              (if (= a b c) 
                  [a b c d] 
              (recur [(fun1 a c) (fun1 b a) (fun1 c b) (+ d 1)]))))