创建一个闭包

oct*_*bus 3 clojure

我想创建一个闭包(函数生成器)来将数字提升为幂,而不使用特定的Clojure库来完成此任务.现在,我可以用循环来做这件事.复发.

(defn exp1
 [in-num in-exp-multi]
 (loop [num in-num exp-multi in-exp-multi]
    (if (> exp-multi 1)
        (recur (* num in-num) (- exp-multi 1))
        num)))
Run Code Online (Sandbox Code Playgroud)

我已经尝试过使用partial来提高功率,但是我仍然坚持使用重复乘以一个数字所需的构造数次.所以,我正在寻找一个生成函数并将其应用x次的示例.

编辑:

这个例子只是用循环来解决问题..复发.我的愿望是用封闭来解决这个问题.

mqp*_*mqp 7

我无法准确地从你的问题中辨别出你的要求,但也许这个?

(defn powers-of [exponent]
  (iterate #(* % exponent) 1))

(defn nth-power-of [exponent]
  (partial nth (powers-of exponent)))

((nth-power-of 5) 2) ;; returns 25
Run Code Online (Sandbox Code Playgroud)

iterate根据你的描述,我认为你正在寻找什么; 它会为种子创建一个懒惰的seq函数.