<?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>Lojic Technologies Blog &#187; ruby</title>
	<atom:link href="http://lojic.com/blog/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://lojic.com/blog</link>
	<description></description>
	<lastBuildDate>Fri, 25 Nov 2011 20:12:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Beware of YAML serialization on Linux without libyaml</title>
		<link>http://lojic.com/blog/2011/11/22/beware-of-yaml-serialization-on-linux-without-libyaml/</link>
		<comments>http://lojic.com/blog/2011/11/22/beware-of-yaml-serialization-on-linux-without-libyaml/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 19:44:46 +0000</pubDate>
		<dc:creator>Brian Adkins</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://lojic.com/blog/?p=876</guid>
		<description><![CDATA[The Problem
I noticed some unusual behavior with respect to YAML String serialization between my Linux production system and my Mac OSX development system.
After dumping the production database via pg_dump -O &#8211;no-acl mydb &#124; gzip &#62; ~/mydb.sql.gz and then restoring it on my development system via rake db:drop; rake db:create; psql mydb &#60; mydb.sql, I noticed [...]]]></description>
			<content:encoded><![CDATA[<h1>The Problem</h1>
<p>I noticed some unusual behavior with respect to <a href="http://www.yaml.org/">YAML</a> String serialization between my Linux production system and my Mac OSX development system.</p>
<p>After dumping the production database via pg_dump -O &#8211;no-acl mydb | gzip &gt; ~/mydb.sql.gz and then restoring it on my development system via rake db:drop; rake db:create; psql mydb &lt; mydb.sql, I noticed that a particular <em>serialized</em> field in my Rails app that should always be an Array of String objects occasionally contained Integers.</p>
<p>After a little research and experimentation, I discovered that the production Linux system would occasionally omit quotations around Strings containing only numeric digits. I haven&#8217;t analyzed the pattern fully, but here are some examples where the YAML serialization did or did not use quotes:</p>
<ul>
<li>&#8220;90103&#8243;</li>
<li>000080</li>
<li>&#8220;000071&#8243;</li>
<li>&#8220;000124&#8243;</li>
<li>&#8220;000003&#8243;</li>
<li>008397</li>
<li>000408</li>
<li>000009</li>
<li>000188</li>
<li>&#8220;000021&#8243;</li>
</ul>
<p>Further investigation revealed that the Linux production system was using Syck (a &#8220;dated C implementation of YAML 1.0&#8243;) and my Mac OSX development system was using psych (a &#8220;libyaml wrapper (in Ruby core for 1.9.2)&#8221;). libyaml is a &#8220;fast C implementation of YAML 1.1. So, either the quotation rules have changed between YAML 1.0 and YAML 1.1, or there is a bug in one of the implementations (likely Syck).</p>
<h1>The Solution</h1>
<p>The solution for proper &#8220;future&#8221; behavior is pretty simple. Install libyaml on the Linux system as follows:</p>
<pre>wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
tar xzf yaml-0.1.4.tar.gz
cd yaml-0.1.4
./configure
make
make install</pre>
<p>I <em>think</em> that&#8217;s enough, but I went ahead and rebuilt my Ruby 1.9.2 just in case it needed to know about the existence of libyaml at build time.</p>
<p>The solution for converting my database with YAML 1.0 serialization to YAML 1.1 serialization is a bit trickier. Since the &#8220;dump&#8221; and &#8220;load&#8221; operations are matched for a particular version of YAML, it seems difficult to load the data using YAML 1.0 (thereby retaining the String type when reading an unquoted 000088) and then dump the data using YAML 1.1 (to get proper quoting of &#8216;000088&#8242;). Further complicating this is the fact that Rails handles the serialization operations automatically.</p>
<p>It does appear possible to dynamically switch between syck and psyck by using the following:</p>
<pre>YAML::ENGINE.yamler = 'syck'
YAML::ENGINE.yamler = 'psych'</pre>
<p>So, one option is to repeatedly switch to syck, read in data, switch to psych, and then write the data. &lt;sigh&gt;</p>
<p><strong>Update:</strong></p>
<p>It appears that due to the semantics of the Rails serialize function, it&#8217;s not enough to just read the model object using <em>syck</em> and then immediately write with <em>psych</em> because that doesn&#8217;t appear to be enough to cause the field to be deserialized. I had to refer to the field for each object. This is a pain because it prevents me from doing a generic loop where I can handle all model objects easily w/o reference to their specific fields.</p>
<p>I&#8217;ll withhold judgment for a while, but my first inclination is to consider abandoning YAML serialization for something a little more robust and portable.</p>
<p><strong>Update 2:</strong></p>
<p>It appears my welcome from psych is a serious memory leak. I&#8217;ve been running long running Ruby/Rails processes for years, and this is the first time I&#8217;ve experienced a failure due to an out of memory condition. There are a number of Google hits regarding the issue. After I fix the leak, I&#8217;ll begin researching alternatives to YAML serialization in Rails.</p>
<p><strong>Update 3:</strong></p>
<p>The number of bug reports on psych and rubygems I&#8217;ve had to wade through recently is amazing. My current solution is to remove the psych system gem and install Ruby 1.9.3p0 which required upgrading Passenger to the latest version from source to get Ruby 1.9.3 compatibility. I still had to track down a few odd errors such as &#8220;undefined method `yaml&#8217; for #&lt;Psych::Nodes::Stream:&#8230;&gt;&#8221; and &#8220;invalid date format in specification: &#8220;2011-10-02 00:00:00.000000000Z&#8221;" &#8211; all because I chose to use the default Rails serialization assuming there would be no issues. Lesson learned.</p>
]]></content:encoded>
			<wfw:commentRss>http://lojic.com/blog/2011/11/22/beware-of-yaml-serialization-on-linux-without-libyaml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Language Popularity &#8211; Part Four</title>
		<link>http://lojic.com/blog/2011/09/22/programming-language-popularity-part-four/</link>
		<comments>http://lojic.com/blog/2011/09/22/programming-language-popularity-part-four/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 15:37:07 +0000</pubDate>
		<dc:creator>Brian Adkins</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[arc]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[sml]]></category>

		<guid isPermaLink="false">http://lojic.com/blog/?p=866</guid>
		<description><![CDATA[I compiled some programming language popularity statistics in April 2009, October 2009 and October 2010 . Here&#8217;s an update for September 2011:
I made a number of Google searches of the forms below and summed the results (previous posts averaged the results):
"implemented in &#60;language&#62;"
  "written in &#60;language&#62;"
Naturally this is of very limited utility, and the [...]]]></description>
			<content:encoded><![CDATA[<p>I compiled some programming language popularity statistics in <a href="http://lojic.com/blog/2009/04/21/programming-language-popularity/">April 2009</a>, <a href="http://lojic.com/blog/2009/10/24/programming-language-popularity-part-two/">October 2009</a> and <a href="http://lojic.com/blog/2010/10/08/programming-language-popularity-%E2%80%93-part-three/">October 2010</a> . Here&#8217;s an update for September 2011:</p>
<p>I made a number of Google searches of the forms below and summed the results (previous posts averaged the results):</p>
<pre class="code">"implemented in &lt;language&gt;"
  "written in &lt;language&gt;"</pre>
<p>Naturally this is of very limited utility, and the numbers are only useful when comparing relatively within the same search since the number of results Google returns can vary greatly over time.<br />
<!-- use ML and SML --></p>
<table border="0">
<tbody>
<tr>
<th>Language</th>
<th style="text-align: left;">Total</th>
<th style="text-align: right;">Prev. Position</th>
<th style="text-align: right;"> Position Delta</th>
</tr>
<tr>
<td>C</td>
<td style="text-align: right;">10,360,000</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;"><strong>1</strong></td>
</tr>
<tr>
<td>PHP</td>
<td style="text-align: right;">10,351,000</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>C++</td>
<td style="text-align: right;">6,495,000</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td>Python</td>
<td style="text-align: right;">5,759,000</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;"><strong>1</strong></td>
</tr>
<tr>
<td>C#</td>
<td style="text-align: right;">5,335,000</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Java</td>
<td style="text-align: right;">4,890,000</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;"><strong>2</strong></td>
</tr>
<tr>
<td>Perl</td>
<td style="text-align: right;">3,702,000</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>JavaScript</td>
<td style="text-align: right;">3,077,000</td>
<td style="text-align: right;">7</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>Ruby</td>
<td style="text-align: right;">1,654,000</td>
<td style="text-align: right;">9</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td>Lisp Family<sup>1</sup></td>
<td style="text-align: right;">1,022,870</td>
<td style="text-align: right;">11</td>
<td style="text-align: right;"><strong>1</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>FORTRAN</td>
<td style="text-align: right;">975,600</td>
<td style="text-align: right;">10</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>Tcl</td>
<td style="text-align: right;">594,500</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td>Lisp</td>
<td style="text-align: right;">486,000</td>
<td style="text-align: right;">14</td>
<td style="text-align: right;"><strong>1</strong></td>
</tr>
<tr>
<td>Haskell</td>
<td style="text-align: right;">450,500</td>
<td style="text-align: right;">16</td>
<td style="text-align: right;"><strong>2</strong></td>
</tr>
<tr>
<td>Erlang</td>
<td style="text-align: right;">419,700</td>
<td style="text-align: right;">13</td>
<td style="text-align: right;">-2</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Lua</td>
<td style="text-align: right;">367,100</td>
<td style="text-align: right;">18</td>
<td style="text-align: right;"><strong>2</strong></td>
</tr>
<tr>
<td>ML Family<sup>2</sup></td>
<td style="text-align: right;">348,400</td>
<td style="text-align: right;">17</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td>COBOL</td>
<td style="text-align: right;">308,270</td>
<td style="text-align: right;">15</td>
<td style="text-align: right;">-3</td>
</tr>
<tr>
<td>Common Lisp</td>
<td style="text-align: right;">254,900</td>
<td style="text-align: right;">19</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td>OCaml</td>
<td style="text-align: right;">240,300</td>
<td style="text-align: right;">21</td>
<td style="text-align: right;"><strong>1</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Prolog</td>
<td style="text-align: right;">224,000</td>
<td style="text-align: right;">20</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>Scala</td>
<td style="text-align: right;">203,400</td>
<td style="text-align: right;">23</td>
<td style="text-align: right;"><strong>1</strong></td>
</tr>
<tr>
<td>Scheme</td>
<td style="text-align: right;">184,700</td>
<td style="text-align: right;">22</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>Smalltalk</td>
<td style="text-align: right;">129,700</td>
<td style="text-align: right;">24</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td>Clojure</td>
<td style="text-align: right;">84,600</td>
<td style="text-align: right;">27</td>
<td style="text-align: right;"><strong>2</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>(S)ML<sup>3</sup></td>
<td style="text-align: right;">83,630</td>
<td style="text-align: right;">25</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>Forth</td>
<td style="text-align: right;">69,980</td>
<td style="text-align: right;">26</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>Caml</td>
<td style="text-align: right;">24,470</td>
<td style="text-align: right;">28</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td>Io</td>
<td style="text-align: right;">17,700</td>
<td style="text-align: right;">30</td>
<td style="text-align: right;"><strong>1</strong></td>
</tr>
<tr>
<td>Arc</td>
<td style="text-align: right;">12,670</td>
<td style="text-align: right;">29</td>
<td style="text-align: right;">-1</td>
</tr>
</tbody>
</table>
<p><sup>1</sup> combines Lisp, Scheme, Common Lisp, Arc &amp; Clojure<br />
<sup>2</sup> combines OCaml, (S)ML, Caml<br />
<sup>3</sup> summed separate searches for sml and ml</p>
]]></content:encoded>
			<wfw:commentRss>http://lojic.com/blog/2011/09/22/programming-language-popularity-part-four/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find Longest Substring Palindrome in Haskell</title>
		<link>http://lojic.com/blog/2011/03/13/find-longest-substring-palindrome-in-haskell/</link>
		<comments>http://lojic.com/blog/2011/03/13/find-longest-substring-palindrome-in-haskell/#comments</comments>
		<pubDate>Sun, 13 Mar 2011 06:37:46 +0000</pubDate>
		<dc:creator>Brian Adkins</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://lojic.com/blog/?p=836</guid>
		<description><![CDATA[I stumbled upon a programming challenge a company was using for recruitment purposes and thought I&#8217;d create a Haskell solution as a learning exercise. The first problem was to find the longest palindrome embedded in a text string. 
The following Haskell solution seems very readable to me, but it&#8217;s a naive solution that&#8217;s inefficient. It [...]]]></description>
			<content:encoded><![CDATA[<p>I stumbled upon a programming challenge a company was using for recruitment purposes and thought I&#8217;d create a Haskell solution as a learning exercise. The first problem was to find the longest palindrome embedded in a text string. </p>
<p>The following Haskell solution seems very readable to me, but it&#8217;s a naive solution that&#8217;s inefficient. It computes an answer on my 2.6 year old Macbook Pro in under 4 seconds, but a 2x increase in text requires a 7x increase in CPU time.</p>
<p>I believe there are algorithms to find the longest embedded palindrome in linear time, so I may post a refinement later.</p>
<pre>
-- Find the longest palindrome in a text string.

module Main where
import Char

text = "I'll just type in some example text here and embed a little \
\palindrome - A man, a plan, a canal, Panama! - I expect that will be \
\the longest palindrome found in this text.\
\Lorem ipsum dolor sit amet, consectetur adipiscing elit.\
\Integer volutpat lorem imperdiet ante bibendum ullamcorper. Mauris \
\tempor hendrerit justo at elementum. Vivamus elit magna, accumsan id \
\condimentum a, luctus a ipsum. Donec fermentum, lectus at posuere \
\ullamcorper, mauris lectus tincidunt nulla, ut placerat justo odio sed\
\ odio. Nulla blandit lorem sit amet odio varius nec vestibulum ante \
\ornare. Aliquam feugiat, velit a rhoncus rutrum, turpis metus pretium \
\dolor, et mattis leo turpis non est. Sed aliquet, sapien quis \
\consequat condimentum, sem magna ornare ligula, id blandit odio nisl \
\vitae erat. Nam vulputate tincidunt quam, non lacinia risus tincidunt \
\lacinia. Aenean fermentum tristique porttitor. Nam id dolor a eros \
\accumsan imperdiet. Aliquam quis nibh et dui ultricies cursus. Nunc \
\et ante non sapien vehicula rutrum. Duis posuere dictum blandit. Nunc \
\vitae tempus purus."

clean = map toLower . filter isAlpha

palindrome str = str == reverse str

substrings []     = []
substrings (x:xs) = substrings' (x:xs) ++ substrings xs where
  substrings' []     = []
  substrings' (y:ys) = [y] : [ (y:s) | s <- substrings' ys ]

longest []     = []
longest (x:xs) = if length x &gt; length max then x else max
  where max = longest xs

longest_palindrome xs =
  longest (filter palindrome (substrings (clean text)))

main = print (longest_palindrome text)
</pre>
<p>As a comparison, I translated the program into Ruby. I program predominantly in Ruby these days, and I like it, but the Ruby version is 25 times slower (98 sec. vs. 4 sec.), and it&#8217;s 2.4 times more lines of code (31 vs. 13 &#8211; excluding the text).</p>
<p>A gain in runtime efficiency, expressive power and multi-core capability is very attractive!</p>
<p>I&#8217;m using Ruby 1.9.2 and GHC 6.12.3 on Mac OS X 10.5.8 on a 2.4 GHz Core 2 Duo w/ 4 GB RAM.</p>
<p>ruby 1.9.2p0 (2010-08-18 revision 29036) [i386-darwin9.8.0]<br />
Glasgow Haskell Compiler, Version 6.12.3, for Haskell 98, stage 2 booted by GHC version 6.12.2</p>
<pre>
TEXT = &lt;&lt;END
I'll just type in some example text here and embed a little
palindrome - A man, a plan, a canal, Panama! - I expect that will be
the longest palindrome found in this text.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Integer volutpat lorem imperdiet ante bibendum ullamcorper. Mauris
tempor hendrerit justo at elementum. Vivamus elit magna, accumsan id
condimentum a, luctus a ipsum. Donec fermentum, lectus at posuere
ullamcorper, mauris lectus tincidunt nulla, ut placerat justo odio sed
 odio. Nulla blandit lorem sit amet odio varius nec vestibulum ante
ornare. Aliquam feugiat, velit a rhoncus rutrum, turpis metus pretium
dolor, et mattis leo turpis non est. Sed aliquet, sapien quis
consequat condimentum, sem magna ornare ligula, id blandit odio nisl
vitae erat. Nam vulputate tincidunt quam, non lacinia risus tincidunt
lacinia. Aenean fermentum tristique porttitor. Nam id dolor a eros
accumsan imperdiet. Aliquam quis nibh et dui ultricies cursus. Nunc
et ante non sapien vehicula rutrum. Duis posuere dictum blandit. Nunc
vitae tempus purus.
END

def clean str
  str.gsub(/[^A-Za-z]/,'').downcase
end

def palindrome? str
  str == str.reverse
end

def subs str
  return [] if str.empty?
  y = str[0,1]
  subs(str[1..-1]).inject([y]) do |result, s|
    result &lt;&lt; y + s
    result
  end
end

def substrings str
  return [] if str.empty?
  subs(str) + substrings(str[1..-1])
end

def longest strs
  strs.inject("") do |max, str|
    max = str if str.length &gt; max.length
    max
  end
end

def longest_palindrome str
  longest(substrings(clean(str)).inject([]) {|result, str|
            result &lt;&lt; str if palindrome?(str)
            result
          })
end

puts longest_palindrome(TEXT)
</pre>
<p><strong>Update 3/13/11 19:30</strong><br />
Rick&#8217;s comment (see his blog post linked to in his comment) regarding the importance of algorithm choice is certainly a valid one &#8211; a better algorithm in a slower language may win over an inferior algorithm in a faster language (for large enough datasets). However, what I&#8217;m becoming interested in is the fact that one may gain both productivity/power <strong>and</strong> runtime speed by making wise programming language choices.</p>
<p>In the case of an interpreted language such as Ruby, it&#8217;s helpful to &#8220;stay in C code&#8221; as much as possible. In other words, to favor built-in library routines that have been implemented in C over hand written Ruby code. As much as I like Ruby, this is one of the things that bothers me &#8211; the fact that the Ruby code written by the programmer is vastly inferior in performance to the built-in library routines.</p>
<p>I wasn&#8217;t planning on refining the Haskell version this soon, but after seeing Rick&#8217;s blog post response, I couldn&#8217;t resist :)</p>
<p>I should first provide some background info for context. When I wrote the original Haskell version above, I was in the middle of an online programming challenge, and my goal was simply to compute the answer in a reasonable amount of time to move on to the next challenge, so the brief, easily understandable, Haskell version worked great. Hence, my disclaimers in the post regarding the naivity and inefficiency of the solution. The Ruby code in this post was an afterthought simply to see a comparison between <strong>identical algorithms</strong>. Given that apple &amp; apple comparison, I&#8217;m impressed with the brevity and speed of the Haskell version.</p>
<p>Since I&#8217;m still very much a Haskell newbie, and short on time, I found an <strong>incredible</strong> solution by <a rel="nofollow" href="http://johanjeuring.blogspot.com/2007/08/finding-palindromes.html">Johan Jeuring</a>. It&#8217;s a little bit longer than the Ruby version, but much, <strong>much</strong> faster. It&#8217;s so fast, I had to increase the input quite a bit to get a reasonable comparison &#8211; I replicated the original text 23 times and reversed one of the replications to make a fairly long palindrome.</p>
<p>Rick&#8217;s <strong>Ruby</strong> version took <strong>169.4</strong> seconds, Johan&#8217;s <strong>Haskell</strong> version took <strong>0.032</strong> seconds. In other words, Ruby takes over 5,000 times as long to compute the result. Clearly this is an apples and oranges comparison, but I fully expect that a Ruby version using an identical algorithm will take 100 times as long (or longer) to run and would be less concise. Giving up runtime performance to gain programmer power is one thing, but giving up runtime performance <strong>and</strong> power is a tough pill to swallow.</p>
<p>Here is a slightly modified version of Johan Jeuring&#8217;s code. He was also kind enough to provide his code <a rel="nofollow" href="http://people.cs.uu.nl/johanj/homepage/palindromes/">here</a>:</p>
<pre>
-- Reorganized from Johan Jeuring's solution:
module Main where
import Data.List (maximumBy,intersperse)
import Data.Char
import Data.Array 

text = "I'll just type in some example text here and embed a little \
\palindrome - A man, a plan, a canal, Panama! - I expect that will be \
\the longest palindrome found in this text.\
\Lorem ipsum dolor sit amet, consectetur adipiscing elit.\
\Integer volutpat lorem imperdiet ante bibendum ullamcorper. Mauris \
\tempor hendrerit justo at elementum. Vivamus elit magna, accumsan id \
\condimentum a, luctus a ipsum. Donec fermentum, lectus at posuere \
\ullamcorper, mauris lectus tincidunt nulla, ut placerat justo odio sed\
\ odio. Nulla blandit lorem sit amet odio varius nec vestibulum ante \
\ornare. Aliquam feugiat, velit a rhoncus rutrum, turpis metus pretium \
\dolor, et mattis leo turpis non est. Sed aliquet, sapien quis \
\consequat condimentum, sem magna ornare ligula, id blandit odio nisl \
\vitae erat. Nam vulputate tincidunt quam, non lacinia risus tincidunt \
\lacinia. Aenean fermentum tristique porttitor. Nam id dolor a eros \
\accumsan imperdiet. Aliquam quis nibh et dui ultricies cursus. Nunc \
\et ante non sapien vehicula rutrum. Duis posuere dictum blandit. Nunc \
\vitae tempus purus."

clean = map toLower . filter isAlpha

longestPalindrome input =
  let inputArray      =  listArrayl0 input
      (maxLength,pos) =  maximumBy
                            (\(l,_) (l',_) -> compare l l')
                            (zip (palindromesAroundCentres inputArray) [0..])
  in showPalindrome inputArray (maxLength,pos)

longestPalindromes m input =
  let inputArray =  listArrayl0 input
  in concat $ intersperse "\n"
            $ map (showPalindrome inputArray)
            $ filter ((m<=) . fst)
            $ zip (palindromesAroundCentres inputArray) [0..]

lengthLongestPalindrome :: String -> String
lengthLongestPalindrome = show . maximum . palindromesAroundCentres . listArrayl0

lengthLongestPalindromes :: String -> String
lengthLongestPalindromes = show . palindromesAroundCentres . listArrayl0

palindromesAroundCentres a =
  let (afirst,_) = bounds a
  in reverse $ extendTail a afirst 0 []

extendTail a n currentTail centres
  | n > alast = finalCentres currentTail centres
                   (currentTail:centres)
  | n-currentTail == afirst =
      extendCentres a n (currentTail:centres)
                    centres currentTail
  | a!n == a!(n-currentTail-1) =
      extendTail a (n+1) (currentTail+2) centres
  | otherwise =
      extendCentres a n (currentTail:centres)
                    centres currentTail
  where  (afirst,alast)  =  bounds a

extendCentres a n centres tcentres centreDistance
  | centreDistance == 0 =
      extendTail a (n+1) 1 centres
  | centreDistance-1 == head tcentres  =
      extendTail a n (head tcentres) centres
  | otherwise =
      extendCentres a n (min (head tcentres)
                    (centreDistance-1):centres)
                    (tail tcentres) (centreDistance-1)

finalCentres 0     _        centres  =  centres
finalCentres (n+1) tcentres centres  =
  finalCentres n
               (tail tcentres)
               (min (head tcentres) n:centres)
finalCentres _     _        _        =  error "finalCentres: input < 0"               

showPalindrome a (len,pos) =
  let startpos = pos `div` 2 - len `div` 2
      endpos   = if odd len
                 then pos `div` 2 + len `div` 2
                 else pos `div` 2 + len `div` 2 - 1
  in show [a!n|n <- [startpos .. endpos]]

listArrayl0 string  = listArray (0,length string - 1) string

sampleText s = concat (replicate 8 s ++ [ "x" ] ++ [ reverse s ] ++ replicate 14 s)

main = print (longestPalindrome (clean (sampleText text)))
</pre>
<p>Here's the relevant change to Rick's Ruby version:</p>
<pre>

def sample_text str
  str * 8 + 'x' + str.reverse + str * 14
end

puts clean(sample_text(TEXT)).longest_palindrome
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lojic.com/blog/2011/03/13/find-longest-substring-palindrome-in-haskell/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Programming Language Popularity – Part Three</title>
		<link>http://lojic.com/blog/2010/10/08/programming-language-popularity-%e2%80%93-part-three/</link>
		<comments>http://lojic.com/blog/2010/10/08/programming-language-popularity-%e2%80%93-part-three/#comments</comments>
		<pubDate>Fri, 08 Oct 2010 20:30:00 +0000</pubDate>
		<dc:creator>Brian Adkins</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[arc]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[sml]]></category>

		<guid isPermaLink="false">http://lojic.com/blog/?p=823</guid>
		<description><![CDATA[I compiled some programming language popularity statistics in April 2009 and October 2009 . Here&#8217;s an update for October 2010:
I made a number of Google searches of the forms below and averaged the results:
"implemented in &#60;language&#62;"
  "written in &#60;language&#62;"
Naturally this is of very limited utility, and the numbers are only useful when comparing relatively [...]]]></description>
			<content:encoded><![CDATA[<p>I compiled some programming language popularity statistics in <a href="http://lojic.com/blog/2009/04/21/programming-language-popularity/">April 2009</a> and <a href="http://lojic.com/blog/2009/10/24/programming-language-popularity-part-two/">October 2009</a> . Here&#8217;s an update for October 2010:</p>
<p>I made a number of Google searches of the forms below and averaged the results:</p>
<pre class="code">"implemented in &lt;language&gt;"
  "written in &lt;language&gt;"</pre>
<p>Naturally this is of very limited utility, and the numbers are only useful when comparing relatively within one column since the number of results Google returns can vary greatly over time.<br />
<!-- use ML and SML --></p>
<table border="0">
<tbody>
<tr>
<th>Language</th>
<th style="text-align: right;">Apr 2009</th>
<th style="text-align: right;">Oct 2009</th>
<th style="text-align: right;">Oct 2010</th>
<th style="text-align: right;"> Position Delta</th>
</tr>
<tr>
<td>PHP</td>
<td style="text-align: right;">680,000</td>
<td style="text-align: right;">5,083,500</td>
<td style="text-align: right;">14,096,000</td>
<td style="text-align: right;">+3</td>
</tr>
<tr>
<td>C</td>
<td style="text-align: right;">1,905,500</td>
<td style="text-align: right;">16,975,000</td>
<td style="text-align: right;">9,675,000</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>C++</td>
<td style="text-align: right;">699,000</td>
<td style="text-align: right;">6,270,000</td>
<td style="text-align: right;">6,510,000</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td><strong>C#</strong></td>
<td style="text-align: right;">349,700</td>
<td style="text-align: right;">2,125,000</td>
<td style="text-align: right;">5,132,000</td>
<td style="text-align: right;"><strong>+4</strong></td>
</tr>
<tr>
<td>Python</td>
<td style="text-align: right;">396,000</td>
<td style="text-align: right;">3,407,000</td>
<td style="text-align: right;">5,114,500</td>
<td style="text-align: right;">+1</td>
</tr>
<tr>
<td>Perl</td>
<td style="text-align: right;">365,500</td>
<td style="text-align: right;">3,132,500</td>
<td style="text-align: right;">4,675,000</td>
<td style="text-align: right;">+1</td>
</tr>
<tr>
<td><strong>JavaScript</strong></td>
<td style="text-align: right;">102,700</td>
<td style="text-align: right;">1,163,000</td>
<td style="text-align: right;">2,120,000</td>
<td style="text-align: right;"><strong>+4</strong></td>
</tr>
<tr>
<td>Java</td>
<td style="text-align: right;">850,000</td>
<td style="text-align: right;">5,118,000</td>
<td style="text-align: right;">1,495,500</td>
<td style="text-align: right;">-5</td>
</tr>
<tr>
<td><strong>Ruby</strong></td>
<td style="text-align: right;">99,650</td>
<td style="text-align: right;">227,000</td>
<td style="text-align: right;">1,426,000</td>
<td style="text-align: right;"><strong>+13</strong></td>
</tr>
<tr>
<td>FORTRAN</td>
<td style="text-align: right;"></td>
<td style="text-align: right;">1,621,000</td>
<td style="text-align: right;">770,850</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td>Lisp Family<sup>1</sup></td>
<td style="text-align: right;">176,507</td>
<td style="text-align: right;">3,489,650</td>
<td style="text-align: right;">399,685</td>
<td style="text-align: right;">-6</td>
</tr>
<tr>
<td><strong>Tcl</strong></td>
<td style="text-align: right;">44,800</td>
<td style="text-align: right;">382,000</td>
<td style="text-align: right;">313,400</td>
<td style="text-align: right;"><strong>+5</strong></td>
</tr>
<tr>
<td><strong>Erlang</strong></td>
<td style="text-align: right;">22,285</td>
<td style="text-align: right;">161,700</td>
<td style="text-align: right;">188,800</td>
<td style="text-align: right;"><strong>+12</strong></td>
</tr>
<tr>
<td>Lisp</td>
<td style="text-align: right;">61,900</td>
<td style="text-align: right;">486,500</td>
<td style="text-align: right;">174,050</td>
<td style="text-align: right;">+1</td>
</tr>
<tr>
<td><strong>COBOL</strong></td>
<td style="text-align: right;"></td>
<td style="text-align: right;">247,300</td>
<td style="text-align: right;">166,435</td>
<td style="text-align: right;"><strong>+6</strong></td>
</tr>
<tr>
<td><strong>Haskell</strong></td>
<td style="text-align: right;">22,550</td>
<td style="text-align: right;">280,500</td>
<td style="text-align: right;">157,150</td>
<td style="text-align: right;"><strong>+4</strong></td>
</tr>
<tr>
<td>ML Family<sup>2</sup></td>
<td style="text-align: right;">29,062</td>
<td style="text-align: right;">1,003,800</td>
<td style="text-align: right;">149,005</td>
<td style="text-align: right;">-5</td>
</tr>
<tr>
<td><strong>Lua</strong></td>
<td style="text-align: right;">13,065</td>
<td style="text-align: right;">131,800</td>
<td style="text-align: right;">128,150</td>
<td style="text-align: right;"><strong>+9</strong></td>
</tr>
<tr>
<td>Common Lisp</td>
<td style="text-align: right;">20,600</td>
<td style="text-align: right;">554,500</td>
<td style="text-align: right;">112,750</td>
<td style="text-align: right;">-5</td>
</tr>
<tr>
<td>Prolog</td>
<td style="text-align: right;">17,750</td>
<td style="text-align: right;">390,500</td>
<td style="text-align: right;">100,000</td>
<td style="text-align: right;">-4</td>
</tr>
<tr>
<td>OCaml</td>
<td style="text-align: right;">22,000</td>
<td style="text-align: right;">343,500</td>
<td style="text-align: right;">99,050</td>
<td style="text-align: right;">-3</td>
</tr>
<tr>
<td>Scheme</td>
<td style="text-align: right;">86,450</td>
<td style="text-align: right;">2,100,000</td>
<td style="text-align: right;">82,650</td>
<td style="text-align: right;">-13</td>
</tr>
<tr>
<td><strong>Scala</strong></td>
<td style="text-align: right;">3,570</td>
<td style="text-align: right;">66,250</td>
<td style="text-align: right;">65,950</td>
<td style="text-align: right;"><strong>+6</strong></td>
</tr>
<tr>
<td>Smalltalk</td>
<td style="text-align: right;">9,105</td>
<td style="text-align: right;">187,500</td>
<td style="text-align: right;">56,950</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td>(S)ML<sup>3</sup></td>
<td style="text-align: right;">5,173</td>
<td style="text-align: right;">590,700</td>
<td style="text-align: right;">42,130</td>
<td style="text-align: right;">-12</td>
</tr>
<tr>
<td>Forth</td>
<td style="text-align: right;">6,465</td>
<td style="text-align: right;">146,450</td>
<td style="text-align: right;">25,880</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td>Clojure</td>
<td style="text-align: right;">782</td>
<td style="text-align: right;">62,200</td>
<td style="text-align: right;">23,525</td>
<td style="text-align: right;">+3</td>
</tr>
<tr>
<td>Caml</td>
<td style="text-align: right;">1,889</td>
<td style="text-align: right;">69,600</td>
<td style="text-align: right;">7,825</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td>Arc</td>
<td style="text-align: right;">6,775</td>
<td style="text-align: right;">286,500</td>
<td style="text-align: right;">6,710</td>
<td style="text-align: right;">-10</td>
</tr>
<tr>
<td>Io</td>
<td style="text-align: right;">1,760</td>
<td style="text-align: right;">198,500</td>
<td style="text-align: right;">3,025</td>
<td style="text-align: right;">-7</td>
</tr>
</tbody>
</table>
<p><sup>1</sup> combines Lisp, Scheme, Common Lisp, Arc &amp; Clojure<br />
<sup>2</sup> combines OCaml, (S)ML, Caml<br />
<sup>3</sup> summed separate searches for sml and ml</p>
]]></content:encoded>
			<wfw:commentRss>http://lojic.com/blog/2010/10/08/programming-language-popularity-%e2%80%93-part-three/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Programming Language Popularity &#8211; Part Two</title>
		<link>http://lojic.com/blog/2009/10/24/programming-language-popularity-part-two/</link>
		<comments>http://lojic.com/blog/2009/10/24/programming-language-popularity-part-two/#comments</comments>
		<pubDate>Sat, 24 Oct 2009 15:23:23 +0000</pubDate>
		<dc:creator>Brian Adkins</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[arc]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[forth]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[io]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[ocaml]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[smalltalk]]></category>
		<category><![CDATA[sml]]></category>
		<category><![CDATA[tcl]]></category>

		<guid isPermaLink="false">http://lojic.com/blog/?p=781</guid>
		<description><![CDATA[I compiled some programming language popularity statistics in April and mentioned I&#8217;d update the results in 6 months, so here they are:
I made a number of Google searches of the forms below and averaged the results:
"implemented in &#60;language&#62;"
"written in &#60;language&#62;"



Language
# Results
Apr 09
# Results
Oct 09
Position
Delta


C
1,905,500
16,975,000
0


C++
699,000
6,270,000
+1


Java
850,000
5,118,000
-1


PHP
680,000
5,083,500
0


Lisp Family1
176,507
3,489,650
+3


Python
396,000
3,407,000
-1


Perl
365,500
3,132,500
-1


C#
349,700
2,125,000
-1


Scheme
86,450
2,100,000
+2


FORTRAN

1,621,000
N/A


JavaScript
102,700
1,163,000
-1


ML Family2
29,062
1,003,800
+3



(S)ML3
5,173
590,700
+12


Common Lisp
20,600
554,500
+5


Lisp
61,900
486,500
-2


Prolog
17,750
390,500
+4


Tcl
44,800
382,000
-3


OCaml
22,000
343,500
0


Arc
6,775
286,500
+4


Haskell
22,550
280,500
-4


COBOL

247,300
N/A


Ruby
99,650
227,000
-10


Io
1,760
198,500
+6


Smalltalk
9,105
187,500
-1


Erlang
22,285
161,700
-7


Forth
6,465
146,450
-1


Lua
13,065
131,800
-5


Caml
1,889
69,600
0


Scala
3,570
66,250
-2


Clojure
782
62,200
0



1 combines Lisp, Scheme, Common Lisp, Arc &#38; Clojure
2 [...]]]></description>
			<content:encoded><![CDATA[<p>I compiled some <a href="http://lojic.com/blog/2009/04/21/programming-language-popularity/">programming language popularity</a> statistics in April and mentioned I&#8217;d update the results in 6 months, so here they are:</p>
<p>I made a number of Google searches of the forms below and averaged the results:</p>
<pre class="code">"implemented in &lt;language&gt;"
"written in &lt;language&gt;"</pre>
<table border="0">
<tbody>
<tr>
<th>Language</th>
<th style="text-align: right;"># Results<br />
Apr 09</th>
<th style="text-align: right;"># Results<br />
Oct 09</th>
<th style="text-align: right;">Position<br />
Delta</th>
</tr>
<tr>
<td>C</td>
<td style="text-align: right;">1,905,500</td>
<td style="text-align: right;">16,975,000</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td><strong>C++</strong></td>
<td style="text-align: right;">699,000</td>
<td style="text-align: right;">6,270,000</td>
<td style="text-align: right;"><strong>+1</strong></td>
</tr>
<tr>
<td>Java</td>
<td style="text-align: right;">850,000</td>
<td style="text-align: right;">5,118,000</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>PHP</td>
<td style="text-align: right;">680,000</td>
<td style="text-align: right;">5,083,500</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td><strong>Lisp Family<sup>1</sup></strong></td>
<td style="text-align: right;">176,507</td>
<td style="text-align: right;">3,489,650</td>
<td style="text-align: right;"><strong>+3</strong></td>
</tr>
<tr>
<td>Python</td>
<td style="text-align: right;">396,000</td>
<td style="text-align: right;">3,407,000</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>Perl</td>
<td style="text-align: right;">365,500</td>
<td style="text-align: right;">3,132,500</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>C#</td>
<td style="text-align: right;">349,700</td>
<td style="text-align: right;">2,125,000</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td><strong>Scheme</strong></td>
<td style="text-align: right;">86,450</td>
<td style="text-align: right;">2,100,000</td>
<td style="text-align: right;"><strong>+2</strong></td>
</tr>
<tr>
<td>FORTRAN</td>
<td style="text-align: right;"></td>
<td style="text-align: right;">1,621,000</td>
<td style="text-align: right;">N/A</td>
</tr>
<tr>
<td>JavaScript</td>
<td style="text-align: right;">102,700</td>
<td style="text-align: right;">1,163,000</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td><strong>ML Family<sup>2</sup></strong></td>
<td style="text-align: right;">29,062</td>
<td style="text-align: right;">1,003,800</td>
<td style="text-align: right;"><strong>+3</strong></td>
</tr>
<p><!-- use ML and SML --></p>
<tr>
<td><strong>(S)ML<sup>3</sup></strong></td>
<td style="text-align: right;">5,173</td>
<td style="text-align: right;">590,700</td>
<td style="text-align: right;"><strong>+12</strong></td>
</tr>
<tr>
<td><strong>Common Lisp</strong></td>
<td style="text-align: right;">20,600</td>
<td style="text-align: right;">554,500</td>
<td style="text-align: right;"><strong>+5</strong></td>
</tr>
<tr>
<td>Lisp</td>
<td style="text-align: right;">61,900</td>
<td style="text-align: right;">486,500</td>
<td style="text-align: right;">-2</td>
</tr>
<tr>
<td><strong>Prolog</strong></td>
<td style="text-align: right;">17,750</td>
<td style="text-align: right;">390,500</td>
<td style="text-align: right;"><strong>+4</strong></td>
</tr>
<tr>
<td>Tcl</td>
<td style="text-align: right;">44,800</td>
<td style="text-align: right;">382,000</td>
<td style="text-align: right;">-3</td>
</tr>
<tr>
<td>OCaml</td>
<td style="text-align: right;">22,000</td>
<td style="text-align: right;">343,500</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td><strong>Arc</strong></td>
<td style="text-align: right;">6,775</td>
<td style="text-align: right;">286,500</td>
<td style="text-align: right;"><strong>+4</strong></td>
</tr>
<tr>
<td>Haskell</td>
<td style="text-align: right;">22,550</td>
<td style="text-align: right;">280,500</td>
<td style="text-align: right;">-4</td>
</tr>
<tr>
<td>COBOL</td>
<td style="text-align: right;"></td>
<td style="text-align: right;">247,300</td>
<td style="text-align: right;">N/A</td>
</tr>
<tr>
<td>Ruby</td>
<td style="text-align: right;">99,650</td>
<td style="text-align: right;">227,000</td>
<td style="text-align: right;">-10</td>
</tr>
<tr>
<td><strong>Io</strong></td>
<td style="text-align: right;">1,760</td>
<td style="text-align: right;">198,500</td>
<td style="text-align: right;"><strong>+6</strong></td>
</tr>
<tr>
<td>Smalltalk</td>
<td style="text-align: right;">9,105</td>
<td style="text-align: right;">187,500</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>Erlang</td>
<td style="text-align: right;">22,285</td>
<td style="text-align: right;">161,700</td>
<td style="text-align: right;">-7</td>
</tr>
<tr>
<td>Forth</td>
<td style="text-align: right;">6,465</td>
<td style="text-align: right;">146,450</td>
<td style="text-align: right;">-1</td>
</tr>
<tr>
<td>Lua</td>
<td style="text-align: right;">13,065</td>
<td style="text-align: right;">131,800</td>
<td style="text-align: right;">-5</td>
</tr>
<tr>
<td>Caml</td>
<td style="text-align: right;">1,889</td>
<td style="text-align: right;">69,600</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td>Scala</td>
<td style="text-align: right;">3,570</td>
<td style="text-align: right;">66,250</td>
<td style="text-align: right;">-2</td>
</tr>
<tr>
<td>Clojure</td>
<td style="text-align: right;">782</td>
<td style="text-align: right;">62,200</td>
<td style="text-align: right;">0</td>
</tr>
</tbody>
</table>
<p><sup>1</sup> combines Lisp, Scheme, Common Lisp, Arc &amp; Clojure<br />
<sup>2</sup> combines OCaml, (S)ML, Caml<br />
<sup>3</sup> summed separate searches for sml and ml</p>
]]></content:encoded>
			<wfw:commentRss>http://lojic.com/blog/2009/10/24/programming-language-popularity-part-two/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TriFunc.org</title>
		<link>http://lojic.com/blog/2009/07/22/trifuncorg/</link>
		<comments>http://lojic.com/blog/2009/07/22/trifuncorg/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 14:15:42 +0000</pubDate>
		<dc:creator>Brian Adkins</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[arc]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[logo]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[sml]]></category>

		<guid isPermaLink="false">http://lojic.com/blog/?p=635</guid>
		<description><![CDATA[I first became interested in functional programming when I was exposed to Python, Ruby &#038; JavaScript a number of years ago. Since then I&#8217;ve looked into Arc, Clojure, Common Lisp, Haskell, Logo, ML &#038; Scheme. I haven&#8217;t yet determined whether I&#8217;ll be more productive in any of them than I am with Ruby for developing [...]]]></description>
			<content:encoded><![CDATA[<p>I first became interested in functional programming when I was exposed to Python, Ruby &#038; JavaScript a number of years ago. Since then I&#8217;ve looked into Arc, Clojure, Common Lisp, Haskell, Logo, ML &#038; Scheme. I haven&#8217;t yet determined whether I&#8217;ll be more productive in any of them than I am with Ruby for developing web applications, but I do find them quite interesting.</p>
<p>After bumping into a number of local programmers who expressed an interest in functional programming, I thought it might be a good time to start a local group that focused on functional programming languages, so I did a couple days ago.</p>
<p><a rel="nofollow" href="http://TriFunc.org">TriFunc.org</a> is a group for programmers who are interested in functional programming languages and live near the Research Triangle area of North Carolina.</p>
<p>If you live in the area and have an interest in functional programming languages, feel free to dive in and start participating in the Google Group discussions. Once we reach a critical mass, I expect we&#8217;ll produce a meeting schedule, etc., but that will depend on where the group wants to take this.</p>
]]></content:encoded>
			<wfw:commentRss>http://lojic.com/blog/2009/07/22/trifuncorg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>2009 Programming Language Plan</title>
		<link>http://lojic.com/blog/2009/04/27/2009-programming-language-plan/</link>
		<comments>http://lojic.com/blog/2009/04/27/2009-programming-language-plan/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 17:36:42 +0000</pubDate>
		<dc:creator>Brian Adkins</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[common_lisp]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[logo]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[sml]]></category>

		<guid isPermaLink="false">http://lojic.com/blog/?p=502</guid>
		<description><![CDATA[Background

The 2008 Programming Language Plan didn&#8217;t go as well as I hoped, so I&#8217;m regrouping for another go at it. I did make progress learning some Logo and teaching it to my daughters, and I worked through seven chapters of &#8220;Programming in Haskell&#8221; which was very enjoyable, but I also spent way too much time [...]]]></description>
			<content:encoded><![CDATA[<h2>Background</h2>
<p>
The <a href="http://lojic.com/blog/2008/01/17/2008-programming-language-plan/">2008 Programming Language Plan</a> didn&#8217;t go as well as I hoped, so I&#8217;m regrouping for another go at it. I did make progress learning some Logo and teaching it to my daughters, and I worked through seven chapters of &#8220;Programming in Haskell&#8221; which was very enjoyable, but I also spent way too much time trying to decide which language(s) to learn without actually learning them.
</p>
<p>
I have now decided which languages I want to learn this year, so I figured I&#8217;d post this blog entry. In some respects, things haven&#8217;t changed much from last years plan, but my decisions are much less tentative which is encouraging. I&#8217;m glad to switch from <em>information gathering</em> to actually learning a few languages.
</p>
<h2>Motivation</h2>
<p>
When I switched from C++ to Java in 1996 and noticed a large increase in productivity; however, it didn&#8217;t occur to me to consider whether switching from Java to another language might also give me a similar boost in productivity. The job demand for Java was high during the 10 years I used it, and I was getting paid for my time vs. what I produced, so the oversight wasn&#8217;t too costly.
</p>
<p>
When I switched from Java to Ruby in 2006 I experienced a comparable, if not greater, boost in productivity as I had with the switch from C++ to Java. This time I considered the effect of programming language choice on productivity more carefully and began to wonder about the optimal programming language for me. As a small business owner, my primary consideration is productivity, not popularity or the interchangeableness of programmers.
</p>
<p>
I don&#8217;t think a perfect programming language exists in general, but I think there is an optimal language for me given my particular circumstances, the problems I want to solve, the type of software I want to develop, my way of thinking, my aesthetic tastes, etc.
</p>
<h2>The Problem</h2>
<p>
There is a cost to research programming languages to determine the optimal one, and there is a cost to switch programming languages, so the benefit of a new programming language needs to exceed those costs.
</p>
<p>
Additionally, a significant catch-22 exists in that to truly determine if a language is optimal, one must reach a level of proficiency with the language. There isn&#8217;t enough time to learn <em>all</em> of the candidate languages, so some filtering mechanism must be used first to narrow the choices to a reasonably small set. This filtering mechanism is theoretical by nature, but I think the subtle practicalities of a language have a significant effect on productivity.
</p>
<p>
I have a long term perspective for this task, so I don&#8217;t mind investing time researching programming languages, and I also don&#8217;t mind if I need to develop/acquire supporting libraries before being ultimately more productive than I am currently with Ruby and Rails.
</p>
<h2>The Process</h2>
<p>
I briefly entertained the idea of going about this research in a more systematic, scientific way. That might be a reasonable approach, but due to the following:
</p>
<ul>
<li>My laziness</li>
<li>The high degree of subjectivity in programming language choice</li>
<li>The lack of consensus among programming language researchers</li>
</ul>
<p>
I ended up with a fairly ad-hoc approach involving examining the following areas and trying to absorb enough information to formulate a plan:
</p>
<h3>Efficiency</h3>
<p>
Programming language efficiency is becoming increasingly important to me as Moore&#8217;s law loses steam, and power &amp; cooling issues become more prominent.
</p>
<p>
On the one hand, Ruby is near the bottom of the pack with respect to efficiency, but the productivity has been outstanding, and at the volume of transactions I&#8217;ve needed to deliver thus far, performance hasn&#8217;t been a problem.
</p>
<p>
On the other hand, if programming languages exist (and I believe they do) that have similar or greater <em>power</em> than Ruby but are compiled instead of interpreted, that would be an advantage.
</p>
<p>
Benchmarks are notoriously controversial with respect to the degree in which they predict performance, but I don&#8217;t think they&#8217;re irrelevant. One popular benchmark site is: <a href="http://shootout.alioth.debian.org/">Programming Language Shootout</a>. I also created some micro-benchmarks of my own to test performance.
</p>
<p>
One negative result of Ruby being inefficient is the disparity in performance of code written in Ruby vs. library code, or extensions, written in C. This creates counter-intuitive scenarios where a piece of code that appears less efficient is actually more efficient because of the use of built-in code implemented in C. I don&#8217;t like thinking in those terms; I would much prefer that user written code in the language is much closer to the efficiency of built-in library code.
</p>
<h3>Concurrency</h3>
<p>
With the flattening of CPU MHz and proliferation of CPU cores, I think concurrency may continue to grow in importance. Although I develop mainly server side web applications which can take advantage of multiple cores by virtue of having multiple independent processes, I would prefer to have better concurrency mechanisms in the language/libraries directly.
</p>
<h3>Joy</h3>
<p>
I think I first heard <em>joy</em> used, in the context of programming languages, in the Ruby community &#8211; probably from Matz himself. I have to agree that programming in Ruby has been more joyful than previous programming languages.
</p>
<p>
I&#8217;m not sure exactly why this is, but I expect it has something to do with the effectiveness and productivity of Ruby, but it may be helped by the incredibly friendly Ruby community.
</p>
<h3>Fundamental Nature</h3>
<p>
My preference is for a programming language to minimize arbitrariness and to maximize orthogonality. In other words, I would prefer the language to have a minimal set of core concepts/axioms/operators/etc. that are built upon systematically.
</p>
<h3>Community</h3>
<p>
Whether my optimal language is <em>mainstream</em> or not is irrelevant to me, so I don&#8217;t need the programming language community to be large (and in fact, being <em>too</em> large is a detriment), but I do think it should be active enough that there are people available to help answer questions, write libraries, maintain/improve compilers, debuggers etc.
</p>
<p>
To this end, I spent time on various usenet groups, IRC channels and blogs. Occasionally asking questions, but mostly observing the dialog and interaction among the community members.
</p>
<h3>Education</h3>
<p>
What educational materials are available?
</p>
<p>
I tend to prefer learning from books, so searching Amazon to see what books are available, how well they&#8217;re received, etc. was helpful. Although I prefer books typically, in the exploratory stages, it&#8217;s cheaper to go through online materials, so searching the web for free PDFs was useful. The existence of a few good texts is also an indication of the vitality of community.
</p>
<h3>Productivity</h3>
<p>
Ultimately, I&#8217;m after the most productive language for me; however, I think productivity is the factor that requires the most proficiency with a language to judge, so it was the most difficult factor to research prior to learning a language. I was limited to anecdotal stories, case studies, personal testimonies, etc.
</p>
<h3>Licensing</h3>
<p>
I prefer open source licensing because I think programming languages with proprietary licensing are more likely to die.
</p>
<h2>The Final Candidates</h2>
<p>
I began with Ruby as the point of reference irrespective of Rails and other libraries. Even though Rails is an important factor in my current productivity, since I&#8217;m taking a long term view, I didn&#8217;t want to exclude a fantastic language simply because it&#8217;s lacking something that Ruby was also lacking N years ago.
</p>
<p>
Due to the cost of researching, learning and switching to a new programming language, I only considered languages that had the potential of offering a significant improvement in one or more areas without losing too much in other areas.
</p>
<p>
The candidates I eventually selected fell into two groups. Both groups have strong <em>functional</em> capabilities, good efficiency, and long, successful track records. I believe that even the best programming language designers make serious mistakes which can only be identified with the hindsight of years of use. It&#8217;s possible that someone is about to release a brand new language that has the potential to be the most productive for me personally; however, I&#8217;m not willing to take the risk to learn it.
</p>
<h3>The Lisp Family</h3>
<p>
Lisp has a very long track record of success and adaptability. In fact, if I was forced to program in a single programming language from now on, I would probably choose a Lisp since I think it would have the greatest likelihood of being able to adapt.
</p>
<p>
The Lisp family has dynamic typing which I&#8217;ve grown to love with Ruby, but most also allow type declarations for efficiency. Lisp also appears to have a more fundamental nature than most languages. I&#8217;ve heard it said that Lisp was more discovered than invented.
</p>
<p>
I first became interested in Lisp when I learned that Ruby was influenced by it. My interest was reinforced by some of Paul Graham&#8217;s essays &amp; books. I like the exploratory and dynamic nature of developing Lisp &#8211; this is mostly from reading comments of others, but I&#8217;ve tasted a small part of this when evaluating Lisp code in Emacs and having it be available immediately.
</p>
<p>
<strong>Logo</strong> is first on the list simply because it&#8217;s both a Lisp and a great language for teaching children how to program, so I can kill two birds with one stone.
</p>
<p>
<strong>Scheme</strong> is next because it&#8217;s a natural sequel to Logo and I <em>still</em> want to work through &#8220;The Structure and Interpretation of Computer Programs&#8221;. The fact that Paul Graham chose mzcheme to create his Arc language is a good &#8220;letter of recommendation&#8221;. Scheme <em>feels</em> the most fundamental, simple &amp; clean thus far.
</p>
<p>
<strong>Common Lisp</strong> is after Scheme due to its eminent practicality and completeness. It has plenty of warts, but also plenty of power and functionality. It&#8217;s been called a great, big, ball of mud &#8211; but in a good way ;)
</p>
<p>
Lastly, is <strong>Clojure</strong>. It&#8217;s last in the Lisp family because I want to learn Scheme &amp; Common Lisp first so I&#8217;ll be better equipped to judge Clojure. This will also allow more time for Clojure to mature. Clojure has a focus on functional programming &amp; concurrency that is <em>symbiotic</em> with the Java platform. The latter provides a quick start and access to Java libraries, the JVM infrastructure, etc., but my preference would be to not be dependent on the JVM in the long run. I&#8217;ll withhold judgment until I&#8217;ve learned it and used it for a while.
</p>
<h3>The ML Family</h3>
<p>
I&#8217;m least familiar with the ML family and with functional programming in general. I&#8217;ve spent most of my career studying and using imperative, object-oriented programming languages.
</p>
<p>
I think the ML family is worth studying because:
</p>
<ul>
<li>Functional programming may be beneficial with respect to concurrency.</li>
<li>Prog. Lang. researchers seem to be enamored with ML family.</li>
<li>There are enough anecdotal testimonies of productivity to warrant further study.</li>
</ul>
<p>
<strong>Standard ML</strong> is an important functional language that differs from Haskell in that it&#8217;s <em>impure</em>, i.e. it allows side effects, and it&#8217;s <em>strict</em>, i.e. not lazy. It shares Hindly-Milner static typing with Haskell. The community seems rather tiny, and I expect that if I go with a static typed language it will likely be Haskell, but I wanted to learn Standard ML first because of its historical importance and to be able to compare an impure/strict language with a pure/nonstrict language.
</p>
<p>
<strong>Haskell</strong> is probably the most different programming language from what I&#8217;m used to. This, in and of itself, has some advantages with respect to gaining new perspectives and ideas. In some ways, it&#8217;s a language that has taken things to extremes with respect to functional purity and laziness.
</p>
<p>
The community is <em>very</em> active. It offers a great compiler (GHC), software transactional memory, a decent base of libraries, etc.
</p>
<p>
At this early stage, I&#8217;m skeptical of static typing, functional purity and laziness, so becoming proficient in Haskell is a great opportunity to be able to determine how I feel about those.
</p>
<h2>The Plan</h2>
<p>
Rather than go through the candidates sequentially, I&#8217;m going to try and make progress on two tracks concurrently to allow me to compare concepts from both families:</p>
<table border="1" cellpadding="3">
<tr>
<th>ML Family</th>
<th>Lisp Family</th>
</tr>
<tr>
<td>Standard ML</td>
<td>Logo</td>
</tr>
<tr>
<td>Haskell</td>
<td>Scheme</td>
</tr>
<tr>
<td></td>
<td>Common Lisp</td>
</tr>
<tr>
<td></td>
<td>Clojure</td>
</tr>
</table>
<p>
<br />
I&#8217;m curious to find out how I feel about these languages after I&#8217;ve achieved some skill with them, but I think becoming proficient in the Lisp and ML families will be time well spent. At minimum, I&#8217;ll be better equipped to compare other languages.</p>
]]></content:encoded>
			<wfw:commentRss>http://lojic.com/blog/2009/04/27/2009-programming-language-plan/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Programming Language Popularity</title>
		<link>http://lojic.com/blog/2009/04/21/programming-language-popularity/</link>
		<comments>http://lojic.com/blog/2009/04/21/programming-language-popularity/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 19:55:41 +0000</pubDate>
		<dc:creator>Brian Adkins</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[arc]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[common_lisp]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[forth]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[io]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[ocaml]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[smalltalk]]></category>
		<category><![CDATA[sml]]></category>
		<category><![CDATA[tcl]]></category>

		<guid isPermaLink="false">http://lojic.com/blog/?p=466</guid>
		<description><![CDATA[Despite the numerous ways in existence to quantify programming language popularity, I thought I&#8217;d throw yet another one into the mix. I made a number of Google searches of the forms below and averaged the results:

"implemented in &#60;language&#62;"
"written in &#60;language&#62;"

I&#8217;m very curious to see how these stats change over time, so I&#8217;ve added a calendar [...]]]></description>
			<content:encoded><![CDATA[<p>Despite the numerous ways in existence to quantify programming language popularity, I thought I&#8217;d throw yet another one into the mix. I made a number of Google searches of the forms below and averaged the results:</p>
<pre class="code">
"implemented in &lt;language&gt;"
"written in &lt;language&gt;"
</pre>
<p>I&#8217;m very curious to see how these stats change over time, so I&#8217;ve added a calendar item to recompute them in six months. Leave a comment if you&#8217;d like to add a programming language to the list, and I&#8217;ll update this article and it will be included in the recomputation six months from now.</p>
<table border="0">
<tbody>
<tr>
<th>Language</th>
<th># Results</th>
</tr>
<tr>
<td>C</td>
<td>1,905,500</td>
</tr>
<tr>
<td>Java</td>
<td>850,000</td>
</tr>
<tr>
<td>C++</td>
<td>699,000</td>
</tr>
<tr>
<td>PHP</td>
<td>680,000</td>
</tr>
<tr>
<td>Python</td>
<td>396,000</td>
</tr>
<tr>
<td>Perl</td>
<td>365,500</td>
</tr>
<tr>
<td>C#</td>
<td>349,700</td>
</tr>
<tr>
<td>Lisp Family<sup>1</sup></td>
<td>176,507</td>
</tr>
<tr>
<td>JavaScript</td>
<td>102,700</td>
</tr>
<tr>
<td>Ruby</td>
<td>99,650</td>
</tr>
<tr>
<td>Scheme</td>
<td>86,450</td>
</tr>
<tr>
<td>Lisp</td>
<td>61,900</td>
</tr>
<tr>
<td>Tcl</td>
<td>44,800</td>
</tr>
<tr>
<td>ML Family<sup>2</sup></td>
<td>29,062</td>
</tr>
<tr>
<td>Haskell</td>
<td>22,550</td>
</tr>
<tr>
<td>Erlang</td>
<td>22,285</td>
</tr>
<tr>
<td>OCaml</td>
<td>22,000</td>
</tr>
<tr>
<td>Common Lisp</td>
<td>20,600</td>
</tr>
<tr>
<td>Prolog</td>
<td>17,750</td>
</tr>
<tr>
<td>Lua</td>
<td>13,065</td>
</tr>
<tr>
<td>Smalltalk</td>
<td>9,105</td>
</tr>
<tr>
<td>Arc</td>
<td>6,775</td>
</tr>
<tr>
<td>Forth</td>
<td>6,465</td>
</tr>
<p><!-- use ML and SML --></p>
<tr>
<td>(S)ML<sup>3</sup></td>
<td>5,173</td>
</tr>
<tr>
<tr>
<td>Scala</td>
<td>3,570</td>
</tr>
<tr>
<td>Caml</td>
<td>1,889</td>
</tr>
<tr>
<td>Io</td>
<td>1,760</td>
</tr>
<tr>
<td>Clojure</td>
<td>782</td>
</tr>
</tbody>
</table>
<p><sup>1</sup> combines Lisp, Scheme, Common Lisp, Arc &amp; Clojure<br />
<sup>2</sup> combines OCaml, (S)ML, Caml<br />
<sup>3</sup> summed separate searches for sml and ml<br />
<strong>Update</strong> 4/23/09 added C#, Tcl per comment requests.</p>
]]></content:encoded>
			<wfw:commentRss>http://lojic.com/blog/2009/04/21/programming-language-popularity/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Use Ruby to parse NMEA sentences from your GPS</title>
		<link>http://lojic.com/blog/2009/04/08/use-ruby-to-parse-nmea-sentences-from-your-gps/</link>
		<comments>http://lojic.com/blog/2009/04/08/use-ruby-to-parse-nmea-sentences-from-your-gps/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 16:07:00 +0000</pubDate>
		<dc:creator>Brian Adkins</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[gadgets]]></category>
		<category><![CDATA[gps]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://lojic.com/blog/?p=457</guid>
		<description><![CDATA[I recently obtained a mobile broadband device that has a built in GPS receiver and can emit NMEA sentences. My old Garmin portable GPS can emit NMEA also, but it&#8217;s a pain to hookup to the laptop. Combining a GPS unit in a mobile broadband device is a great idea.
Update: it appears that the accuracy [...]]]></description>
			<content:encoded><![CDATA[<p>I recently obtained a mobile broadband device that has a built in GPS receiver and can emit NMEA sentences. My old Garmin portable GPS can emit NMEA also, but it&#8217;s a pain to hookup to the laptop. Combining a GPS unit in a mobile broadband device is a great idea.</p>
<p><strong>Update</strong>: it appears that the accuracy radius of the wireless card is quite a bit larger than my old Garmin unit. The Garmin is usually between 15 and 30 feet, but the Sierra Wireless 598U ranges from 100 to 1,000 feet or more. </p>
<p>After installing the ruby-serialport gem, I was able to write a simple Ruby program to read GPS information from the device and update a remote file on my web server to allow real time location tracking.</p>
<p>Add a simple server side script to read the file and update an iframed Google Map and you&#8217;re all set.</p>
<p>The code is also in the Ruby section of my <a href="http://github.com/lojic/sample_code/tree/master">sample code repository</a> on Github.</p>
<pre class="code">
sudo gem install ruby-serialport
</pre>
<pre class="code">
#!/usr/local/bin/ruby
# Author: Brian Adkins
# Date:   2009/04/08
# Copyright 2009 Brian Adkins - All Rights Reserved
#
# Ruby program to retrieve and parse GPS information (via NMEA sentences)
# from a Sprint Sierra Wireless 598U device.
#
# ruby gps-nmea.rb                # prints latititude/longitude info
# ruby gps-nmea.rb update-remote  # scp a file of location info to a remote server
#
# This program depends on the ruby-serialport gem:
# sudo gem install ruby-serialport
#
# From: http://www.gpsinformation.org/dale/nmea.htm#GGA
#  $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
# Where:
#      GGA          Global Positioning System Fix Data
#      123519       Fix taken at 12:35:19 UTC
#      4807.038,N   Latitude 48 deg 07.038' N
#      01131.000,E  Longitude 11 deg 31.000' E
#      1            Fix quality: 0 = invalid
#                                1 = GPS fix (SPS)
#                                2 = DGPS fix
#                                3 = PPS fix
#              4 = Real Time Kinematic
#              5 = Float RTK
#                                6 = estimated (dead reckoning) (2.3 feature)
#              7 = Manual input mode
#              8 = Simulation mode
#      08           Number of satellites being tracked
#      0.9          Horizontal dilution of position
#      545.4,M      Altitude, Meters, above mean sea level
#      46.9,M       Height of geoid (mean sea level) above WGS84
#                       ellipsoid
#      (empty field) time in seconds since last DGPS update
#      (empty field) DGPS station ID number
#      *47          the checksum data, always begins with *

require 'rubygems'
require 'serialport'

# Emacs macro to reset user modified values (highlight, then: M-x eval-region )
# ((lambda (&#038;optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ("USERNAME
\"
\372\"HOSTNAME
\"
\372\"REMOTE_DIR
\"
\372\"" 0 "%d")) arg)))

# --- MODIFY THESE -- #
USERNAME   = ""  # Username for remote host
HOSTNAME   = ""  # Remote host name e.g. foo.com
REMOTE_DIR = ""  # Remote directory e.g. /var/www/bar
# --- MODIFY THESE -- #

port_str  = '/dev/cu.sierra05'
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity    = SerialPort::NONE

sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)

# lat is of the form 4807.038 where the first 2 digits are degrees and
#   the remainder is minutes.
# dir is either 'N' or 'S'
def convert_lat lat, dir
  degrees = lat[0,2].to_f + (lat[2,lat.length-2].to_f / 60.0)
  dir == 'N' ? degrees : -degrees
end

# lon is of the form 01131.000 where the first 3 digits are degrees and
#   the remainder is minutes.
# dir is either 'E', or 'W'
def convert_lon lon, dir
  degrees = lon[0,3].to_f + (lon[3,lon.length-2].to_f / 60.0)
  dir == 'E' ? degrees : -degrees
end

TEMP_PATH = '/tmp'
TEMP_FILE = 'location.txt'

def update_remote_info lat, lon
  File.open("#{TEMP_PATH}/#{TEMP_FILE}", 'w') do |tf|
    tf.puts Time.now.to_s
    tf.puts "#{lat},#{lon}"
  end
  puts 'Updating remote location info'
  `scp #{TEMP_PATH}/#{TEMP_FILE} #{USERNAME}@#{HOSTNAME}:#{REMOTE_DIR}/#{TEMP_FILE}`
  File.delete("#{TEMP_PATH}/#{TEMP_FILE}")
end

# 99 requests should be sufficient to find a $GPGGA sentence
99.times do
  if (str = sp.gets) =~ /^\$GPGGA/
    fix = str.split(',')
    if fix[6] == '1'
      lat = convert_lat(fix[2], fix[3])
      lon = convert_lon(fix[4], fix[5])
      if ARGV[0] == 'update-remote'
        update_remote_info(lat,lon)
      elsif
        puts "#{lat}, #{lon}"
      end
      exit 0
    end
  end
end

puts "Invalid data - GPS coordinates not found"
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lojic.com/blog/2009/04/08/use-ruby-to-parse-nmea-sentences-from-your-gps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Retrieve Sunrise, Sunset &amp; Twilight Info in Ruby</title>
		<link>http://lojic.com/blog/2009/03/11/retrieve-sunrise-sunset-twilight-in-ruby/</link>
		<comments>http://lojic.com/blog/2009/03/11/retrieve-sunrise-sunset-twilight-in-ruby/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 05:41:54 +0000</pubDate>
		<dc:creator>Brian Adkins</dc:creator>
				<category><![CDATA[internet]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[science]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[astronomy]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[usno]]></category>

		<guid isPermaLink="false">http://lojic.com/blog/?p=418</guid>
		<description><![CDATA[Sunrise, Sunset &#38; Twilight
I was curious about the exact time of sunrise &#38; sunset at my location, so I found this US Naval Observatory site. In the process, I learned a more precise definition of  twilight. I wanted to be able to automate the process of retrieving the information, so my first attempt was [...]]]></description>
			<content:encoded><![CDATA[<h2>Sunrise, Sunset &amp; Twilight</h2>
<p><a href="http://lojic.com/blog/wp-content/uploads/2009/03/seascape_after_sunset_900x600.jpg"><img class="alignleft size-medium wp-image-441" title="Twilight" src="http://lojic.com/blog/wp-content/uploads/2009/03/seascape_after_sunset_900x600-300x200.jpg" alt="Twilight" width="300" height="200" /></a>I was curious about the exact time of sunrise &amp; sunset at my location, so I found this <a href="http://aa.usno.navy.mil/data/docs/RS_OneDay.php">US Naval Observatory</a> site. In the process, I learned a more precise definition of  <a href="http://aa.usno.navy.mil/faq/docs/RST_defs.php#top"><em>twilight</em></a>. I wanted to be able to automate the process of retrieving the information, so my first attempt was to simply put the query parameters used in the form in the URL as an HTTP GET request, but the server wouldn&#8217;t accept that, so I needed to issue an HTTP POST request.<br />
<br style="clear: left;" /></p>
<h2>Ruby Code</h2>
<p>Ruby is a great language for this sort of task, so I put together the following simple program:</p>
<pre>require 'net/http'

YOUR_ID    = ''    # A unique ID per comment above
YOUR_CITY  = ''    # The name of your city
YOUR_STATE = ''    # Two letter state abbreviation

now   = Time.now
month = now.month
day   = now.day + 1 # Tomorrow
year  = now.year

Net::HTTP.start('aa.usno.navy.mil') do |query|
  response = query.post('/cgi-bin/aa_pap.pl',
    "FFX=1&amp;amp;amp;amp;ID=#{YOUR_ID}&amp;amp;amp;amp;xxy=#{year}&amp;amp;amp;amp;xxm=#{month}&amp;amp;amp;amp;xxd=#{day}&amp;amp;amp;amp;st=#{YOUR_STATE}&amp;amp;amp;amp;place=#{YOUR_CITY}&amp;amp;amp;amp;ZZZ=END")
  if response.body =~ /Begin civil twilight[^0-9]*(\d+:\d{2} [ap].m.).*Sunrise[^0-9]*(\d+:\d{2} [ap].m.).*Sunset[^0-9]*(\d+:\d{2} [ap].m.).*End civil twilight[^0-9]*(\d+:\d{2} [ap].m.)/m
    puts "#{month}/#{day}/#{year}"
    puts "Begin Twilight: #{$1}"
    puts "Sunrise       : #{$2}"
    puts "Sunset        : #{$3}"
    puts "End Twilight  : #{$4}"
  end
end</pre>
<p>You just need to edit the three constants that begin with YOUR_. The id used on the Navy web form is &#8216;AA&#8217;, but they have a comment in the HTML that requests you use a unique id of your own up to 8 characters to help them with tracking. You can find a more complete version of <a href="http://github.com/lojic/sample_code/blob/7b41e88adefd84154a54c97de86924f8517dca2c/ruby/sunrise_http_post.rb">the code</a> in my <a href="http://github.com/lojic">github profile</a>.</p>
<h2>Emacs Goodness</h2>
<p>After writing the above Ruby script, I made it executable, &#8216;chmod +x sunrise.rb&#8217;, and placed it in my path so I could write a simple Emacs function to invoke it.</p>
<pre>(defun bja-sunrise ()
  "Display sunrise, sunset &amp;amp; twilight information."
  (interactive)
  (shell-command "sunrise.rb"))</pre>
<p>Imagine my surprise when I invoked the Emacs apropos help &#8216;C-h a&#8217; to see my newly defined function and discovered that Emacs, naturally, already has several commands to display sunrise/sunset information!</p>
<dl>
<dt>calendar-mouse-sunrise/sunset</dt>
<dd>Show sunrise/sunset times for mouse-selected date.</dd>
<dt>calendar-sunrise-sunset</dt>
<dd>Local time of sunrise and sunset for date under cursor.</dd>
<dt>sunrise-sunset</dt>
<dd>Local time of sunrise and sunset for today.  Accurate to a few seconds.</dd>
</dl>
<p>It doesn&#8217;t, however, display <strong>twilight</strong> information, so my simple function still has a purpose in life. Emacs is awesome :)</p>
]]></content:encoded>
			<wfw:commentRss>http://lojic.com/blog/2009/03/11/retrieve-sunrise-sunset-twilight-in-ruby/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

