Eduardo Cavazos wrote:
Here's a version in FP style, just for fun.
Joy has a 'binrec' (binary recursion) combinator. See this article for
details:
http://www.latrobe.edu.au/philosophy/phimvt/joy/j01tut.html
Here's an interpretation of it in Scheme:
(define (binrec f g h i j)
(define (fun x)
(if (f x)
(g x)
(j (fun (h x))
(fun (i x)))))
fun)
Next exhibit at the zoo:
----------------------------------------------------------------------
(import (rnrs) (ikarus))
(define (binrec f g h i j)
(define (fun x)
(if (f x)
(g x)
(j (fun (h x))
(fun (i x)))))
fun)
(define (constant x)
(lambda (y)
x))
(define (less-than= x)
(lambda (y)
(<= y x)))
(define (sub x)
(lambda (y)
(- y x)))
(define fib
(binrec (less-than= 1)
(constant 1)
(sub 1)
(sub 2)
+))
(time (fib 34))
----------------------------------------------------------------------
It does better than the ultra FP one:
~ # ikarus --r6rs-script /scratch/_fib-binrec-a.scm
running stats for (fib 34):
no collections
2536 ms elapsed cpu time, including 0 ms collecting
2599 ms elapsed real time, including 0 ms collecting
0 bytes allocated
~ #
Ed