Clojure 示例 : I cannot comprehend what are the values of "%2" and "%1" in "(str %2 %1)"

标签 clojure functional-programming lisp

我正在研究 clojure(初学者),我找到了一个例子,问题是我无法理解“(str %2 %1)”中“%2”和“%1”的值是什么。完整示例如下:

(defn my-reverse [s]
  (let [lst (list)]
    (reduce #(str %2 %1)
             (mapcat #(conj lst %1) s))))  

我知道 %2 指的是第二个参数,但我只在“#(str %2 %1)”之后看到一个参数值,它是 mapcat 表达式,应该是 %1。

感谢您的帮助。希望我已经说清楚了。

最佳答案

#(str %2 %1)reduce 的第一个参数:

(reduce f coll)

f should be a function of 2 arguments...returns the result of applying f to the first 2 items in coll, then applying f to that result and the 3rd item, etc.

因此 f 的一个参数是要处理的当前值(集合 coll 中的当前项),另一个是到目前为止的累积结果。

在这种情况下,(mapcat #(conj lst %1) s) 的结果是集合。在第一次调用 #(str %2 %1) 时,参数 %1%2 将是该集合中的前两个值.下次将使用该结果和集合中的第三个值调用它。从而构建一个包含 (mapcat #(conj lst %1) s) 产生的所有值的字符串。

关于Clojure 示例 : I cannot comprehend what are the values of "%2" and "%1" in "(str %2 %1)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27337153/

相关文章:

macros - 如何编写简单的 LISP 宏来返回指定形式的输出(来自形式列表)?

javascript - 从谷歌编译器中排除函数

validation - clojure 的 Schema 中至少存在一个可选键

javascript - React - 将函数传递给两扇门,并带有参数

java - Apache-Commons Commons-Functor 现状

scala - Scala中的Corecursion vs Recursion理解

https - Clojure:在heroku上包装ssl重定向?

clojure - 如何在配置文件/项目配置的另一部分中使用 profiles.clj 中的值?

lisp - Lisp 函数中针/大海捞针的排序

lisp - 你如何在 Clojure 中构建符号?