<?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>Amateur Topologist &#187; planet sipb</title>
	<atom:link href="http://www.amateurtopologist.com/blog/tag/planet-sipb/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.amateurtopologist.com/blog</link>
	<description>Everything but topology.</description>
	<lastBuildDate>Sun, 15 Jan 2012 21:33:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha-19719</generator>
		<item>
		<title>Some useful approximations</title>
		<link>http://www.amateurtopologist.com/blog/2010/12/01/some-useful-approximations/</link>
		<comments>http://www.amateurtopologist.com/blog/2010/12/01/some-useful-approximations/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 21:08:57 +0000</pubDate>
		<dc:creator>Patrick Hurst</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[approximations]]></category>
		<category><![CDATA[planet sipb]]></category>

		<guid isPermaLink="false">http://www.amateurtopologist.com/?p=1139</guid>
		<description><![CDATA[As much as I hate to admit it, mathematicians tend to deal with approximations. A lot of times, formulas are just too complicated to deal with the full complicated formula, and you have to simplify it. So here&#8217;s some handy approximations, as well as about where they&#8217;re valid. for for , and in particular and [...]]]></description>
			<content:encoded><![CDATA[<p>As much as I hate to admit it, mathematicians tend to deal with approximations. A lot of times, formulas are just too complicated to deal with the full complicated formula, and you have to simplify it. So here&#8217;s some handy approximations, as well as about where they&#8217;re valid.</p>
<ul>
<li><img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-29c2df2d395e505f990630d60ed449b1_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#92;&#108;&#111;&#103;&#32;&#40;&#49;&#43;&#120;&#41;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#120;&#44;&#32;&#101;&#94;&#120;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#49;&#43;&#120;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/> for <img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-fce1ba7d20bf6c0d962850a7d4765ae2_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#120;&#32;&#92;&#108;&#108;&#32;&#49;" title="Rendered by QuickLaTeX.com" style="vertical-align: -2px;"/></li>
<li><img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-ae9b37084eff6a648ce24c831ba472a6_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#40;&#49;&#43;&#120;&#41;&#94;&#123;&#110;&#125;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#49;&#43;&#110;&#120;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/> for <img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-714aa64c731d65eb06e8f96645875bfb_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#110;&#120;&#32;&#92;&#108;&#108;&#32;&#49;" title="Rendered by QuickLaTeX.com" style="vertical-align: -2px;"/>, and in particular <img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-3df1019aeda51474f4c8ce98bc740b03_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#92;&#115;&#113;&#114;&#116;&#123;&#49;&#43;&#120;&#125;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#49;&#43;&#120;&#47;&#50;" title="Rendered by QuickLaTeX.com" style="vertical-align: -5px;"/> and <img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-7c80e89ab9ddc88bd3e1c00095f43e76_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#40;&#49;&#43;&#120;&#41;&#94;&#123;&#45;&#49;&#125;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#49;&#45;&#120;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/></li>
<li><img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-e0d2bd62d300b1f0318e0ea1068d705b_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#92;&#115;&#105;&#110;&#32;&#120;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#120;&#44;&#32;&#92;&#99;&#111;&#115;&#32;&#120;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#49;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/> for <img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-fce1ba7d20bf6c0d962850a7d4765ae2_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#120;&#32;&#92;&#108;&#108;&#32;&#49;" title="Rendered by QuickLaTeX.com" style="vertical-align: -2px;"/></li>
<li><img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-954b197293c6df86583546cc6dee5166_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#40;&#49;&#43;&#92;&#102;&#114;&#97;&#99;&#123;&#97;&#125;&#123;&#110;&#125;&#41;&#94;&#123;&#98;&#110;&#125;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#101;&#94;&#123;&#97;&#98;&#125;" title="Rendered by QuickLaTeX.com" style="vertical-align: -6px;"/> for <img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-9ec5d761fa8cc0bcccac3cde2ddd5a95_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#110;&#32;&#92;&#103;&#103;&#32;&#97;&#44;&#98;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/>, especially for <img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-7d1ec18e47305f7b79bea9eda4432159_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#97;&#44;&#98;&#32;&#60;&#32;&#49;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/></li>
<li><img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-ba174d5bbe28e69561fc4bc7a0a90b9c_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#110;&#33;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#92;&#115;&#113;&#114;&#116;&#123;&#50;&#32;&#92;&#112;&#105;&#32;&#110;&#125;&#32;&#92;&#108;&#101;&#102;&#116;&#40;&#92;&#102;&#114;&#97;&#99;&#123;&#110;&#125;&#123;&#101;&#125;&#92;&#114;&#105;&#103;&#104;&#116;&#41;&#94;&#110;" title="Rendered by QuickLaTeX.com" style="vertical-align: -7px;"/></li>
<li><img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-27f11dd142481fbee0e3cfbd4cfdcb69_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#72;&#95;&#110;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#92;&#108;&#110;&#32;&#110;&#32;&#43;&#32;&#92;&#103;&#97;&#109;&#109;&#97;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/>, where <img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-121828c0361beaf80f8ef522c1a2a327_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#92;&#103;&#97;&#109;&#109;&#97;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#48;&#46;&#53;&#55;&#55;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/> is the <a href="http://en.wikipedia.org/wiki/Euler-Mascheroni_constant">Euler-Mascheroni constant</a></li>
<li><img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-75c6b8d85fc5294bf65a9dd37619d103_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#92;&#112;&#105;&#40;&#110;&#41;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#110;&#32;&#47;&#32;&#92;&#108;&#110;&#32;&#110;" title="Rendered by QuickLaTeX.com" style="vertical-align: -5px;"/>, where <img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-75feb426ac191638e99058ceae07291a_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#92;&#112;&#105;&#40;&#110;&#41;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/> is the number of primes less than <img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-a63eb5ff0272d3119fa684be6e7acce8_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#110;" title="Rendered by QuickLaTeX.com" style="vertical-align: 0px;"/></li>
<li><img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-0a831e2dade0c3ef82838dcc92202cbb_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#112;&#40;&#110;&#41;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#110;&#32;&#92;&#108;&#110;&#32;&#110;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/>, where <img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-9a471dde523ab0f5584da8db4327ec9e_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#112;&#40;&#110;&#41;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/> is the <img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-a63eb5ff0272d3119fa684be6e7acce8_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#110;" title="Rendered by QuickLaTeX.com" style="vertical-align: 0px;"/>th prime number.</li>
<li><img src="http://www.amateurtopologist.com/blog/wp-content/ql-cache/quicklatex.com-24c3fc31465d577951b6e6c4c46c83e9_l3.png?9d7bd4" class="ql-img-inline-formula" alt="&#50;&#94;&#123;&#49;&#48;&#125;&#32;&#92;&#97;&#112;&#112;&#114;&#111;&#120;&#32;&#49;&#48;&#94;&#51;" title="Rendered by QuickLaTeX.com" style="vertical-align: -1px;"/></li>
</ul>
<p>What do you think every mathematician should know?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amateurtopologist.com/blog/2010/12/01/some-useful-approximations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook&#8217;s privacy settings: another look</title>
		<link>http://www.amateurtopologist.com/blog/2010/11/22/facebooks-privacy-settings-another-look/</link>
		<comments>http://www.amateurtopologist.com/blog/2010/11/22/facebooks-privacy-settings-another-look/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 23:34:21 +0000</pubDate>
		<dc:creator>Patrick Hurst</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[planet sipb]]></category>
		<category><![CDATA[privacy]]></category>
		<category><![CDATA[social media]]></category>
		<category><![CDATA[the internet]]></category>

		<guid isPermaLink="false">http://www.amateurtopologist.com/?p=1105</guid>
		<description><![CDATA[Much has been made in the news recently about Facebook&#8217;s relative lack of privacy controls, and the degree to which they&#8217;re hidden and made unintuitive to use. Naturally, people have been speculating about why they do this. The intuitive answer, and the one that I&#8217;ve heard a lot of people claim, is that it allows [...]]]></description>
			<content:encoded><![CDATA[<p>Much has been made in the news recently about Facebook&#8217;s relative lack of privacy controls, and the degree to which they&#8217;re hidden and made unintuitive to use. Naturally, people have been speculating about why they do this. The intuitive answer, and the one that I&#8217;ve heard a lot of people claim, is that it allows them to sell data to advertisers; the reasoning goes that they can&#8217;t sell your data to third parties and make money off of you if your profile settings are all set to private. But as far as I know this isn&#8217;t the case; I&#8217;ve got all my Facebook data pretty locked down; the only information about me if you&#8217;re not my friend is my name and a picture of me, which I figure is enough to allow people I know to friend me while being certain that they&#8217;re getting the right me. Yet I still get ads that I know are targeted to me because they mention my college, my location, etc.</p>
<p>Instead, I suspect that the real money that Facebook makes off of people who don&#8217;t have their information private is off advertising impressions. A lot of people, when they want to find more about someone, will immediately check Facebook to see if they have a profile, and if they do, they&#8217;ll spend a while &#8216;stalking&#8217; them on it. If the &#8216;stalkee&#8217; has their information private and the stalker isn&#8217;t friends with them, then they have to send a friend request, which means that most of the time they&#8217;ll back off. But if the stalkee&#8217;s information is public, then the stalker can spend large amounts of time looking at their information. Which means large amounts of pageviews, and large amounts of advertisements being displayed to them, which means more money for Facebook. And I think that that&#8217;s one point that a lot of people miss when they write about Facebook; not only do they want you to put all of your information on there so they can sell it, they want you to put your information on there so that other people will spend time on their site looking at your information.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amateurtopologist.com/blog/2010/11/22/facebooks-privacy-settings-another-look/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Perl will never go away, ever</title>
		<link>http://www.amateurtopologist.com/blog/2010/05/20/perl-will-never-go-away-ever/</link>
		<comments>http://www.amateurtopologist.com/blog/2010/05/20/perl-will-never-go-away-ever/#comments</comments>
		<pubDate>Fri, 21 May 2010 00:00:44 +0000</pubDate>
		<dc:creator>Patrick Hurst</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[planet sipb]]></category>

		<guid isPermaLink="false">http://www.amateurtopologist.com/?p=866</guid>
		<description><![CDATA[Perl was one of the first languages that I ever learned and actually truly did things with; it was the first language I ever wrote a nontrivial program in (a DES implementation that I have unfortunately lost the source code to, or else I would post it). The first language I ever wrote a program [...]]]></description>
			<content:encoded><![CDATA[<p>Perl was one of the first languages that I ever learned and actually truly did things with; it was the first language I ever wrote a nontrivial program in (a DES implementation that I have unfortunately lost the source code to, or else I would post it). The first language I ever wrote a program in was something I don&#8217;t even remember in BASIC; I seem to have blocked all memory of it from my memory, probably for the better. So I have a bit of a soft spot for the language, and so I still have some of my bad habits; since I didn&#8217;t <code class="codecolorer perl default"><span class="perl"><span style="color: #000000; font-weight: bold;">use</span> strict</span></code> or <code class="codecolorer perl default"><span class="perl"><span style="color: #339933;">-</span>w</span></code>, my code would likely be full of uninitialized variables and barewords. It&#8217;s a bad habit, and to this day I still have to be reminded occasionally that other languages, such as Python, do require variables to be declared.</p>
<p>But Perl is old now, and I&#8217;ve mostly moved on to other languages, like Python. I like the object-orientation, the support for functional paradigms and other nice things like <a class="zem_slink" href="http://en.wikipedia.org/wiki/List_comprehension" title="List comprehension" rel="wikipedia">list comprehensions</a> and lambda functions. I like not having to sigil all of my variables with $ or @ or %, I like being able to supply keyword arguments to my functions so that I don&#8217;t have to remember which weird order I decided to use, I like the sheer amount of fun things that you can do with object orientation combined with reflection, metaprogramming, and everything being a <a class="zem_slink" href="http://en.wikipedia.org/wiki/First-class_object" title="First-class object" rel="wikipedia">first-class object</a>. And yet, I still think it&#8217;ll stick around for a while.</p>
<p>Why do I say that? Simple. I was talking with someone who had left in the middle of an online IRC-based role-playing game, and they had asked for chatlogs of what had happened after they left. I had them, since I run weechat in tmux (like irssi in screen, but better!) and so am in every IRC channel I&#8217;m in 24/7. But the question was: how could I pull out just the lines that were said when he left? And the answer was Perl. It turns out that the .. operator, which in a for loop or other situations where a list is expected produces a range (so (1..9) as a list produces the list (1,2,3,4,5,6,7,8,9)), does something completely different in a scalar context, like in the conditional of an if statement. Take the statement <code class="codecolorer perl default"><span class="perl"><a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/Person.*has quit/</span> <span style="color: #339933;">..</span> <span style="color: #009966; font-style: italic;">/Person.*has joined/</span><span style="color: #009900;">&#41;</span></span></code>. Each time this statement is run, the conditional will evaluate to false, until the left-hand side evaluates to true. Then it&#8217;ll start evaluating to true, until the right-hand side evaluates to false, and then it&#8217;ll stop being true (but it&#8217;ll still be true until it&#8217;s evaluated again!), etc., etc. So if this is in an implicit while loop running through the lines of a file, it&#8217;ll start printing when it sees a line saying Person has quit, including that line, then stop when they rejoin, but still print that line, and then it&#8217;ll keep going until it sees another quit line, etc. And the best part is, if you call perl with -n, you automatically get a while loop that assigns the current line of the file it&#8217;s reading from to $_, the implicit variable in the matching and print.</p>
<p>If I wanted to do that in something like Python, I&#8217;d have to manually set up the read loop, write a function to trawl through, build up regexp objects to match on, etc. And that&#8217;s fine for a piece of code I intend to maintain. But for a quick one-line script like this? Too much effort. All I need is perl -ne.</p>
<div class="zemanta-pixie" style="margin-top:10px;height:15px"><img class="zemanta-pixie-img" alt="" src="http://img.zemanta.com/pixy.gif?x-id=4d5cdf8f-420b-4f32-8a10-4d20fa6ef966" style="border:none;float:right"><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.amateurtopologist.com/blog/2010/05/20/perl-will-never-go-away-ever/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Security vulnerability in Haskell with CGI</title>
		<link>http://www.amateurtopologist.com/blog/2010/04/23/security-vulnerability-in-haskell-with-cgi/</link>
		<comments>http://www.amateurtopologist.com/blog/2010/04/23/security-vulnerability-in-haskell-with-cgi/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 19:18:11 +0000</pubDate>
		<dc:creator>Patrick Hurst</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[cgi]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[planet sipb]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.amateurtopologist.com/?p=821</guid>
		<description><![CDATA[Compiled Haskell programs all include special RTS (Run Time System) options, that change things like the number of cores that it runs on, various internal things relating to how often garbage collection runs, etc. They&#8217;re specified by invoking the program like ./foo +RTS -m10 -k2000 -RTS to run the GHC-compiled program &#8216;foo&#8217;, reserving 10% of [...]]]></description>
			<content:encoded><![CDATA[<p>Compiled Haskell programs all include special <a title="RTS (Run Time System) options" href="http://www.haskell.org/ghc/docs/latest/html/users_guide/runtime-control.html">RTS (Run Time System) options</a>, that change things like the number of cores that it runs on, various internal things relating to how often garbage collection runs, etc. They&#8217;re specified by invoking the program like ./foo +RTS -m10 -k2000 -RTS to run the GHC-compiled program &#8216;foo&#8217;, reserving 10% of the heap for allocation and setting each thread&#8217;s stack size to a maximum of 2000 bytes. In the current build of GHC, there is no way to disable these options from working (although the option &#8211;RTS will make all further options be interpreted as normal, non-RTS options). The problem is that the option -tout will write profiling data to the file out. So, if your program is setuid root, anybody who runs it can write the profiling data to, say /etc/passwd and render the system unusable. They don&#8217;t get to pick what gets written, so they can&#8217;t add a backdoor for themselves, but they can essentially scribble over whatever files they want. This is bug <a href="http://hackage.haskell.org/trac/ghc/ticket/3910">#3910</a>, and the fix (disabling RTS by default) has been uploaded.</p>
<p>Now, one of the more little-known features of CGI is that if you pass a <a class="zem_slink" title="Query string" rel="wikipedia" href="http://en.wikipedia.org/wiki/Query_string">query string</a> that does not contain any = signs to a CGI script, the httpd may pass the string along as command-line arguments. This is specified in section 4.4 of <a href="http://www.ietf.org/rfc/rfc3875">RFC 3875</a>, and it specifies how the query string SHOULD be turned into arguments (although it does not say anything about whether the httpd should behave this way, only that some do). <a href="http://code.amateurtopologist.com/args.pl?foo%20bar+%2BRTS+2">This</a> is an example script that only outputs its arguments in a comma-separated list; the link gives it some sample arguments. Note that by URL-escaping, you can send arbitrary strings through&#8230; including +RTS. So if that were, say, a Haskell script, I could pass the query string ?%2BRTS+-tindex.html+-RTS and overwrite index.html.</p>
<p>There are three ways to get around this: first, GHC 6.12.2 has the -no-rtsopts option, which will obviously disable RTS options. So if you just recompile your script with that, it&#8217;ll be safe. Note that 6.14 will disable the RTS options by default; the 6.12.2 patch didn&#8217;t for backwards-compatibility reasons. Second, if you don&#8217;t want to use 6.12.2 for whatever reason, you can wrap it in a shell script that calls it with no options. For example, replace the Haskell script with a shell script called, say, hscript.cgi (if your Haskell program is called hscript) that calls it with no arguments, e.g.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/bin/bash</span><br />
.<span style="color: #000000; font-weight: bold;">/</span>hscript.real</div></div>
<p>and rename the Haskell script to hscript.real, so that it doesn&#8217;t get run as CGI (I&#8217;m assuming that .real files don&#8217;t get run as CGI on your machine!) Another thing you can do is to add the following to your .htaccess, which will give 403 Forbidden errors to anybody passing RTS arguments in the URL:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">RewriteEngine on<br />
RewriteCond %{QUERY_STRING} ^(?:[^=]*\+)?(?:%2[bB]|(?:-|%2[dD]){1,2})(?:%52|R)(?:%54|T)(?:%53|S)(?:\+[^=]*)?$<br />
RewriteRule ^ - [F]</div></div>
<p>This will solve it for every Haskell script you use, but relies on the regex being correct, which isn&#8217;t something I can guarantee.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/pixy.gif?x-id=dc920732-9bc7-483f-86ba-973d77aa0634" alt="" /><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.amateurtopologist.com/blog/2010/04/23/security-vulnerability-in-haskell-with-cgi/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>dissociated-blogosphere: never have to write an original post again!</title>
		<link>http://www.amateurtopologist.com/blog/2010/04/06/dissociated-blogosphere-never-have-to-write-an-original-post-again/</link>
		<comments>http://www.amateurtopologist.com/blog/2010/04/06/dissociated-blogosphere-never-have-to-write-an-original-post-again/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 01:18:30 +0000</pubDate>
		<dc:creator>Patrick Hurst</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[dissociated blogosphere]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[planet sipb]]></category>

		<guid isPermaLink="false">http://www.amateurtopologist.com/?p=796</guid>
		<description><![CDATA[For the past two weeks or so, I&#8217;ve been working off and on on a project called dissociated blogosphere (OSX and Linux binaries here). It takes a bunch of URLs, looks through them for an RSS for the raw content of the posts, and then stores the words of the posts in an array. It [...]]]></description>
			<content:encoded><![CDATA[<p>For the past two weeks or so, I&#8217;ve been working off and on on a project called <a href="http://github.com/veinor/dissociated-blogosphere">dissociated blogosphere</a> (OSX and Linux binaries <a href="http://code.amateurtopologist.com/dissociated-blogosphere/bin">here</a>). It takes a bunch of URLs, looks through them for an RSS for the raw content of the posts, and then stores the words of the posts in an array. It then picks N random, consecutive words (where in this case N is 2), and starts generating new text, by picking a new word x% of the time if x% of the time, the previous N words were followed by that word. For example, if 90% of the time, the words &#8216;the quick&#8217; were followed by &#8216;brown&#8217;, and the other 10% of the time, they were followed by &#8216;red&#8217;, then when the two-word phrase &#8216;the quick&#8217; was randomly generated, it would pick &#8216;brown&#8217; 9 times out of 10, and &#8216;red&#8217; 1 time out of 10. This is the algorithm <a class="zem_slink" title="Emacs" rel="homepage" href="http://www.gnu.org/software/emacs/">Emacs</a>&#8216;s <a class="zem_slink" title="Dissociated press" rel="wikipedia" href="http://en.wikipedia.org/wiki/Dissociated_press">dissociated press</a> feature uses, hence the name. Running it a few times on this site and picking some of my favorite sentences gives:</p>
<blockquote><p>
Second, I ignored the axes of the work you envision. So start small, and think about the free group on two generators, which is obviously highly undesirable behavior. However, it does have the web interface, I&#8217;ll have it up by last week, but that obviously didnt happen. Taking into account the fact that I&#8217;m using. The central thing that makes MS Paint Adventures unique to the point where it&#8217;s my go-to language for random programs (I still use Python for that), but if we pick two of them and rotate one of the set of all rotations that you have some custom function you want soup or salad, both is not a valid answer.</p></blockquote>
</blockquote>
<p>It&#8217;s my first medium-scale project written in <a class="zem_slink" title="Haskell (programming language)" rel="homepage" href="http://haskell.org">Haskell</a> (even though there isn&#8217;t a lot of code, what little was there was not trivial to write), and I&#8217;ve learned several lessons from it:</p>
<ul>
<li><strong>The <a title="Haskell wiki" href="http://www.haskell.org/haskellwiki/Haskell">Haskell wiki</a> is an excellent resource.</strong> When I was trying to learn how to use HXT, the <a title="HXT" href="http://www.fh-wedel.de/~si/HXmlToolbox/">Haskell XML Toolbox</a>, I found the provided documentation somewhat inadequate. But the <a title="HXT article on the Haskell wiki" href="http://www.haskell.org/haskellwiki/HXT">HXT article on the Haskell wiki</a> is an excellent introduction to the filter abstraction, which is all that I need for the basic stuff that I&#8217;m using.</li>
<li><strong>Read the Haddock documentation.</strong> The HXT article, as useful as it was, didn&#8217;t cover a couple essential things I needed to know (such as how to pull all elements with type &#8220;application/rss+xml&#8221;). So I look at the documentation for <a title="documentation for Text.XML.HXT.Arrow.XmlArrow" href="http://hackage.haskell.org/packages/archive/hxt/8.5.2/doc/html/Text-XML-HXT-Arrow-XmlArrow.html">Text.XML.HXT.Arrow.XmlArrow</a> (the module containing the arrows that HXT uses to filter XML), and saw that <code class="codecolorer haskell default"><span class="haskell">hasAttrValue <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="color: #cccc00; font-weight: bold;">Bool</span></a><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> a XmlTree XmlTree</span></code> looks about right; from the type, I can guess (correctly) that I need to pass it the attribute and a prediate on the value of the attribute (i.e., <code class="codecolorer haskell default"><span class="haskell">hasAttrValue <span style="background-color: #3cb371;">&quot;href&quot;</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">==</span> <span style="background-color: #3cb371;">&quot;application/rss+xml&quot;</span><span style="color: green;">&#41;</span></span></code>).</li>
<li><strong>One goal at a time.</strong> This isn&#8217;t specific to Haskell. When I started on this, I meant for it to require you to provide the RSS feed. Then, I realized that having a larger corpus might be better, so I added the ability to pull from multiple feeds. Then I decided that expecting people to find the RSS feed by hand might be a bit much, so I rewrote it to pull the RSS feed from the site. And I eventually plan to write a CGI frontend so that you can just run it online. If I had decided from the start to do all these things, I probably never would&#8217;ve gotten started. As <a href="http://web.archive.org/web/20050404020308/http://www.linuxtimes.net/modules.php?name=News&amp;file=article&amp;sid=145">Linus Torvalds said</a>:<br />
<blockquote><p>Nobody should start to undertake a large project. You start with a small trivial project, and you should never expect it to get large. If you do, you&#8217;ll just overdesign and generally think it is more important than it likely is at that stage. Or worse, you might be scared away by the sheer size of the work you envision. So start small, and think about the details. Don&#8217;t think about some big picture and fancy design. If it doesn&#8217;t solve some fairly immediate need, it&#8217;s almost certainly over-designed.</p></blockquote>
</li>
<li><strong>Strip and gzip your executables if you&#8217;re going to distribute them.</strong> Due to the fact that I&#8217;m statically linking in HXT, which is a sizeable library, the compiled, non-stripped version of dissociated-blogosphere is a whopping 12 megabytes. This isn&#8217;t due to inefficiencies in my own code, but due to the sheer size of the HXT library. Running the Unix command line utility <a href="http://en.wikipedia.org/wiki/Strip_(Unix)">strip</a> (which only removes internal debugging information) cuts it down to about 5 MB, and then gzipping the binaries takes it down to a little over a megabyte.</li>
<li><strong>Split things into libraries where it&#8217;s appropriate.</strong> Part of the problem with using HXT is that it makes recompilation slow; if I could do it all over again, I might have used <a href="http://www.haskell.org/HaXml/">HaXml</a>, but HXT has the advantage of having nontrivial amounts of documentation written about it (on the Haskell wiki). If I had instead split the RSS parsing code into its own library, I could have only recompiled those parts whenever I touched them, which wasn&#8217;t nearly as often as I touched the code frontend. Plus, it&#8217;s just good programming practice.</li>
</ul>
<p>So what do I have planned for dissociated-blogosphere? First off, I plan to make it faster by caching RSS lookups; by storing a map from page URLs to RSS feed, I can cut the number of network requests in half. Second, I plan to implement actual error handling; right now if you give it a bad URL it fails and doesn&#8217;t produce any useful output, regardless of whether other URLs are good. Third, I&#8217;m going to split out the RSS part into its own library, which I might make its own package on hackage. Fourth, I intend to eventually write a web interface (either in Haskell or in Python) so that you don&#8217;t have to download and install it. I originally intended to have the web interface up by last week, but that obviously didn&#8217;t happen. Taking into account the fact that it&#8217;ll take longer than I think it does, I&#8217;m guessing I&#8217;ll have it up by two weeks from now (so, a month). And finally, when/if I do the web interface, I&#8217;ll have it color the text according to which blog it&#8217;s from, or maybe even output xterm color codes if I don&#8217;t write the web interface.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/pixy.gif?x-id=d52a7644-8d19-4dd2-b6d2-4603793b4e7c" alt="" /><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.amateurtopologist.com/blog/2010/04/06/dissociated-blogosphere-never-have-to-write-an-original-post-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What comes after reCAPTCHA?</title>
		<link>http://www.amateurtopologist.com/blog/2010/03/16/what-comes-after-recaptcha/</link>
		<comments>http://www.amateurtopologist.com/blog/2010/03/16/what-comes-after-recaptcha/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 23:02:38 +0000</pubDate>
		<dc:creator>Patrick Hurst</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[planet sipb]]></category>

		<guid isPermaLink="false">http://www.amateurtopologist.com/?p=775</guid>
		<description><![CDATA[reCAPTCHA, the system I use to keep spam out of the comments, is probably one of the most popular CAPTCHAs (Completely Automated Method[s] to Tell Computers and Humans Apart) out there. And for a very good reason: it draws its source words only from texts that current optical character recognition (OCR) technology is unable to [...]]]></description>
			<content:encoded><![CDATA[<p>reCAPTCHA, the system I use to keep spam out of the comments, is probably one of the most popular CAPTCHAs (Completely Automated Method[s] to Tell Computers and Humans Apart) out there. And for a very good reason: it draws its source words only from texts that current optical character recognition (OCR) technology is unable to read; therefore, no spam bot should be able to read them, especially after reCAPTCHA applies some extra distortion to render it absolutely non-machine-readable. But what do we do when the state of OCR technology advances to the point where they get as good as humans at reading text? As technology for reading words improves, it seems likely that within the next decade or two, the level of distortion necessary to render it unreadable by machines will also make it illegible to humans. So what next?</p>
<p>One class is image-recognition CAPTCHA: you present the user with ten distorted images (to prevent random guessing by bots) and ask them which ones contain a cat, or which ones have been rotated upside-down, or which ones are people. This is essentially a generalization of text-based CAPTCHAS, but it has several problems. First and foremost, you need a large source of images to show the user.  This is one of the huge advantages of text-based CAPTCHAs: they can be procedurally generated. If the image database for a CAPTCHA service is small, then it&#8217;ll be passed around by spam bots; since recognizing whether two images are the same is a fairly solved problem, all they have to do is answer your question for each of the images once. (The distortion&#8217;s purpose is to make image comparison harder in case spammers do get a hold of your database, not to make it impossible). One method would be to browse Flickr for photos tagged with an object and assume that each such photo contains an object, but you&#8217;re running into copyright issues as well as essentially relying on the fact that someone won&#8217;t tag a photo &#8216;cat&#8217; just because it has a kitten in the distant background.</p>
<p>One other idea that I&#8217;ve seen a couple sites use is knowledge-based, relying on the fact that machines can&#8217;t yet parse natural language. So it asks a question like &#8220;what is 2 plus 2?&#8221;. The fundamental problem I see with this is that, again, you&#8217;re going to have a very small repertoire of questions; a CAPTCHA has to be able to be generated by a computer. Not to mention the fact that whatever question-generating algorithm you use could just be reverse-engineered to extract content, then passed to Google or Wolfram Alpha to get the answer. Unlike images, there&#8217;s no way to &#8216;distort&#8217; a question.</p>
<p>A third possibility, orthogonal to trying to tell real people from computers, is to look at the content of the message, rather than require the message sender to pass some arbitrary test. This is the approach Akismet (which comes by default on WordPress) uses, and is similar to the way e-mail clients detect spam. This has the downside of having a higher false positive rate than CAPTCHA-based methods. A short comment saying &#8216;Hey, I read your article and liked it; check out this link&#8217; can either be legitimate or spam, and determining which one it is would require knowing the contents of the link. So your CAPTCHA system would have to visit links posted by users, which is obviously highly undesirable behavior. However, it does have the advantage of not relying on some problem being &#8216;hard&#8217; to solve, and it also removes the (admittedly small) barrier to commenting that CAPTCHAs produce.</p>
<p>For now, reCAPTCHA will remain good enough; it&#8217;s easy to solve, and the word combinations that I can&#8217;t easily read can be dismissed with a click of the refresh button. And since I have very low traffic, I can afford to have an e-mail sent to me for every comment I get here; if it does wind up being spam (apparently, either reCAPTCHA isn&#8217;t completely impervious to computer solving or there&#8217;s some sweatshop worker whose job is to spam sites with cheap Viagra ads) I can just delete it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amateurtopologist.com/blog/2010/03/16/what-comes-after-recaptcha/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Why I Love Currying</title>
		<link>http://www.amateurtopologist.com/blog/2010/02/12/why-i-love-currying/</link>
		<comments>http://www.amateurtopologist.com/blog/2010/02/12/why-i-love-currying/#comments</comments>
		<pubDate>Sat, 13 Feb 2010 01:35:54 +0000</pubDate>
		<dc:creator>Patrick Hurst</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[planet sipb]]></category>

		<guid isPermaLink="false">http://www.amateurtopologist.com/?p=613</guid>
		<description><![CDATA[So I&#8217;ve been playing around with Haskell a lot lately and using it for various random stuff; I haven&#8217;t progressed to the point where it&#8217;s my go-to language for random programs (I still use Python for that), but I at least have an idea of how to use it. And there&#8217;s one feature of Haskell [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve been playing around with <a href="http://www.haskell.org/">Haskell</a> a lot lately and using it for various random stuff; I haven&#8217;t progressed to the point where it&#8217;s my go-to language for random programs (I still use Python for that), but I at least have an idea of how to use it. And there&#8217;s one feature of Haskell that I miss sorely when I write code in Python, or pretty much any other vaguely functional language: currying.</p>
<p>In Haskell, every function takes a single argument. A function of multiple arguments, such as <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a></span></code>, which applies a function to every element in a list, actually only has one argument; for example, map can be interpreted either as taking a function and a list and returning a list, or as taking a function and returning a function that takes a list and returns a list. More formally, in Haskell, these two type declarations are equivalent:</p>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>b<span style="color: green;">&#93;</span><br />
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>b<span style="color: green;">&#93;</span><span style="color: green;">&#41;</span></div></div>
<p>This process, of taking a multi-argument function and converting it into a series of single-argument functions is known as currying, after the mathematician <a href="http://en.wikipedia.org/wiki/Haskell_Curry">Haskell Curry</a> (who, obviously, is also the source of the name Haskell); the process of partially applying arguments to a function in this way is known as &#8216;partial application&#8217;, but is also called currying. One of the most obvious examples of currying is in sections: the function <code class="codecolorer haskell default"><span class="haskell"><span style="color: green;">&#40;</span><span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">==</span><span style="color: green;">&#41;</span></span></code> is syntactic sugar for <code class="codecolorer haskell default"><span class="haskell"><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">==</span><span style="color: green;">&#41;</span> <span style="color: red;">0</span></span></code>, and returns whether its argument is equal to zero. Furthermore, we can also partially apply the predicate to filter, to make a function that filters its argument on a fixed predicate. So, these three examples are completely equivalent:</p>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">removeZeros <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Integer"><span style="color: #cccc00; font-weight: bold;">Integer</span></a><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Integer"><span style="color: #cccc00; font-weight: bold;">Integer</span></a><span style="color: green;">&#93;</span><br />
removeZeros xs <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span style="font-weight: bold;">filter</span></a> <span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">-&gt;</span> x <span style="color: #339933; font-weight: bold;">/=</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span> xs<br />
removeZeros xs <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span style="font-weight: bold;">filter</span></a> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">/=</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span> xs<br />
removeZeros <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span style="font-weight: bold;">filter</span></a> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">/=</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span></div></div>
<p>(where <code class="codecolorer haskell default"><span class="haskell"><span style="color: #339933; font-weight: bold;">/=</span></span></code> is Haskell&#8217;s not-equal operator). The first is the most explicitly-written version, using no currying at all. The second curries the predicate; <code class="codecolorer haskell default"><span class="haskell"><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">/=</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span> x</span></code> is the same as <code class="codecolorer haskell default"><span class="haskell">x <span style="color: #339933; font-weight: bold;">/=</span> <span style="color: red;">0</span></span></code>. Finally, since <code class="codecolorer haskell default"><span class="haskell">removeZeroes</span></code> applied to an argument is the same as applying <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span style="font-weight: bold;">filter</span></a> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">/=</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span></span></code> to it, we might as well define the former as the latter. Or, to take another example, look at the sortBy: it has type <code class="codecolorer haskell default"><span class="haskell"><span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ordering"><span style="color: #cccc00; font-weight: bold;">Ordering</span></a><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span></span></code>, where Ordering is a datatype that can either be <code class="codecolorer haskell default"><span class="haskell">EQ<span style="color: #339933; font-weight: bold;">,</span> LT</span></code> or <code class="codecolorer haskell default"><span class="haskell">GT</span></code> for equal, less than, or greater than. So if you have some custom function you want to sort a list on, you can just say <code class="codecolorer haskell default"><span class="haskell">mySort <span style="color: #339933; font-weight: bold;">=</span> sortBy f</span></code> and it will be the same as writing <code class="codecolorer haskell default"><span class="haskell">mySort xs <span style="color: #339933; font-weight: bold;">=</span> sortBy f xs</span></code>, only cleaner and neater. Or in <a href="http://github.com/veinor/nimber/blob/master/Data/Nimber.hs">my Data.Nimber module</a> (specifically lines <a href="http://github.com/veinor/nimber/blob/master/Data/Nimber.hs#L38">38</a>, <a href="http://github.com/veinor/nimber/blob/master/Data/Nimber.hs#L39">39</a>, and <a href="http://github.com/veinor/nimber/blob/master/Data/Nimber.hs#L43">43</a>), many operations on Nimbers that&#8217;re required in order for me to call then &#8216;numbers&#8217; are just the identity operation. So instead of saying <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span style="font-weight: bold;">abs</span></a> x <span style="color: #339933; font-weight: bold;">=</span> x</span></code>, I can just say <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span style="font-weight: bold;">abs</span></a> <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a></span></code>.</p>
<p>Furthermore, without currying, you couldn&#8217;t have <a href="http://www.amateurtopologist.com/2010/01/12/variadic-functions-in-haskell/">variadic functions</a>; in order to work inside Haskell&#8217;s type system, the two types <code class="codecolorer haskell default"><span class="haskell">a <span style="color: #339933; font-weight: bold;">-&gt;</span> b <span style="color: #339933; font-weight: bold;">-&gt;</span> c</span></code> and <code class="codecolorer haskell default"><span class="haskell">a <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>b <span style="color: #339933; font-weight: bold;">-&gt;</span> c<span style="color: green;">&#41;</span></span></code> have to be the same type. The full explanation involves typeclasses, and is (in my opinion) worth a read, because it&#8217;s a good explanation of a pretty horriblexcellent (it&#8217;s both at once, you see) type system hack.</p>
<p>As an aside, this also means that <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a> <span style="color: #339933; font-weight: bold;">::</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> a</span></code>, the identity function, is in a sense the same thing as <code class="codecolorer haskell default"><span class="haskell"><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">$</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> b</span></code>, which is function application. You can see this by substituting <code class="codecolorer haskell default"><span class="haskell"><span style="color: green;">&#40;</span>b <span style="color: #339933; font-weight: bold;">-&gt;</span> c<span style="color: green;">&#41;</span></span></code> for <code class="codecolorer haskell default"><span class="haskell">a</span></code> in the type of id, then removing parentheses:</p>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a> <span style="color: #339933; font-weight: bold;">::</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> a<br />
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>b <span style="color: #339933; font-weight: bold;">-&gt;</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>b <span style="color: #339933; font-weight: bold;">-&gt;</span> c<span style="color: green;">&#41;</span><br />
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>b <span style="color: #339933; font-weight: bold;">-&gt;</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> b <span style="color: #339933; font-weight: bold;">-&gt;</span> c</div></div>
<p>So, in particular, <code class="codecolorer haskell default"><span class="haskell">f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>` x</span></code> is the same as <code class="codecolorer haskell default"><span class="haskell">f <span style="color: #339933; font-weight: bold;">$</span> x</span></code>, which is just <code class="codecolorer haskell default"><span class="haskell">f x</span></code>. Another way to think of this is that <code class="codecolorer haskell default"><span class="haskell">f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>` x <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a> f x <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a> f<span style="color: green;">&#41;</span> x <span style="color: #339933; font-weight: bold;">=</span> f x</span></code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amateurtopologist.com/blog/2010/02/12/why-i-love-currying/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Variadic Functions in Haskell</title>
		<link>http://www.amateurtopologist.com/blog/2010/01/12/variadic-functions-in-haskell/</link>
		<comments>http://www.amateurtopologist.com/blog/2010/01/12/variadic-functions-in-haskell/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 22:50:32 +0000</pubDate>
		<dc:creator>Patrick Hurst</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[planet sipb]]></category>

		<guid isPermaLink="false">http://www.amateurtopologist.com/?p=540</guid>
		<description><![CDATA[Most modern languages have some kind of printf analogue: a function that takes a format string, and a series of things to be inserted into that string, and formats them all accordingly. At first glance, Haskell&#8217;s strong type system would seem to preclude this. There&#8217;s no built-in system for writing functions that take variable numbers [...]]]></description>
			<content:encoded><![CDATA[<p>Most modern languages have some kind of printf analogue: a function that takes a format string, and a series of things to be inserted into that string, and formats them all accordingly. At first glance, <a href="http://en.wikipedia.org/wiki/Haskell">Haskell&#8217;s</a> strong type system would seem to preclude this. There&#8217;s no built-in system for writing functions that take variable numbers of arguments, and it seems like it would be difficult to write one. The standard approach is to take a list instead, but this fundamentally doesn&#8217;t work for printf, since you&#8217;re going to be wanting to print Integers, Strings, and Floats. It&#8217;s possible to just pre-apply show to everything, but that&#8217;s not really a good idea, because you might want to show them in a different way than the built-in show does. You can use an extension called existential types to create a list of PrintfWrappers which wrap integers/floats/strings (more on that below), but that requires your users to manually do the wrapping, which is, once again, not a good idea. Haskell&#8217;s <a href="http://haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/Text-Printf.html">Text.Printf</a> module takes a third approach. Look at the following lines:</p>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>IsChar c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> PrintfType <span style="color: green;">&#91;</span>c<span style="color: green;">&#93;</span><br />
<span style="color: #06c; font-weight: bold;">instance</span> PrintfType <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> a<span style="color: green;">&#41;</span><br />
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>PrintfArg a<span style="color: #339933; font-weight: bold;">,</span> PrintfType r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> PrintfType <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> r<span style="color: green;">&#41;</span><br />
<br />
<span style="color: #06c; font-weight: bold;">instance</span> PrintfArg <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Integer"><span style="color: #cccc00; font-weight: bold;">Integer</span></a><br />
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>IsChar c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> PrintfArg <span style="color: green;">&#91;</span>c<span style="color: green;">&#93;</span><br />
<br />
printf <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>PrintfType r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> r</div></div>
<p>Here&#8217;s how to interpret this: PrintfType is the type of things that can be printed to. Printing to a <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a></span></code> just gives you a string, much like sprintf in C or Perl, printing to an <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span></span></code> will actually print it out (so you can use it like a normal printf in do blocks, a behavior which I personally find distasteful.). However, printf will return undefined when asked to return an <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> r</span></code>; the reason that you can nevertheless return one is that only declaring IO () as an instance of PrintfType is invalid according to Haskell 98.</p>
<p><code class="codecolorer haskell default"><span class="haskell">PrintfArg</span></code>, by comparison, are the elements that are valid arguments to <code class="codecolorer haskell default"><span class="haskell">printf</span></code>; they basically consist of the various <code class="codecolorer haskell default"><span class="haskell">WordN<span style="color: #339933; font-weight: bold;">/</span>IntN</span></code> types, <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Integer"><span style="color: #cccc00; font-weight: bold;">Integer</span></a><span style="color: #339933; font-weight: bold;">,</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Float"><span style="color: #cccc00; font-weight: bold;">Float</span></a><span style="color: #339933; font-weight: bold;">,</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Char"><span style="color: #cccc00; font-weight: bold;">Char</span></a></span></code>, and <code class="codecolorer haskell default"><span class="haskell"><span style="color: green;">&#40;</span>IsChar c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: green;">&#91;</span>c<span style="color: green;">&#93;</span></span></code>. The point of the last instance is that, while you can&#8217;t have a specific version of a polymorphic type be an instance of a typeclass, you can restrict it to types whose parameters are themselves instance of another typeclass; the only instance of <code class="codecolorer haskell default"><span class="haskell">isChar</span></code> is <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Char"><span style="color: #cccc00; font-weight: bold;">Char</span></a></span></code>.<br />
So now that we have that clarified, let&#8217;s suppose we want to call printf with &#8220;%s %d %f&#8221; &#8220;foo&#8221; 42 3.1, passing it the format string, String, an Integer, and a Float. This causes printf&#8217;s type to become</p>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">printf <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Integer"><span style="color: #cccc00; font-weight: bold;">Integer</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Float"><span style="color: #cccc00; font-weight: bold;">Float</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a></div></div>
<p>Does this match the pattern <code class="codecolorer haskell default"><span class="haskell"><span style="color: green;">&#40;</span>PrintfType r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> r</span></code>? Let&#8217;s go in reverse. <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a></span></code> is an instance of <code class="codecolorer haskell default"><span class="haskell">PrintfType</span></code>, and <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Float"><span style="color: #cccc00; font-weight: bold;">Float</span></a></span></code> is an instance of <code class="codecolorer haskell default"><span class="haskell">PrintfArg</span></code>, so <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Float"><span style="color: #cccc00; font-weight: bold;">Float</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a></span></code> is an instance of <code class="codecolorer haskell default"><span class="haskell">PrintfType</span></code>. Therefore, <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Integer"><span style="color: #cccc00; font-weight: bold;">Integer</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Float"><span style="color: #cccc00; font-weight: bold;">Float</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a><span style="color: green;">&#41;</span></span></code> is an instance of <code class="codecolorer haskell default"><span class="haskell">PrintfArg</span></code>, and so is <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Integer"><span style="color: #cccc00; font-weight: bold;">Integer</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Float"><span style="color: #cccc00; font-weight: bold;">Float</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></span></code>. Dropping parentheses, this becomes <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Integer"><span style="color: #cccc00; font-weight: bold;">Integer</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Float"><span style="color: #cccc00; font-weight: bold;">Float</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a></span></code>. So the types all check out. If you pass an invalid type, then you&#8217;ll run into something that isn&#8217;t an instance of <code class="codecolorer haskell default"><span class="haskell">PrintfArg</span></code> and so the types won&#8217;t check.</p>
<p>I mentioned above that if you use something called &#8216;existential types&#8217;, you can do something similar. The way it works is that you define a new type whose data constructor only requires that its argument be of a given typeclass. Look at the following example</p>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #5d478b; font-style: italic;">{-# LANGUAGE ExistentialQuantifiers #-}</span><br />
<span style="color: #06c; font-weight: bold;">data</span> Box <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">forall</span> s<span style="color: #339933; font-weight: bold;">.</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Show"><span style="color: #cccc00; font-weight: bold;">Show</span></a> s<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> Box s<br />
<br />
boxes <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>Box <span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span> Box <span style="background-color: #3cb371;">&quot;f&quot;</span><span style="color: #339933; font-weight: bold;">,</span> Box <span style="color: green;">&#91;</span><span style="color: red;">8</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>\<br />
<br />
showBoxes <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span>Box<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a><br />
showBoxes <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;&quot;</span><br />
showBoxes <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>Box x<span style="color: green;">&#41;</span>:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:show"><span style="font-weight: bold;">show</span></a> x <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot; &quot;</span> <span style="color: #339933; font-weight: bold;">++</span> showBoxes xs</div></div>
<p>When you run <code class="codecolorer haskell default"><span class="haskell">showBoxes boxes</span></code>, you get <code class="codecolorer text default"><span class="text">2 &quot;f&quot; 83</span></code>, exactly as you&#8217;d expect. Note that, however, the function <code class="codecolorer haskell default"><span class="haskell">unbox <span style="color: green;">&#40;</span>Box x<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> x</span></code> cannot be written; it would have to be of type <code class="codecolorer haskell default"><span class="haskell"><span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Show"><span style="color: #cccc00; font-weight: bold;">Show</span></a> s<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> Box <span style="color: #339933; font-weight: bold;">-&gt;</span> s</span></code>, and there&#8217;s just no real way to do that. So once you&#8217;ve wrapped something up in a Box, you can only get at it by <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:show"><span style="font-weight: bold;">show</span></a></span></code>ing it. From this, you can see how to pass a heterogeneous list to <code class="codecolorer haskell default"><span class="haskell">printf</span></code>. The reason that this approach is suboptimal is that it would require Text.Printf to export a Printf data constructor which would wrap up everything to make it of the appropriate type, and that would be rather annoying, especially since it relies on show preserving enough information for you to format the number after <code class="codecolorer haskell default"><span class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:read"><span style="font-weight: bold;">read</span></a></span></code>ing it back in.</p>
<p>This pattern can obviously extended to any other variadic, heterogeneous function, as long as you can define a suitable typeclass that its arguments must all be instances of. And that&#8217;s not really a restriction at all; if you can&#8217;t specify a behavior that the instances must have, then you don&#8217;t really know what you can do with the arguments, and so you can&#8217;t do anything at all!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amateurtopologist.com/blog/2010/01/12/variadic-functions-in-haskell/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Electrons are not like planets</title>
		<link>http://www.amateurtopologist.com/blog/2010/01/01/electrons-are-not-like-planets/</link>
		<comments>http://www.amateurtopologist.com/blog/2010/01/01/electrons-are-not-like-planets/#comments</comments>
		<pubDate>Fri, 01 Jan 2010 17:34:00 +0000</pubDate>
		<dc:creator>Patrick Hurst</dc:creator>
				<category><![CDATA[Science]]></category>
		<category><![CDATA[particle physics]]></category>
		<category><![CDATA[physics]]></category>
		<category><![CDATA[planet sipb]]></category>

		<guid isPermaLink="false">http://www.amateurtopologist.com/?p=485</guid>
		<description><![CDATA[One of my first posts on this blog was about the stability of the atomic nucleus; given that it consists of a bunch of positive/neutral charges clumped together, why doesn&#8217;t it fly apart? The answer involves the strong force, which is strong at atomic distances but miniscule at inter-nuclear distances; on distances comparable to that [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://amateurtopologist.com/2009/06/13/why-everything-doesnt-collapse/">One of my first posts on this blog</a> was about the stability of the atomic nucleus; given that it consists of a bunch of positive/neutral charges clumped together, why doesn&#8217;t it fly apart? The answer involves the strong force, which is strong at atomic distances but miniscule at inter-nuclear distances; on distances comparable to that of an atomic nucleus, it&#8217;s strong enough to overcome the electromagnetic repulsion. But there&#8217;s another question, and it involves the model of orbiting electrons.</p>
<p>Classically (meaning without quantum mechanics), the electron is pictured as a pointlike particle orbiting the nucleus. For simplicitly, we&#8217;ll look at the hydrogen atom. The electron orbits at the Bohr radius , <img src='http://s.wordpress.com/latex.php?latex=a_0%3D5.3%20%5Ctimes%2010%5E%7B-13%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='a_0=5.3 \times 10^{-13}' title='a_0=5.3 \times 10^{-13}' class='latex' /> cm, which can&#8217;t really be derived in an easy way from theory; we&#8217;ll just take it as a given. So the acceleration the electron undergoes is <img src='http://s.wordpress.com/latex.php?latex=q%5E2%20%2F%20m_e%20r%5E2&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='q^2 / m_e r^2' title='q^2 / m_e r^2' class='latex' /> (using cgs units to avoid factors of <img src='http://s.wordpress.com/latex.php?latex=1%2F4%5Cpi%20%5Cepsilon_0&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='1/4\pi \epsilon_0' title='1/4\pi \epsilon_0' class='latex' /> and such everywhere), using good old <img src='http://s.wordpress.com/latex.php?latex=F%3Dma&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='F=ma' title='F=ma' class='latex' />.</p>
<p>However, there is a problem: any accelerating charge radiates energy. The reason for this is, roughly speaking, an accelerating charge has more energy in its electromagnetic field, so you therefore have to expend more energy to accelerate it; since an atom is a closed system, there can be no energy source, so it &#8216;extracts&#8217; it by spiraling inwards. The derivation of formula is complicated, but it turns out that a particle of charge <img src='http://s.wordpress.com/latex.php?latex=q&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='q' title='q' class='latex' /> accelerating at a rate <img src='http://s.wordpress.com/latex.php?latex=a&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='a' title='a' class='latex' /> will radiate energy at a rate</p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=P%3D%5Cfrac%7B2%20q%5E2%20a%5E2%7D%7B3%20c%5E3%7D%3D%5Cfrac%7B2q%5E6%7D%7B3m_e%5E2r%5E4c%5E3%7D&#038;bg=ffffff&#038;fg=000000&#038;s=1' alt='P=\frac{2 q^2 a^2}{3 c^3}=\frac{2q^6}{3m_e^2r^4c^3}' title='P=\frac{2 q^2 a^2}{3 c^3}=\frac{2q^6}{3m_e^2r^4c^3}' class='latex' /></p>
<p style="text-align: left;">(the second equation what we get when we plug in our value for acceleration).</p>
<p>So what&#8217;s the energy of an electron orbiting its atom? The kinetic energy is equal to <img src='http://s.wordpress.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7Dmv%5E2%3D%5Cfrac%7B1%7D%7B2%7DrF%3Dq%5E2%2F2r&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\frac{1}{2}mv^2=\frac{1}{2}rF=q^2/2r' title='\frac{1}{2}mv^2=\frac{1}{2}rF=q^2/2r' class='latex' />, and the potential energy is equal to $-q^2/r$, so the total energy is just</p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=E%3D-q%5E2%2F2r&#038;bg=ffffff&#038;fg=000000&#038;s=1' alt='E=-q^2/2r' title='E=-q^2/2r' class='latex' /></p>
<p style="text-align: left;">Considering both energy and radiated power as functions of time, we get <img src='http://s.wordpress.com/latex.php?latex=dE%2Fdt%3D-P&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='dE/dt=-P' title='dE/dt=-P' class='latex' />; but since the only variable that can change is the radius, we then get the following differential equation for the radius:</p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=r%27%3D-%5Cfrac%7B4q%5E4%7D%7B3m%5E2c%5E3r%5E2%7D&#038;bg=ffffff&#038;fg=000000&#038;s=1' alt='r&#039;=-\frac{4q^4}{3m^2c^3r^2}' title='r&#039;=-\frac{4q^4}{3m^2c^3r^2}' class='latex' /></p>
<p style="text-align: left;">The method of solving this equation isn&#8217;t important; I used Mathematica. What <strong>is</strong> important is the solution:</p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=r%3D%5Cleft%28a_0%5E3-%5Cfrac%7B4q%5E4t%7D%7Bc%5E3m%5E2%7D%5Cright%29%5E%7B1%2F3%7D&#038;bg=ffffff&#038;fg=000000&#038;s=1' alt='r=\left(a_0^3-\frac{4q^4t}{c^3m^2}\right)^{1/3}' title='r=\left(a_0^3-\frac{4q^4t}{c^3m^2}\right)^{1/3}' class='latex' /></p>
<p style="text-align: left;">where <img src='http://s.wordpress.com/latex.php?latex=a_0&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='a_0' title='a_0' class='latex' /> is the Bohr radius I mentioned earlier. This will obviously become zero at <img src='http://s.wordpress.com/latex.php?latex=t_0%3D%5Cfrac%7Ba_0%5E3m_e%5E2c%5E3%7D%7B4q%5E4%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='t_0=\frac{a_0^3m_e^2c^3}{4q^4}' title='t_0=\frac{a_0^3m_e^2c^3}{4q^4}' class='latex' />; plugging in the cgs values for these constants, we get that <img src='http://s.wordpress.com/latex.php?latex=t_0%3D3.11%20%5Ctimes%2010%5E%7B-11%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='t_0=3.11 \times 10^{-11}' title='t_0=3.11 \times 10^{-11}' class='latex' /> seconds. So according to the Bohr model, a hydrogen atom decays in less than the time it takes light to move a centimeter.</p>
<p>So what&#8217;s the answer? One is to use the <a href="http://en.wikipedia.org/wiki/Bohr_model">Bohr model</a>, which requires a minimum energy; the electron cannot fall farther into an energy well. This model works to a certain extent, but fails with more complicated atoms; not only that, but it predicts that the hydrogen atom has a minimum nonzero angular momentum, which is not the case. But a full treatment of the failings of the Bohr model goes beyond what I know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amateurtopologist.com/blog/2010/01/01/electrons-are-not-like-planets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using apc
Database Caching 1/38 queries in 0.015 seconds using apc
Object Caching 3363/3439 objects using apc

Served from: www.amateurtopologist.com @ 2012-02-05 21:26:59 -->
