Factorial in Lisp
Example for versions
JScheme 7.2,
MIT/GNU Scheme 7.7.9,
guile 1.8.5
This example uses tail-recursive factorial calculation. Note that GNU Guile and MIT/GNU Scheme print correct result, while JScheme has values of factorial starting with 13! wrong due to overflow.
(define (factorial x)
(if (< x 2)
1
(* x (factorial (- x 1)))))
(do ((i 0 (+ i 1)))
((> i 16))
(display (string-append (number->string i) "! = "))
(display (number->string (factorial i)))
(newline))
Comments
]]>blog comments powered by Disqus
]]>