hyper-abstracted R contest

Feb 02, 2014 21:44

(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 ( Read more... )

programming

Leave a comment

Comments 14

wjl December 3 2009, 15:34:47 UTC
woohoo, functional programming! except *real* FPLs actually support recursion :P

P.S. that essay is great!

Reply

gustavolacerda December 3 2009, 20:28:26 UTC
yes it is. As of last week, he's my hero. Check out his other writings: http://www.scottaaronson.com/writings/

Have you seen the Gödel CAPTCHA: http://www.scottaaronson.com/writings/captcha.html

Reply

gustavolacerda December 4 2009, 02:11:49 UTC
R supports recursion:

> fact <- function(n) ifelse(n==1,1,n*fact(n-1))
> fact(7)
[1] 5040

I honestly don't know why it breaks down here.

Reply

wjl December 4 2009, 06:04:23 UTC
my off-the-cuff guess was that it has a far-too-conservative recursion depth limit to cut off "infinite looping". my tongue-in-cheek comment about supporting recursion is just to say that in a functional programming language, such deep recursion is *expected* :) it's really funny to me how some language designers have such a limited idea of how deep recursion can be and still be useful.

Reply


gustavolacerda December 4 2009, 08:26:38 UTC
um, I think I need to use:

hyper <- function(fn) function(a,b) Reduce(fn, rep(a,b), right=TRUE)

foldr, not foldl.

Reply

wjl December 5 2009, 04:51:45 UTC
i don't see why it shouldn't work with either -- i just tried it in SML and both work fine

Reply

wjl December 5 2009, 04:55:16 UTC
or anyway, neither loops infinitely.. i think hyper is indeed incorrect if it's not a fold right

Reply

gustavolacerda December 5 2009, 11:41:56 UTC
yeah, that's what I was saying.

Reply


Leave a comment

Up