<?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; Programming</title>
	<atom:link href="http://paulbutler.org/archives/category/programming/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>Do we really need another programming language?</title>
		<link>http://paulbutler.org/archives/do-we-really-need-another-programming-language/</link>
		<comments>http://paulbutler.org/archives/do-we-really-need-another-programming-language/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 03:39:24 +0000</pubDate>
		<dc:creator>Paul Butler</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://paulbutler.org/?p=239</guid>
		<description><![CDATA[Yesterday, a group inside Google released a new programming language called go. Among the many comments on the new language, I noticed a number of people rhetorically asking if there is really room for more programming languages. I&#8217;ve seen the &#8230; <a href="http://paulbutler.org/archives/do-we-really-need-another-programming-language/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Yesterday, a group inside Google released a new programming language called <a href="http://golang.org">go</a>. Among the many comments on the new language, I noticed a number of people rhetorically asking if there is really room for more programming languages. I&#8217;ve seen the same sentiment expressed before, seemingly whenever someone releases a pet language (Arc, Factor) or rediscovers an older programming language (Smalltalk, Erlang, LISP).</p>
<p>I&#8217;m convinced that there is, in fact, room for more programming languages.</p>
<p>I recently read The Mythical Man Month, the 1975 classic about software engineering. Two things stuck out at me. First, that compared to programmers in 1975, programmers today are spoiled by hardware. Many software constraints, like memory and CPU speed &mdash; let alone the size of the program on disk &mdash; are not issues worth much thought for many software projects. There are of course exceptions: low-level systems code, graphics, data-intensive calculations (in other words, &#8220;the fun stuff&#8221;) &mdash; but in general, application programmers face far fewer constraints than they did 35 years ago. The second thing that struck me is how comparatively little programming has changed since then. Despite the loosening of constraints, we still write applications that, say, directly use pointer arithmetic and manually allocate and deallocate memory.</p>
<p>It&#8217;s not just C++, either. Here&#8217;s an experiment. Consider the last program you wrote in any language. Imagine you had to describe, in plain English, your program. The description must be complete enough that another programmer could read it and produce your program. Not your program exactly, of course, but one that satisfies the same requirements your program does. (If you actually try this, you may get something that looks like it was written by a lawyer who charges by the comma. That&#8217;s fine.)</p>
<p>Now compare your description to the program&#8217;s source code. The source code is almost certainly several times longer, less intuitive, and less descriptive. And yet, if your description is precise enough, all the source code does is communicate the same idea to the compiler.</p>
<p>There are two main things that differentiate the source from your description. One is that the source is designed so that a computer can parse it. This amounts to having some regular structure and a well-defined meaning to all the symbols. The other difference is that the description defines <strong>what</strong> the program should do, and the source describes <strong>how</strong>. These differences both contribute to the verbosity of the source code, but I suspect the latter contributes more (consider how concise formal math notation can be, despite having structure and being well-defined).</p>
<p>The obvious way to make a language more expressive, then, is to make it more declarative &mdash; to describe <em>what</em> the program does, not <em>how</em> it does it. Ideally our programs could be completely declarative. We could translate the description we wrote for the experiments a few paragraphs back into a more formal language, without adding any implementation details. Then we would have a compiler that created a program which satisfied our description and produced an executable. This type of programming is actually possible in certain, constrained domains (SQL and Prolog, for example), but unfortunately there isn&#8217;t a good general algorithm to translate any program we describe declaratively into machine instructions.</p>
<p>The best we can do, then, is to give imperative languages more declarative features. In fact, many high-level language features fall under this category. Take continuations, which let you say &#8220;resume code at this point&#8221; instead of how &#8220;store these pieces of state there, then restore the state and return here when called&#8221;. Or list comprehensions, which let you say &#8220;give me a list satisfying these properties&#8221;, rather than &#8220;loop through these elements and do that to them, discarding them if this is not true&#8221;. Or iteration constructs, which let you say &#8220;do this stuff for each element in that&#8221;, without specifying that space must be allocated for a counter, how to look up each element, or that the counter is incremented after each element.</p>
<p>All of those concepts have existed for decades. When available, they make code easier to write, understand, and modify. But even today, if you want to develop a native, cross-platform application, you have to give them all up or resort to using a relatively obscure language like Haskell.</p>
<p>It&#8217;s unfair to expect C++ to evolve to support these features, because it has decades of legacy code that it must be backwards compatible with. The language must be improved by either breaking legacy code or adding language features in an awkward sort of tacked-on way. Neither works very well, so the language evolves slowly or not at all.</p>
<p>I don&#8217;t mean to knock C++; sometimes it&#8217;s exactly what the problem needs. But I think a lot of programmers&#8217; time is wasted solving high level problems with low level tools.</p>
<p>I&#8217;m doubtful that any one language will ever be the right tool for every job. Certain abstractions work well with certain domains (continuations with web programming, the actor model with concurrency, etc.), and abstractions that are baked into the language tend to have advantages to those that aren&#8217;t.</p>
<p>That&#8217;s why I welcome new programming languages. And as long as programming languages are less expressive than written English, I&#8217;ll keep welcoming them.</p>
]]></content:encoded>
			<wfw:commentRss>http://paulbutler.org/archives/do-we-really-need-another-programming-language/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

