haw*_*eye 5 lisp recursion clojure y-combinator
在Clojure中为单个参数函数(如factorial或fibonacci)执行Y-Combinator已有详细记录:http: //rosettacode.org/wiki/Y_combinator#Clojure
我的问题是 - 你如何为两个参数函数这样做,例如这个getter?
(这里假设我想以递归方式解决这个问题,这个非惯用的clojure代码是故意出于另一个原因)
[非y组合器版]
(defn get_ [n lat]
(cond
(empty? lat) ()
(= 0 (- n 1)) (first lat)
true (get_ (- n 1) (rest lat))))
(get_ 3 '(a b c d e f g h i j))
Run Code Online (Sandbox Code Playgroud)
args 的数量不会改变任何东西,因为它们args是apply'd 的。您只需要更改以下结构get_:
(定义 get_ [f]
(fn [n 纬度]
(条件
(空?纬度)()
(= 1 n)(第一个纬度)
:else (f (dec n) (下一个纬度)))))
(定义 Y [f]
((fn [x] (xx))
(fn [x]
(f (fn [& 参数]
(应用(xx)参数))))))
用户=> ((Y getf) 3 '(abcdefghij)) C