K
Kevin Bullock
Take the following two recursive implementations of Euclid's algorithm,
the first in Guile, the second in Ruby:
(define (euclid m n)
(if (= n 0)
m
(euclid n (remainder m n))))
def euclid(m, n)
if n == 0 then m
else euclid(n, m % n)
end
end
The results of running each function (which are both the same) are as
follows:
guile> (euclid (random (expt 10 1000)) (random (expt 10 1000)))
irb(main):001:0> euclid(rand(10**1000),rand(10**1000))
SystemStackError: stack level too deep
Why is it that guile can handle such recursion but Ruby can't? Is it
that guile is implemented with iterative recursion in mind, and so
optimizes for it better?
Pacem in terris / Mir / Shanti / Salaam / Heiwa
Kevin R. Bullock
the first in Guile, the second in Ruby:
(define (euclid m n)
(if (= n 0)
m
(euclid n (remainder m n))))
def euclid(m, n)
if n == 0 then m
else euclid(n, m % n)
end
end
The results of running each function (which are both the same) are as
follows:
guile> (euclid (random (expt 10 1000)) (random (expt 10 1000)))
irb(main):001:0> euclid(rand(10**1000),rand(10**1000))
SystemStackError: stack level too deep
Why is it that guile can handle such recursion but Ruby can't? Is it
that guile is implemented with iterative recursion in mind, and so
optimizes for it better?
Pacem in terris / Mir / Shanti / Salaam / Heiwa
Kevin R. Bullock