<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Factorial code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 01:45:45 GMT</pubDate>
    <description>DZone Snippets: Factorial code</description>
    <item>
      <title>factorials in many incarnations</title>
      <link>http://snippets.dzone.com/posts/show/2717</link>
      <description>// http://www.cs.indiana.edu/~sganz/publications/icfp99/paper.pdf&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;;; simple definition of factorial:&lt;br /&gt;;; this will run into trouble due to precision&lt;br /&gt;&lt;br /&gt;(define (factorial-1  num)&lt;br /&gt;  (if (= num 0)&lt;br /&gt;      1&lt;br /&gt;      (* num (factorial-1 (- num 1)))))&lt;br /&gt;&lt;br /&gt;;; factorial using imported GMP package:&lt;br /&gt;;; it works better but runs into trouble with too deep call stack&lt;br /&gt;&lt;br /&gt;(load "gmp.lsp")&lt;br /&gt;&lt;br /&gt;(define (factorial-gmp num)&lt;br /&gt;  (if (= num 0)&lt;br /&gt;      "1"&lt;br /&gt;      (GMP:* (string num) (factorial-gmp (- num 1)))))&lt;br /&gt;&lt;br /&gt;;; Finally, a trampoline style recursive definition of&lt;br /&gt;;; factorial which simulates tail recursive implementation&lt;br /&gt;;; avoiding call stack overflow&lt;br /&gt;&lt;br /&gt;(define (factorial-acc num acc)&lt;br /&gt;  (if (= num 0)&lt;br /&gt;      (land (string acc))&lt;br /&gt;      (bounce factorial-acc (- num 1) (GMP:* (string num) (string acc)))))&lt;br /&gt;&lt;br /&gt;(define (factorial-trampoline num)&lt;br /&gt;  (trampoline factorial-acc num 1))&lt;br /&gt;&lt;br /&gt;(define (trampoline func num acc)&lt;br /&gt;  (set 'bouncer (bounce func num acc))&lt;br /&gt;  (catch (while 1&lt;br /&gt;		(set 'bouncer (bouncer))&lt;br /&gt;		(if (not (lambda? bouncer))&lt;br /&gt;		    (throw bouncer)))))&lt;br /&gt;&lt;br /&gt;(define (bounce)&lt;br /&gt;  (letex (func (args 0) num (args 1) acc (args 2))&lt;br /&gt;	(lambda() (func num acc))))&lt;br /&gt;&lt;br /&gt;(define (land val)  val)&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 26 Sep 2006 02:53:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2717</guid>
      <author>frontera000 (bob bae)</author>
    </item>
    <item>
      <title>Factorial in Scheme</title>
      <link>http://snippets.dzone.com/posts/show/1734</link>
      <description>;Calculate the factorial of a number&lt;br /&gt;;(factorial 5) = 1 * 2 * 3 * 4 * 5 = 120&lt;br /&gt;;(factorial 50) = 30414093201713378043612608166064768844377641568960512000000000000&lt;br /&gt;&lt;code&gt;&lt;br /&gt;(define factorial&lt;br /&gt;  (lambda (n)&lt;br /&gt;    (if (= n 0) 1&lt;br /&gt;        (* n (factorial (- n 1))))))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 23 Mar 2006 12:59:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1734</guid>
      <author>willpost ()</author>
    </item>
  </channel>
</rss>
