我在考虑如何构建*
出+
, 并最终构建+
出inc
,然后在另一个方向上应用相同的模式,使用较低的函数(f a b)
b 次,你会得到一个不断增加的超乘函数的阶梯。所以我决定尝试编写一个不断增加的超算子的无限列表。我想出了这个,这非常接近!:
(defn operator-staircase
[]
(iterate
(fn [f]
(fn [a b]
(loop [bottom b
result a]
(if (>= 0 bottom)
result
(recur
(dec bottom)
(f a result))))))
(fn [a b]
(inc b))))
(def ops (operator-staircase))
((nth ops 0) 3 5) ;; --> 6 (inc is arity one so it must ignore one of the args?)
((nth ops 1) 3 5) ;; --> 8 (correct addition)
((nth ops 2) 3 5) ;; --> 18 (oops, one off! otherwise correctly multiplies.)
Basically implements (fn [a b] (* a (inc b)))
((nth ops 3) 3 5) ;; ----> 1092 (Wow)
我唯一不知道怎么做的就是result
以一般的方式定义首字母!我之所以这样做是a
因为它有点用,但是对于加法,例如它必须是 0,而乘法应该是 1。
如何result
在上面的循环中定义初始值,以便它在所有情况下都能以一般方式工作?
提前致谢!