为什么Clojure会认为它应该只有一个参数? [英] Why does Clojure recur think it should only have one argument?

查看:150
本文介绍了为什么Clojure会认为它应该只有一个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:

这个问题的答案是我正在查看函数,而不是循环参数。

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

在下面两个函数的第二个,我不知道为什么recur认为它只是传递一个参数。

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:错误的参数计数重复,期望:1 args,got:2,编译:(/ home / cnorton / projects / clojure / clj_in_action / mr1 / src / mr1.clj:84)

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


推荐答案

重要的是这里的部​​分:

The important part is the section here:

(loop [mov moves] ...)

code> mov 到移动。虽然 recur 循环内,因此 recur 根据 loop 定义,只有一个参数。

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.

这篇关于为什么Clojure会认为它应该只有一个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆