我正在研究 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/