Derick Eddington wrote:
As you can see, the same algorithm took mine 7.2 seconds and took Factor
only 0.6 second.
Here's a version in FP style, just for fun.
I have many of the supporting procedures in separate libraries
(sometimes more general versions, as macros) but included them here for
clarity.
----------------------------------------------------------------------
(import (rnrs)
(ikarus))
(define (bi f g c)
(lambda (x)
(c (f x)
(g x))))
(define (uni f c)
(lambda (x)
(c (f x))))
(define (sub x)
(lambda (y)
(- y x)))
(define (constant x)
(lambda (y)
x))
(define (ifte f g h)
(lambda (x)
(if (f x)
(g x)
(h x))))
(define (less-than= x)
(lambda (y)
(<= y x)))
(define fib
(ifte (less-than= 1)
(constant 1)
(bi (uni (sub 1) (lambda (x) (fib x)))
(uni (sub 2) (lambda (x) (fib x))) +)))
(time (fib 34))
----------------------------------------------------------------------
~ # ikarus --r6rs-script /scratch/_fp-fib-b.scm
running stats for (fib 34):
no collections
4088 ms elapsed cpu time, including 0 ms collecting
4159 ms elapsed real time, including 0 ms collecting
0 bytes allocated
~ #
For comparison, here's the standard Scheme version:
----------------------------------------------------------------------
(import (rnrs)
(ikarus))
(define (fib n)
(if (<= n 1)
1
(+ (fib (- n 1))
(fib (- n 2)))))
(time (fib 34))
----------------------------------------------------------------------
~ # ikarus --r6rs-script /scratch/_scheme-fib-a.sps
running stats for (fib 34):
no collections
889 ms elapsed cpu time, including 0 ms collecting
889 ms elapsed real time, including 0 ms collecting
0 bytes allocated
~ #
Finally, the Factor version ran in 1.422944 seconds.
Aziz, do you have any suggestions for improving the time of the FP version?
Ed