<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Assertion Through Structure &#187; Programming Languages</title>
	<atom:link href="http://www.blog.dannygagne.com/archives/category/programming-languages/feed" rel="self" type="application/rss+xml" />
	<link>http://www.blog.dannygagne.com</link>
	<description>Manipulating the future one day at a time</description>
	<lastBuildDate>Sun, 15 Jan 2012 16:14:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>A Prototype Factorial</title>
		<link>http://www.blog.dannygagne.com/archives/19</link>
		<comments>http://www.blog.dannygagne.com/archives/19#comments</comments>
		<pubDate>Tue, 29 May 2007 02:42:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming Languages]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[So I’ve been a fan of prototype based programming since I first came across SELF a few years ago. I was talking to duncan over the weekend about various OO/PL type things and we realized that neither of us had seen factorial, the standard functional programming demo, implemented in a prototype langauge. This being the [...]]]></description>
			<content:encoded><![CDATA[<p>So I’ve been a fan of prototype based programming since I first came across SELF a few years ago.  I was talking to duncan over the weekend about various OO/PL type things and we realized that neither of us had seen factorial, the standard functional programming demo, implemented in a prototype langauge.  This being the case, I set out to rectify it.  </p>
<p>Here is an implementation in Javascript (tested in firefox)<br />
<code></p>
<pre>
	Number.prototype.factorial = function ()
	{
		if (this == 1)
			return 1;

		return this * (new Number (this -1)).factorial ();
	};

	//Test
	myNum = new Number (10);
	alert (\"Factorial of \" +myNum +\": \" +myNum.factorial ());
</pre>
</p>
<p></code></p>
<p>So you can see it’s not that bad, but its not as good as it could be.  </p>
<p>Now the following code <b>DOESN’T WORK.</b>  This is how I wish I could program it.<br />
<code></p>
<pre>

   	NaturalNumber.prototype.factorial = function () {return this * (this -1).factorial()};

	0.factorial = function () { return 1; }
</pre>
<p></code><br />
Now this code leverages the power of a prototype based language.  This code doesn’t work since JavaScripts doens’t treat literal #’s (1, 42, 1337, etc) as instances of the Number object, and doesn’t define a collection of natural #’s.</p>
<p>It defines a factorial function for every Natural # (integers &gt;= 0).  We have one special case 0, 0! = 1.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.dannygagne.com/archives/19/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

