Why does Clojure recur think it should only have one argument?

和自甴很熟 提交于 2019-12-22 01:04:38

问题


Edit:

The answer to this is I was looking at the function, not loop parameters.

In the second of the following two functions, I cannot figure out why the recur thinks it so only supposed to be passed one argument.

CompilerException java.lang.IllegalArgumentException: Mismatched argument count to recur, expected: 1 args, got: 2, compiling:(/home/cnorton/projects/clojure/clj_in_action/mr1/src/mr1.clj:84)

I am not seeing what is incorrect.

(defn determine-rover-move
    [rover-coord mov]

    (println rover-coord mov)
    (cond
        (= \L mov) (assoc rover-coord 0 (adj-compass-posL (first rover-coord)))
        (= \R mov) (assoc rover-coord 0 (adj-compass-posR (first rover-coord)))
        (= \M mov) (mov-rover rover-coord)

    ))


(defn execute-each-move
    [moves rover-coord]
    (loop [mov moves]
        (if (nil? mov)
            rover-coord
            (recur (rest moves) (determine-rover-move rover-coord mov)))))

回答1:


The important part is the section here:

(loop [mov moves] ...)

This code fragment is binding mov to moves from the outer function scope. The use of recur comes inside the loop though, so recur expects only one parameter according to the loop definition.



来源:https://stackoverflow.com/questions/8466806/why-does-clojure-recur-think-it-should-only-have-one-argument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!