(originally posted December 02, 2009, 21:44)
## `*` is the hyper of `+`, `^` is the hyper of `*`
> hyper <- function(fn) function(a,b) Reduce(fn, rep(a,b))
> compose <- function(fn1,fn2) function(x) fn1(fn2(x))
> hyperoperation <- function(n) Reduce(compose,listRep(hyper,n))(`+`)
('rep(obj,n)' and 'listRep(obj,n)' just return a list containing 'obj' n times. I had to invent 'listRep' for technical reasons, namely passing closures to 'rep' returns an error: "object of type 'closure' is not subsettable")
## 'hyper' clearly works:
> multiplication <- hyper(`+`)
> multiplication(2,3)
[1] 6
> exponential <- hyper(hyper(`+`))
> exponential(2,3)
[1] 8
> tetration <- hyper(hyper(hyper(`+`)))
> tetration(2,3) ##2^(2^2)
[1] 16
'compose' also works:
> compose(function(x) x^2, function(x) x+1)(4)
[1] 25
and so does 'hyperoperation', woohoo!
> hyperoperation(1)(2,3)
[1] 6
until R decided to be lame:
> hyperoperation(2)(2,3)
Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
>
But I was impressed that I got this far with R! I suspect I could get further if I learn to use defmacro.
inspi#FF0000 by Scott Aaronson's essay,
"Who Can Name the Bigger Number?"