{"id":462,"date":"2014-03-12T01:04:23","date_gmt":"2014-03-12T06:04:23","guid":{"rendered":"http:\/\/sunapi386.ca\/wordpress\/?p=462"},"modified":"2015-10-28T16:06:58","modified_gmt":"2015-10-28T21:06:58","slug":"programming-languages","status":"publish","type":"post","link":"https:\/\/sunapi386.ca\/wordpress\/programming-languages\/","title":{"rendered":"Programming Languages"},"content":{"rendered":"<p>Low level programs contain a lot of ad hoc, buggy, and slow implementation of higher level languages. Paul Graham described an excellent idea in &#8220;Hackers and Painters&#8221;<\/p>\n<blockquote><p>If you solve a hard problem, the question is not whether you will use a powerful enough language, but whether you will (a) use a powerful enough language, (b) write a defacto interpreter for one, or (c) yourself become a human compiler for one.&#8221;<br \/>\n[&#8230;]<br \/>\nFor example in the OO world you hear a good deal about &#8220;patterns.&#8221; I wonder if these patterns are not sometimes of case (c), the human compiler, at work.&#8221;<\/p><\/blockquote>\n<p>In short, as developers we like concise, unbloated languages that is sufficient for what we need.<\/p>\n<h2>In-list functions<\/h2>\n<p>I was reading Peter Norvig&#8217;s <a href=\"http:\/\/norvig.com\/spell-correct.html\">spell correct<\/a> and discovered python had in-list comprehensions. It&#8217;s a nice feature which allows you to easily construct a list.<\/p>\n<p><code><br \/>\n&gt;&gt;&gt; [ x**2 for x in range(1,11) if x % 2 == 0 ]<br \/>\n[0, 4, 16, 36, 64]<br \/>\n<\/code><\/p>\n<p>It looks very readable. Now, ruby being my favourite language over python, I wanted to see if ruby had a similar feature. The best I can come up with is this:<\/p>\n<p><code><br \/>\n2.0.0-p247 :017 &gt; (1..10).to_a.map{|x| x**2 if x%2 == 0 }.compact<br \/>\n=&gt; [4, 16, 36, 64, 100]<br \/>\n<\/code><\/p>\n<p>This isn&#8217;t as easy to comprehend. For instance, why is it necessary to have <code>.compact<\/code>?<br \/>\nWhy do we need to call <code>.to_a<\/code>? Python wins in this case. I&#8217;m now a lesser fan of ruby and more of python.<br \/>\nLastly, I checked this out in haskell &#8211; which many consider to be a dense language. It&#8217;s also a language I&#8217;m looking to do some coding in.<\/p>\n<p><code><br \/>\nPrelude&gt; [ x^2 | x [4,16,36,64,100]<br \/>\n<\/code><\/p>\n<p>Not bad. I&#8217;d argue that this is as easy as pythons &#8211; provided that you understand haskell syntax. Syntax wise, I suspect python is more wordy than haskell.<\/p>\n<h2>Ruby lambda vs. procs<\/h2>\n<p>In ruby, lambda and procs are pretty much the same, but not quite. Syntactically, they are similar.<\/p>\n<p><code><br \/>\nlambda_larger_than_4 = lambda { |num| num if num &gt; 4 }<br \/>\nproc_larger_than_4 = Proc.new { |num| num if num &gt; 4 }<br \/>\n(-1..8).to_a.collect &amp;lambda_larger_than_4<br \/>\n=&gt; [nil, nil, nil, nil, nil, nil, 5, 6, 7, 8]<br \/>\n(-1..8).to_a.collect &amp;proc_larger_than_4<br \/>\n=&gt; [nil, nil, nil, nil, nil, nil, 5, 6, 7, 8]<br \/>\n<\/code><\/p>\n<p>However, there are two main differences.<\/p>\n<ul>Lambda checks number of arguments passed to it and throws error. Proc ignores unexpected arguments and assigns nil to any missing.<\/ul>\n<ul>When lambda returns, control is passed to caller. When proc returns, control does not get passed back to caller.<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Low level programs contain a lot of ad hoc, buggy, and slow implementation of higher level languages. Paul Graham described an excellent idea in &#8220;Hackers and Painters&#8221; If you solve a hard problem, the question is not whether you will use a powerful enough language, but whether you will (a) use a powerful enough language, &hellip; <a href=\"https:\/\/sunapi386.ca\/wordpress\/programming-languages\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Programming Languages<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-462","post","type-post","status-publish","format-standard","hentry","category-academica"],"_links":{"self":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/comments?post=462"}],"version-history":[{"count":4,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/462\/revisions"}],"predecessor-version":[{"id":467,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/posts\/462\/revisions\/467"}],"wp:attachment":[{"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/media?parent=462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/categories?post=462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunapi386.ca\/wordpress\/wp-json\/wp\/v2\/tags?post=462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}