<?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>Paul Butler &#187; Ruby</title>
	<atom:link href="http://paulbutler.org/archives/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://paulbutler.org</link>
	<description></description>
	<lastBuildDate>Mon, 25 Jul 2011 10:41:36 +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>Why R doesn&#039;t suck</title>
		<link>http://paulbutler.org/archives/why-r-doesnt-suck/</link>
		<comments>http://paulbutler.org/archives/why-r-doesnt-suck/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 13:45:01 +0000</pubDate>
		<dc:creator>Paul Butler</dc:creator>
				<category><![CDATA[Data Mining]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://paulbutler.org/?p=336</guid>
		<description><![CDATA[I first encountered the R programming language a few years ago when I needed to make some plots. Although I&#8217;ve used it occasionally since, I always considered it a sort of &#8220;Perl for statisticians&#8221; &#8212; a useful swiss-army knife with &#8230; <a href="http://paulbutler.org/archives/why-r-doesnt-suck/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I first encountered the <a href="http://www.r-project.org/">R programming language</a> a few years ago when I needed to make some plots. Although I&#8217;ve used it occasionally since, I always considered it a sort of &#8220;Perl for statisticians&#8221; &mdash; a useful swiss-army knife with ugly syntax and inconsistent semantics. My workflow generally involved manipulating the data in Python and using R to make a simple plot, minimizing the amount of R code I wrote as much as possible.</p>
<p>When I recently decided to sit down and properly learn the language, I was pleasantly surprised that underneath the line noise was an interesting and unique language. R is a descendant of LISP and, deep down, maintains some of the beauty its ancestor. It also borrows some unique and interesting features from other functional and dynamic languages.</p>
<h3>Code is Data</h3>
<p>R is true to its LISP roots in that you can create, modify, and evaluate parse trees from the code itself. One way to do so is with the <samp>quote()</samp> special-function, which returns its argument, unevaluated, as an expression object that can be traversed, modified and evaluated.</p>
<p>A fun (though not especially useful) consequence of this is that you can write an <a href="http://en.wikipedia.org/wiki/Quine_(computing)">expression which returns itself</a> as a quote:<br />
<code><br />
> (function(x) substitute((x)(x)))(function(x) substitute((x)(x)))<br />
(function(x) substitute((x)(x)))(function(x) substitute((x)(x)))<br />
> expression <- (function(x) substitute((x)(x)))(function(x) substitute((x)(x)))<br />
> expression == eval(expression)<br />
[1] TRUE<br />
</code></p>
<h3>Optional Laziness</h3>
<p>By default, R uses <a href="http://en.wikipedia.org/wiki/Eager_evaluation">eager evaluation</a>, so expressions are evaluated as soon as they are assigned. However, R takes after functional languages like Haskell and OCaml in that it allows lazy evaluation, where expressions are only evaluated at the time they are first used.</p>
<p>For example, consider the Haskell code:<br />
<code><br />
m = sum [1..]<br />
</code></p>
<p>Where <samp>sum</samp> returns the sum of a list and <samp>[1..]</samp> is the (infinite) list of all natural numbers. In most languages, the assignment would cause the program to loop forever trying to sum all the natural numbers so it can assign that value to <samp>m</samp>. In Haskell, the assignment does complete; it simply assigns the expression <samp>sum [1..]</samp> to <samp>m</samp> so that it can be evaluated when the value of <samp>m</samp> is first used.</p>
<p>In R we can accomplish something similar with the <samp>delayedAssign()</samp> function:<br />
<code><br />
delayedAssign("m", sum(1:Inf))<br />
</code></p>
<p>Note that in R, unlike OCaml, the variables may be explicitly made lazy with <samp>delayedAssign</samp>, but are evaluated automatically when they are used.</p>
<p>Unfortunately, R evaluates lazy variables when they are pointed to by a data structure, even if their value is not needed at the time. This means that infinite data structures, one common application of laziness in Haskell, are not possible in R.</p>
<h3>Operators are functions</h3>
<p>When using higher-order functions, it&#8217;s sometimes useful to be able to treat operators as functions. Python accomplishes this in a clunky way: there is an <samp>operator</samp> module which redefines the built-in operators as functions. R takes a more functional approach. As in Haskell and O&#8217;Caml, operators are just syntactic sugar for ordinary functions. Enclosing any operator in backticks lets you use it as if it were an ordinary function. For example, calling <samp>`+`(2, 3)</samp> returns <samp>5</samp>.</p>
<p>In fact, both the infix and prefix form are indistinguishable once they are parsed.<br />
<code><br />
> quote(3 + 4) == quote(`+`(3, 4))<br />
[1] TRUE<br />
</code></p>
<p>One surprising fact in R is that the assignment operators (<samp>&lt;-</samp>, <samp>&lt;&lt;-</samp> and <samp>=</samp>) are functions like any other. As a result, they can be overwritten or passed around as desired, though neither strikes me as a particularly good idea.</p>
<h3>Continuations</h3>
<p><a href="http://en.wikipedia.org/wiki/Continuation">Continuations</a> in R are a way of &#8220;breaking out&#8221; of a computation and jumping down the call stack to return early. The R function <samp>callCC()</samp> (<strong>call</strong> with <strong>c</strong>urrent <strong>c</strong>ontinuation) takes one argument, a function. It then evaluates that function, passing in a special function as an argument. <samp>callCC()</samp> then returns the first value that the special function is called with, or the return value of evaluating its argument if the special function is not called before the function returns.</p>
<p>To give you a better idea of what that looks like, consider this example:<br />
<code><br />
> callCC(function(m) {return(4)})<br />
[1] 4<br />
> callCC(function(m) {m(2); return(4)})<br />
[1] 2<br />
</code></p>
<p>Calling the function <samp>m(2)</samp> essentially cuts the computation short, drops down in the call stack to <samp>callCC</samp>, and returns <samp>2</samp>.</p>
<p>If you&#8217;ve used continuations in another language, note that in R the exit function can only be called before <samp>callCC()</samp> returns. This makes R&#8217;s continuation semantics less powerful than those of languages like Scheme, Smalltalk, and Ruby.</p>
<p>R is not without its flaws and legacy baggage (you can trace its roots back to the <a href="http://en.wikipedia.org/wiki/S_(programming_language)">S programming language</a> 35 years ago), but once you learn to use it right, it&#8217;s a very powerful and indispensable language.</p>
]]></content:encoded>
			<wfw:commentRss>http://paulbutler.org/archives/why-r-doesnt-suck/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Garden Path Sentences</title>
		<link>http://paulbutler.org/archives/garden-path-sentences/</link>
		<comments>http://paulbutler.org/archives/garden-path-sentences/#comments</comments>
		<pubDate>Wed, 27 Jun 2007 17:05:27 +0000</pubDate>
		<dc:creator>Paul Butler</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.paulbutler.org/archives/garden-path-sentances/</guid>
		<description><![CDATA[I recently came across an interesting post on the Powerset Blog recently about garden path sentences. Garden path sentences are sentences that lead you down the wrong path through a string of words with multiple meanings. For example, The complex &#8230; <a href="http://paulbutler.org/archives/garden-path-sentences/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently came across an interesting post on the Powerset Blog recently about <a href="http://blog.powerset.com/2007/6/18/search-engines-leaking-oil-for-holes">garden path sentences</a>. Garden path sentences are sentences that lead you down the wrong path through a string of words with multiple meanings. For example,</p>
<blockquote><p>The complex houses married and single students and their families</p></blockquote>
<p>In this case, most readers would probably think <em>complex</em> was an adjective that modified the plural noun <em>houses</em>. The post ended with a challenge &#8211; how easy would it be to create a program to automatically generate these sentences. Since school is out and I have some free time, I tried it myself. I found a decent <a href="http://www.ibiblio.org/webster/">free xml dictionary</a>, and wrote a Ruby script to parse the important bits (the type of word and alternate forms) into an SQL database. I cross-checked all the words against a word frequency table to make sure there were no obscure words. I then wrote a Python script to put the words together into a (hopefully meaningful, but not often) sentence. <strike>I put the Python script onto my server so you can play with it <em>here</em></strike> <em><strong>April 2009 Update</strong>: I removed the live demo as part of a server move</em>.</p>
<p><a href="http://test.paulbutler.org/wp-content/uploads/2009/04/gardenpath.png"><img src="http://test.paulbutler.org/wp-content/uploads/2009/04/gardenpath.png" alt="His concrete spheres foster complexities" title="gardenpath" width="300" height="70" class="size-full wp-image-110" /></a></p>
<p>As you can see, the sentences that it comes up with are far from meaningful. However, in most cases you can at least see how a reader could be taken down the wrong path (at least in the cases where there is a right path). In the above example, concrete could be an adjective or a noun, and spheres could be a noun or a verb (to form a sphere). Foster could be an adjective or a noun depending on the context, but I couldn&#8217;t see the reader seeing it as an adjective here. Certainly the sentence generator leaves a lot to be desired (especially considering that this was one of the better sentences), but I got about as far with it as I expected to. I think it could be improved further with a few modifications:</p>
<ul>
<li>Words in the database are already cross-checked to make sure they aren&#8217;t obscure, but often a word will be common as a noun and uncommon as a verb, or vice versa. I didn&#8217;t have a dataset that allowed me to determine if this was the case for a particular word.</li>
<li>The valency of verbs is ignored. All verbs are assumed to be transitive, even though valency information is available in the database.</li>
<li>I underestimated the difficulty of having a computer generate a meaningful sentence. It is difficult to determine what verbs are compatible with what nouns, I guess you would need to parse a large amount of English text (perhaps some of <a href="http://www.gutenberg.org/wiki/Main_Page">Project Gutenberg</a> &#8211; I think Wikipedia would not be varied enough but I could be wrong).</li>
</ul>
<p>I noticed later that <a href="http://blog.dkbza.org/2007/06/powerset-and-garden-path.html">Ero Carrera</a> had taken a similar approach to what I did, but with his linguistics experience he better anticipated the problems I ran into. He has some good ideas, and his post is an interesting read.</p>
]]></content:encoded>
			<wfw:commentRss>http://paulbutler.org/archives/garden-path-sentences/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

