<?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>Michi's blog &#187; J</title>
	<atom:link href="http://blog.mikael.johanssons.org/archive/category/computer/programming/j/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mikael.johanssons.org</link>
	<description>Because my LiveJournal is too silly</description>
	<lastBuildDate>Sat, 12 Nov 2011 15:09:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>J, or how I learned to stop worrying and love the matrix</title>
		<link>http://blog.mikael.johanssons.org/archive/2008/12/j-or-how-i-learned-to-stop-worrying-and-love-the-matrix/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2008/12/j-or-how-i-learned-to-stop-worrying-and-love-the-matrix/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 07:50:17 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[J]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/?p=186</guid>
		<description><![CDATA[Or actually, I haven&#8217;t quite yet. But, out of a whim, I downloaded J and started to play with it while reading this set of neat notes on Functional Programming and J. And &#8230; well &#8230; my reaction so far is kinda &#8220;Buh!? What the *** just happened there?&#8221; The first example I ran across, [...]]]></description>
			<content:encoded><![CDATA[<p>Or actually, I haven&#8217;t quite yet.</p>
<p>But, out of a whim, I downloaded <a href="http://www.jsoftware.com">J</a> and started to play with it while reading <a href="http://www.cs.trinity.edu/~jhowland/math-talk/functional1/">this</a> set of neat notes on Functional Programming and J.</p>
<p>And &#8230; well &#8230; my reaction so far is kinda &#8220;Buh!? What the *** just happened there?&#8221;</p>
<p>The first example I ran across, tried to read, and finally managed to is the following:</p>
<div class="dean_ch" style="white-space: wrap;">
+/ , 5 = q: &gt;: i. 100<br />
&nbsp;</div>
<p>This snippet is supposed to tell us how many 0s are trailing 100!. To get at this, we first need to figure out what, exactly, is done here. First observation is that 100! is the product of all integers from 1 to 100. The second is that the number of 0s trailing this is the same as the lower of the orders of 2 and 5, respectively, dividing 100!. Which, in turn is the lower of the numbers of 2s and 5s occurring in the totality of all prime decompositions of all the integers from 1 to 100.</p>
<p>Now, if <img src='/latexrender/pictures/ebc139755bf12a13ba04795b06a8123a.png' title='k\cdot 5^n&lt;100' alt='k\cdot 5^n&lt;100' align='middle' />, then certainly <img src='/latexrender/pictures/2f4c31ed665290ef6adc71ebca6d6ad9.png' title='k\cdot 2^n&lt;100' alt='k\cdot 2^n&lt;100' align='middle' />, so the sum of orders of 2 is guaranteed to be larger than the sum of orders of 5, so it is enough to count the number of 5s dividing any of the numbers along the way. And THIS is what the program above computes.</p>
<p>Next thing to figure out is how it does this. It turns out, after some experimenting, that the best way of reading this is to go through from right to left and figure out what it all does. All the functions we're looking at end up being applied in a <i>monadic</i> fashion &#8211; which means something completely different in J than in Haskell &#8211; here it means that the function gets applied to a single argument. So, here goes:<br />
<code lang="J">i. n</code> lists the first n integers, starting at 0. So <code lang="J">i. 100</code> is the 1&#215;100 matrix containing 0, 1, 2, &#8230;, 99.<br />
<code lang="J">&gt;:</code>, when applied monadically, increments each element it sees by one. So <code lang="J">&gt;: i. 100</code> is the 1&#215;100 matrix containing 1, 2, 3, &#8230;, 100.<br />
<code lang="J">q:</code> yields a prime factorization of the integers it sees. So from <code lang="J">q: &gt;: i. 100</code> we get a matrix where each row carries the primes dividing the row number, with 0 padding at the end.<br />
Then we see the one <i>dyadic</i> (as opposed to monadic) application in this program. <code lang="J">5 = mtx</code> yields a matrix of the same shape as mtx, but with a 1 whenever the corresponding entry is a 5, and 0 otherwise. So <code lang="J">5 = q: &gt;: i. 100</code> picks out the entries in all the prime factorizations that are equal to 5.<br />
Then <code lang="J">,</code>. This is one of two complementary operators to reshape matrices. <code lang="J">,</code> gets us an 1xn matrix with the same entries, read row by row, from left to right, as we started with &#8211; whereas <code lang="J">n m $ mtx</code> takes the matrix in mtx and builds an nxm matrix out of it. Thus, <code lang="J">, 5 = q: &gt;: i. 100</code> flattens out the matrix with the 0 and 1 we got out above.<br />
And finally, <code lang="J">+/</code> is an example of a J adverb being used. <code lang="J">+</code> is a dyadic function corresponding to, as expected, usual addition. <code lang="J">/</code> is a monadic adverb that takes a dyadic function, and inserts it between all the elements in the following list. This is essentially identical to the Haskell call <code lang="Haskell">foldr</code>. So <code lang="J">+/</code> is more commonly known as &#8220;sum&#8221;. And thus, <code lang="J">+/ , 5 = q: &gt;: i. 100</code> sums up the 0s and 1s produced by picking out all the entries in the prime factorization matrix that are equal to 5, after flattening the matrix. Or, in other words, counts the 1 entries. Which is the same as counting the number of 5s in the prime factorizations of all the integers between 1 and 100.<br />
Note that if we skipped the flattening step given by <code lang="J">,</code> then the application of <code lang="J">+/</code> would have just summed each column in the matrix. So, we would have needed to apply it again to get the total tally.</p>
<p>So far, my impression is essentially that WHOA! That language is compact to the point of insanity. Once you master the vocabulary, it kinda gives the feeling that the system has insane amounts of power &#8211; but the vocabulary is kinda imposing, as is the shift in the way I think about programming. I foresee learning J giving me the same kinds of epiphanies and major brain twists as Haskell did &#8211; so it might well be an appropriate next challenge.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2008/12/j-or-how-i-learned-to-stop-worrying-and-love-the-matrix/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

