我正想着如何最终构建*
+
和+
以及inc
,然后在另一个方向上应用相同的模式,使用较低的函数(f a b)
b次,您会得到一个超乘法函数的阶梯不断增加的超度。所以我决定尝试编写一个不断增加的超级引擎的无限列表。我想出了这个,这非常接近! :clojure:如果我正在创建操作员楼梯,我如何为操作员定义一般身份?
(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
的初始值,以便它在所有一般情况下的案件?
在此先感谢!
我不确定它是否适合你想要达到的目标,但是你能利用'(+)'为0而'(*)'为1的事实吗? – Hugh 2013-03-21 05:06:55
请参阅http://en.wikipedia.org/wiki/Hyperoperation。你必须定义几个基本情况。请注意,将定义直接转换为递归代码会使堆栈即使在较小的值下也会受到影响,即使在较小的值下,迭代实现也需要非常非常长的时间来计算。看到[价值表](http://en.wikipedia.org/wiki/Knuth%27s_up-arrow_notation#Tables_of_values)了解这种增长有多快。 – 2013-03-21 16:43:04