参数值说明
这个问题与 StackOverFlow while countingdigits 直接相关。我已经从这个问题中提出了一个解决方案,并且有两个问题:
首先是解决方案:
(defn num-digits-tail-recursion
([n count]
(if (= 0 n)
count
(recur (quot n 10) (inc count))))
([n] (num-digits-tail-recursion n 0)))
count 本身就是一个函数,用于计算字符串中的字符数。我假设 count 被允许作为 var 是否正确,因为它不在列表的第一个位置?
count 是第二个参数,但我只传递一个参数——数字。 以列表开头而不是以列表开头的函数语法有什么特别之处 典型的参数向量
(defn test_fn [xy] (println x))
?
编辑:
对于原始问题的不清楚之处,我深表歉意。为什么提供函数名称(count)不会导致 num-digits-tail-recursion 需要第二个参数?如果我提供 ([nx]...
而不是 (n count]...
并使用一个参数调用该函数,我会收到 insuf 参数错误。
This question is directly related to StackOverFlow while counting digits . I have lifted a solution from that question, and have a two questions:
First here is the solution:
(defn num-digits-tail-recursion
([n count]
(if (= 0 n)
count
(recur (quot n 10) (inc count))))
([n] (num-digits-tail-recursion n 0)))
count is a function in its own right for counting the number of characters in a string. Am I correct in assuming count is allowed as a var, because it's not in the first position of a list?
count is the second parameter, but I only am passing one parameter -- the number.
What is special about the function syntax that starts with a list, rather than
the typical parameter vector(defn test_fn [x y] (println x))
?
Edit:
I apologize for lack of clarity in the original question. Why does supplying a function name -- count -- not cause num-digits-tail-recursion to expect a second parameter? If I supply ([n x]...
instead of (n count]...
and call the function with one argument, I get an insuf arguments error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将函数命名为与引用的命名空间中的现有函数相同的参数并不是一个好主意,尽管在您的示例中,计数不以这种方式使用。在您的示例中,count 只是一个在每次递归迭代中都会递增的 int!
但是,如果您想传递一个计数函数并将用于它的参数命名为“count”,那么将调用什么函数?函数
clojure.core/count
还是传递的函数? (更正:由于词法作用域而传递的函数)。因此,请避免混淆并选择不同的名称。即使您不传递函数,避免使用这些名称仍然是一个好主意。函数支持参数重载,这就是您现在在这里使用的。只需阅读以下内容:http://clojure.org/function_programming
Naming args that carry functions the same as an existing function from a referenced namespace is not a good idea, although in your example here count is not used in that manner. In your example, count is just an int that gets incremented in every recursive iteration!
But if you want to pass a count function and you name the argument which is used for it 'count', what function will be called? The function
clojure.core/count
or the passed function? (rectification: The passed function due to lexical scoping). So avoid confusion and choose a different name. Even if you're not passing functions it is still a good idea to avoid these names.Functions support arity overloading and that is what you are using here now. Just read this: http://clojure.org/functional_programming
问题#1:Clojure 具有词法范围。因此,如果您创建一个与封闭范围中定义的 var 同名的本地变量,则本地副本将“隐藏”外部变量。在这种情况下,clojure.core 中的
count
是外部作用域,该函数中的count
覆盖了它。当您在此函数中引用count
时,您会看到本地函数参数列表中的定义。在您的特定情况下,
count
是在不同的命名空间中定义的,尽管范围概念是相同的。第 2 部分的示例:
Question #1: Clojure is lexicaly scoped. so if you create a local var with the same name as a var defined in an enclosing scope the local copy will 'shadow' the outer one. In this case the
count
from clojure.core is the outer scope and thecount
from this function is covering it up. when you refer tocount
within this function you see the deffinition from the local functions argument list.in your specific case
count
is defined in a different namespace, though the scoping concept is the same.example of part 2: