我在这个函数中有一个问题,虽然它编译没有错误!
该函数获得两个具有相同长度的n向量,另一个具有长度的向量2^n和一个索引.该函数执行简单的计算,然后返回一个向量.
尝试调用该函数时出现问题.例如:
(check [1 2 3] [1 2 3] [1 2 3 4 5 6 7 8] 1)
java.lang.IllegalArgumentException: Key must be integer (NO_SOURCE_FILE:0)
Run Code Online (Sandbox Code Playgroud)
功能定义:
(defn check [row1 row2 arr j]
(
(def x1 (nth arr (nth row1 j)))
(def x2 (nth arr (nth row2 (- j 1))))
(def x3 (nth arr (nth row1 (- j 1))))
(if (<= x1 x2)
(
(def row1 (assoc row1 j x3))
row1
)
((def row1 (assoc row1 (- j 1) x2))
row1)
)
)
)
Run Code Online (Sandbox Code Playgroud)
我清理了你的代码:
(defn check [row1 row2 arr j]
(let [x1 (nth arr (nth row1 j))
x2 (nth arr (nth row2 (- j 1)))
x3 (nth arr (nth row1 (- j 1)))]
(if (<= x1 x2)
(assoc row1 j x3)
(assoc row1 (- j 1) x2))))
Run Code Online (Sandbox Code Playgroud)
我不知道这是否符合您的要求,但函数会进行评估,并返回合理的值,例如
user=> (check [1 2 3] [1 2 3] [1 2 3 4 5 6 7 8] 1)
[2 2 3]
Run Code Online (Sandbox Code Playgroud)