<?xml version="1.0"?>
<rss version="2.0">
<channel>
  <title>Weiqi Gao&#039;s Observations - clojure tag</title>
  <link>http://www.weiqigao.com/blog/tags/clojure/</link>
  <description>Sharing My Experience...</description>
  <language>en</language>
  <copyright>Weiqi Gao</copyright>
  <lastBuildDate>Fri, 11 May 2012 12:48:36 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  
  <image>
    <url>http://pebble.sourceforge.net/common/images/powered-by-pebble.gif</url>
    <title>Weiqi Gao&#039;s Observations</title>
    <link>http://www.weiqigao.com/blog/</link>
  </image>
  
  
  <item>
    <title>X = X + 1 Is Evil</title>
    <link>http://www.weiqigao.com/blog/2009/09/22/x_x_1_is_evil.html</link>
    
      
        <description>
          &lt;p&gt;&lt;em&gt;(Not via &lt;a href= &#034;http://twitter.com/&#034;&gt;Twitter&lt;/a&gt;.)&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;&lt;a href= &#034;http://www.artima.com/articles/hickey_on_timeP.html&#034;&gt;Rich Hickey (in an Artima interview)&lt;/a&gt;: For example, take a mutable Date class that has year, month, and day. To me, changing a date is like trying to change 42 into 43.&lt;/blockquote&gt;

&lt;p&gt;This is as close an elevator pitch for pure functional programming as I&#039;ve ever seen.&lt;/p&gt;

&lt;p&gt;Rich&#039;s analogy reminds me of the most painful experience I had when I started programming.  So painful that I suppressed that memory ever since.  That was when I first saw&lt;/p&gt;

&lt;pre style=&#034;margin-left:3em&#034;&gt;&lt;span style=&#034;color:red&#034;&gt;x&lt;/span&gt; &lt;span style=&#034;color:purple&#034;&gt;=&lt;/span&gt; &lt;span style=&#034;color:blue&#034;&gt;x&lt;/span&gt; + 1;&lt;/pre&gt;

&lt;p&gt;That&#039;s impossible, I thought.  That&#039;s plainly wrong.  It took me days or weeks to &#034;get over&#034; it.  My mind fought the rationalization for such stupidity:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The equal sign is not a mathematical equal sign but an assignment.  (What?)&lt;/li&gt;
&lt;li&gt;The read &lt;tt style=&#034;color:red&#034;&gt;x&lt;/tt&gt; and the blue &lt;tt style=&#034;color:blue&#034;&gt;x&lt;/tt&gt; means different things.  (What?)&lt;/li&gt;
&lt;li&gt;The blue one is the value before the assignment, and the red after.  (Before?)&lt;/li&gt;
&lt;li&gt;It&#039;s really the location.  (Location or value, which is it?)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, given no other practical alternatives, I gave in just like everyone else.  And now we are in this concurrency crisis.  For countless times, we&#039;ve been called to &#034;look at&#034; problems where a program is not &#034;working&#034; because the assignments are not happening in the order the programmer envisioned.  There clearly is a systemic problem.&lt;/p&gt;

&lt;p&gt;Rich Hickey&#039;s solution is his &lt;a href= &#034;http://clojure.org/&#034; &gt;clojure&lt;/a&gt; programming language.  I dabbled with it a bit in the past year.  I have gotten tremendous value out of Mark Volkmann&#039;s write ups on &lt;a href= &#034;http://java.ociweb.com/mark/clojure/article.html&#034; &gt;Clojure&lt;/a&gt; and &lt;a href= &#034;http://java.ociweb.com/mark/stm/article.html&#034; &gt;STM&lt;/a&gt;.

&lt;p&gt;In Mark&#039;s latest tweets and emails, he often mentions these three things in one breath:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Immutable objects&lt;/li&gt;
&lt;li&gt;Persistent data structures&lt;/li&gt;
&lt;li&gt;Transactional software memory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I wonder if that will become the rallying cry for Clojure and Clojure-like alternatives to main stream languages.  If so, a paradigm shift is afoot.&lt;/p&gt;

&lt;p&gt;Remember OO got traction after its &#034;encapsulation, inheritance, and polymorephism&#034; motto was coined.&lt;/p&gt;
        </description>
      
      
    
    
    
    <comments>http://www.weiqigao.com/blog/2009/09/22/x_x_1_is_evil.html#comments</comments>
    <guid isPermaLink="true">http://www.weiqigao.com/blog/2009/09/22/x_x_1_is_evil.html</guid>
    <pubDate>Tue, 22 Sep 2009 13:11:50 GMT</pubDate>
  </item>
  
  <item>
    <title>Jeff Brown&#039;s Probability Quiz: What Are The Chances ...</title>
    <link>http://www.weiqigao.com/blog/2009/08/03/jeff_browns_probability_quiz_what_are_the_chances.html</link>
    
      
        <description>
          &lt;p&gt;... Jeff is of course talking about &lt;a href= &#034;http://javajeff.blogspot.com/2009/07/grails-springsource-and-clojure.html&#034; &gt;Grails, SpringSource, and Clojure&lt;/a&gt;.  Near the end of the post, Jeff challenged his readers with a probability brain teaser:&lt;/p&gt;

&lt;blockquote&gt;
&lt;a href= &#034;http://javajeff.blogspot.com/2009/07/grails-springsource-and-clojure.html&#034; &gt;Jeff Brown&lt;/a&gt;: I am honestly not sure how many technologists work for SpringSource but for the sake of having a number to work with, lets say there are 50.  Lets also say that Clojure is interesting enough that 15% of JVM developers are interested in learning more about it.  I can&#039;t back that number up with any research, lets just go with it.  If 15% of JVM developers are interested in the language and you take a random group of 50 JVM developers (the 50 in question are certainly not 50 random developers, these are the edge cutters which probably makes them more likely to be interested in keeping an eye on what is new, but work with the idea that they are random)... Do the math.  No, really... Do the math.  Do the math to figure out the likelihood that 3 of the 50 would be interested in Clojure.  I challenge you to do the math in Clojure and post your solution in a comment here.  Do it in Scala,  Do it in Groovy.  Pick a JVM language and do the math.&lt;br&gt;
&lt;/blockquote&gt;

&lt;p&gt;My answer got a little bit too long for a comment on Jeff&#039;s blog.  So I&#039;m putting it here.&lt;/p&gt;

&lt;p&gt;First of all, Jeff, its probability, not mathematics.  They branched off a long time ago, just like computer science branched off more recently.  It&#039;s nevertheless an interesting problem to ponder about in a Saturday morning.  It also helps that I&#039;m married to a statistician.  Here&#039;s how the conversation went:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Me&lt;/b&gt;: Jeff Brown blogged about an interesting probability problem.  (&lt;em&gt;Reading the problem&lt;/em&gt;)&lt;br/&gt;
&lt;b&gt;Her&lt;/b&gt;: The problem is not complete.  He didn&#039;t mention how many JVM developers there are.  The answer probably depends on it.&lt;br/&gt;
&lt;b&gt;Me&lt;/b&gt;: For the sake of argument, let&#039;s say there are 100 of them.&lt;br/&gt;
&lt;b&gt;Her&lt;/b&gt;: OK.  Then the problem is equivalent to the following:&lt;/p&gt;

&lt;p style=&#034;margin-left:3em&#034;&gt;&lt;b&gt;Problem&lt;/b&gt;: There is a jar of 100 balls. Among them 85 balls are black, and 15 balls are read.  A batch of 50 balls are selected at random from the jar.  A group of 3 balls are selected at random from the batch.  What is the probability that all 3 balls in the selected group are red?&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Me&lt;/b&gt;: That sounds like a probability problem the students could tackle.&lt;br/&gt;
&lt;b&gt;Her&lt;/b&gt;: You analyze the scenario into more concrete cases: What if the batch of 50 contains no red balls? 1 red ball? 2? 3?, ... Use the &lt;a href= &#034;http://en.wikipedia.org/wiki/Law_of_total_probability&#034; &gt;Law of Total Probability&lt;/a&gt;.  Here&#039;s the book!&lt;p&gt;

&lt;p&gt;(&lt;em&gt;Saturday morning turned into Sunday evening, and we finally have a chance to work the problem out.&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;We&#039;ll solve the probability problem by hand and reduce it to a computation problem that is as simple as possible, and then use the computing machinery.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Solution&lt;/b&gt;: We use the following letters to denote the various quantities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;i&gt;n&lt;/i&gt;&amp;mdash;the total number of balls&lt;/li&gt;
&lt;li&gt;&lt;i&gt;r&lt;/i&gt;&amp;mdash;the number of red balls&lt;/li&gt;
&lt;li&gt;&lt;i&gt;m&lt;/i&gt;&amp;mdash;the number of balls in the batch&lt;/li&gt;
&lt;li&gt;&lt;i&gt;k&lt;/i&gt;&amp;mdash;the number of balls in the group&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The event of selecting a batch of &lt;i&gt;m&lt;/i&gt; balls and then selecting exactly &lt;i&gt;k&lt;/i&gt; red balls from the &lt;i&gt;m&lt;/i&gt; can be decomposed to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;selecting a batch of &lt;i&gt;m&lt;/i&gt; balls &lt;span style=&#034;color:red&#034;&gt;containing exactly &lt;i&gt;k&lt;/i&gt; red balls&lt;/span&gt; and then selecting exactly &lt;i&gt;k&lt;/i&gt; red balls from the &lt;i&gt;m&lt;/i&gt;&lt;/li&gt;
&lt;li&gt;selecting a batch of &lt;i&gt;m&lt;/i&gt; balls &lt;span style=&#034;color:red&#034;&gt;containing exactly &lt;i&gt;k + 1&lt;/i&gt; red balls&lt;/span&gt; and then selecting exactly &lt;i&gt;k&lt;/i&gt; red balls from the &lt;i&gt;m&lt;/i&gt;&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;li&gt;selecting a batch of &lt;i&gt;m&lt;/i&gt; balls &lt;span style=&#034;color:red&#034;&gt;containing exactly &lt;i&gt;r&lt;/i&gt; red balls&lt;/span&gt; and then selecting exactly &lt;i&gt;k&lt;/i&gt; red balls from the &lt;i&gt;m&lt;/i&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The law of total probability dictates that the probability of out target event is the sum of the products of the probabilities of each of the successive events in the above list:&lt;/p&gt;

&lt;center&gt;
&lt;i&gt;P = &lt;big&gt;&amp;Sigma;&lt;/big&gt;&lt;sub&gt;i = k&lt;/sub&gt;&lt;sup&gt;r&lt;/sup&gt; P(n, r; m, i) P(m, i; k, k)&lt;/i&gt;
&lt;/center&gt;

&lt;p&gt;where &lt;i&gt;P(n, r; m, i)&lt;/i&gt; is the probability of selecting &lt;i&gt;m&lt;/i&gt; balls from a jar of &lt;i&gt;n&lt;/i&gt; balls containing &lt;i&gt;r&lt;/i&gt; red balls and having exactly &lt;i&gt;i&lt;/i&gt; red balls in the selected batch.  The probability of this event is given by the formula&lt;/p&gt;

&lt;center&gt;
&lt;i&gt;P(n, r; m, i) = C(r, i) C(n - r, m - i) / C(n, m)&lt;/i&gt;
&lt;/center&gt;

&lt;p&gt;where &lt;i&gt;C(r, i)&lt;/i&gt; is the binomial coefficient.&lt;/p&gt;

&lt;p&gt;With the problem thus &lt;i&gt;solved&lt;/i&gt;, I went ahead to &#034;&lt;i&gt;Pick a JVM language and do the math.&lt;/i&gt;&#034;  Guess which language I picked? Java.  Yes, Java.  Not Clojure.  Not Scala.  Not Groovy, but the old dependable Java.  &#034;Why?&#034; you ask.  Because doing it in any other language would have taken me longer to get the results out.&lt;/p&gt;

&lt;p&gt;Here are the classes I wrote:&lt;/p&gt;

&lt;style type=&#034;text/css&#034;&gt;
.ln { color: rgb(0,0,0); font-weight: normal; font-style: normal; }
.s0 { color: rgb(0,0,128); font-weight: bold; }
.s1 { }
.s2 { color: rgb(0,128,0); font-weight: bold; }
.s3 { color: rgb(0,0,255); }
&lt;/style&gt;

&lt;TABLE CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH=&#034;100%&#034; BGCOLOR=&#034;#C0C0C0&#034; &gt;
&lt;TR&gt;&lt;TD&gt;&lt;CENTER&gt;
&lt;FONT FACE=&#034;Arial, Helvetica&#034; COLOR=&#034;#000000&#034;&gt;
Foo.java&lt;/FONT&gt;
&lt;/center&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TABLE&gt;
&lt;pre&gt;
&lt;a name=&#034;l1&#034;&gt;&lt;span class=&#034;ln&#034;&gt;1    &lt;/span&gt;&lt;/a&gt;&lt;span class=&#034;s0&#034;&gt;public class &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;Foo { 
&lt;a name=&#034;l2&#034;&gt;&lt;span class=&#034;ln&#034;&gt;2    &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;private int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;n; 
&lt;a name=&#034;l3&#034;&gt;&lt;span class=&#034;ln&#034;&gt;3    &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;private int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;r; 
&lt;a name=&#034;l4&#034;&gt;&lt;span class=&#034;ln&#034;&gt;4    &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;private int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;m; 
&lt;a name=&#034;l5&#034;&gt;&lt;span class=&#034;ln&#034;&gt;5    &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;private int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;k; 
&lt;a name=&#034;l6&#034;&gt;&lt;span class=&#034;ln&#034;&gt;6    &lt;/span&gt;&lt;/a&gt; 
&lt;a name=&#034;l7&#034;&gt;&lt;span class=&#034;ln&#034;&gt;7    &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;public &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;Foo(&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;n, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;r, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;m, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;k) { 
&lt;a name=&#034;l8&#034;&gt;&lt;span class=&#034;ln&#034;&gt;8    &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;this&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;.n = n; 
&lt;a name=&#034;l9&#034;&gt;&lt;span class=&#034;ln&#034;&gt;9    &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;this&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;.r = r; 
&lt;a name=&#034;l10&#034;&gt;&lt;span class=&#034;ln&#034;&gt;10   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;this&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;.m = m; 
&lt;a name=&#034;l11&#034;&gt;&lt;span class=&#034;ln&#034;&gt;11   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;this&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;.k = k; 
&lt;a name=&#034;l12&#034;&gt;&lt;span class=&#034;ln&#034;&gt;12   &lt;/span&gt;&lt;/a&gt;  } 
&lt;a name=&#034;l13&#034;&gt;&lt;span class=&#034;ln&#034;&gt;13   &lt;/span&gt;&lt;/a&gt; 
&lt;a name=&#034;l14&#034;&gt;&lt;span class=&#034;ln&#034;&gt;14   &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;public static double &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;C(&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;n, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;k) { 
&lt;a name=&#034;l15&#034;&gt;&lt;span class=&#034;ln&#034;&gt;15   &lt;/span&gt;&lt;/a&gt;    System.out.println(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;C(&amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ n + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;, &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ k + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;) called.&amp;quot;&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;); 
&lt;a name=&#034;l16&#034;&gt;&lt;span class=&#034;ln&#034;&gt;16   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;double &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;prod = &lt;/span&gt;&lt;span class=&#034;s3&#034;&gt;1.0&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;; 
&lt;a name=&#034;l17&#034;&gt;&lt;span class=&#034;ln&#034;&gt;17   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;for &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;(&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;i = &lt;/span&gt;&lt;span class=&#034;s3&#034;&gt;1&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;; i &amp;lt;= k; i++) { 
&lt;a name=&#034;l18&#034;&gt;&lt;span class=&#034;ln&#034;&gt;18   &lt;/span&gt;&lt;/a&gt;      prod *= ((&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;double&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;) (n - i + &lt;/span&gt;&lt;span class=&#034;s3&#034;&gt;1&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;)) / ((&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;double&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;) i); 
&lt;a name=&#034;l19&#034;&gt;&lt;span class=&#034;ln&#034;&gt;19   &lt;/span&gt;&lt;/a&gt;      System.out.println(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;\t&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;Iteration &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ i + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;: prod = &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ prod); 
&lt;a name=&#034;l20&#034;&gt;&lt;span class=&#034;ln&#034;&gt;20   &lt;/span&gt;&lt;/a&gt;    } 
&lt;a name=&#034;l21&#034;&gt;&lt;span class=&#034;ln&#034;&gt;21   &lt;/span&gt;&lt;/a&gt;    System.out.println(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;\t&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;Returning &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ prod); 
&lt;a name=&#034;l22&#034;&gt;&lt;span class=&#034;ln&#034;&gt;22   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;return &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;prod; 
&lt;a name=&#034;l23&#034;&gt;&lt;span class=&#034;ln&#034;&gt;23   &lt;/span&gt;&lt;/a&gt;  } 
&lt;a name=&#034;l24&#034;&gt;&lt;span class=&#034;ln&#034;&gt;24   &lt;/span&gt;&lt;/a&gt; 
&lt;a name=&#034;l25&#034;&gt;&lt;span class=&#034;ln&#034;&gt;25   &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;public static double &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;P(&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;n, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;r, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;m, &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;i) { 
&lt;a name=&#034;l26&#034;&gt;&lt;span class=&#034;ln&#034;&gt;26   &lt;/span&gt;&lt;/a&gt;    System.out.println(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;P(&amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ n + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;, &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ r + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;, &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ m + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;, &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ i + &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;) called.&amp;quot;&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;); 
&lt;a name=&#034;l27&#034;&gt;&lt;span class=&#034;ln&#034;&gt;27   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;double &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;retVal = C(r, i) * C(n - r, m - i) / C(n, m); 
&lt;a name=&#034;l28&#034;&gt;&lt;span class=&#034;ln&#034;&gt;28   &lt;/span&gt;&lt;/a&gt;    System.out.println(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;\t&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;Returning &amp;quot; &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;+ retVal); 
&lt;a name=&#034;l29&#034;&gt;&lt;span class=&#034;ln&#034;&gt;29   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;return &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;retVal; 
&lt;a name=&#034;l30&#034;&gt;&lt;span class=&#034;ln&#034;&gt;30   &lt;/span&gt;&lt;/a&gt;  } 
&lt;a name=&#034;l31&#034;&gt;&lt;span class=&#034;ln&#034;&gt;31   &lt;/span&gt;&lt;/a&gt; 
&lt;a name=&#034;l32&#034;&gt;&lt;span class=&#034;ln&#034;&gt;32   &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;public double &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;P() { 
&lt;a name=&#034;l33&#034;&gt;&lt;span class=&#034;ln&#034;&gt;33   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;double &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;sum = &lt;/span&gt;&lt;span class=&#034;s3&#034;&gt;0.0&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;; 
&lt;a name=&#034;l34&#034;&gt;&lt;span class=&#034;ln&#034;&gt;34   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;for &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;(&lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;int &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;i = k; i &amp;lt;= r; i++) { 
&lt;a name=&#034;l35&#034;&gt;&lt;span class=&#034;ln&#034;&gt;35   &lt;/span&gt;&lt;/a&gt;      sum += P(n, r, m, i) * P(m, i, k, k); 
&lt;a name=&#034;l36&#034;&gt;&lt;span class=&#034;ln&#034;&gt;36   &lt;/span&gt;&lt;/a&gt;    } 
&lt;a name=&#034;l37&#034;&gt;&lt;span class=&#034;ln&#034;&gt;37   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;return &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;sum; 
&lt;a name=&#034;l38&#034;&gt;&lt;span class=&#034;ln&#034;&gt;38   &lt;/span&gt;&lt;/a&gt;  } 
&lt;a name=&#034;l39&#034;&gt;&lt;span class=&#034;ln&#034;&gt;39   &lt;/span&gt;&lt;/a&gt;} 
&lt;a name=&#034;l40&#034;&gt;&lt;span class=&#034;ln&#034;&gt;40   &lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;TABLE CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH=&#034;100%&#034; BGCOLOR=&#034;#C0C0C0&#034; &gt;
&lt;TR&gt;&lt;TD&gt;&lt;CENTER&gt;
&lt;FONT FACE=&#034;Arial, Helvetica&#034; COLOR=&#034;#000000&#034;&gt;
Main.java&lt;/FONT&gt;
&lt;/center&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TABLE&gt;
&lt;pre&gt;
&lt;a name=&#034;l1&#034;&gt;&lt;span class=&#034;ln&#034;&gt;1    &lt;/span&gt;&lt;/a&gt;&lt;span class=&#034;s0&#034;&gt;public class &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;Main { 
&lt;a name=&#034;l2&#034;&gt;&lt;span class=&#034;ln&#034;&gt;2    &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;public static void &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;main(String[] args) { 
&lt;a name=&#034;l3&#034;&gt;&lt;span class=&#034;ln&#034;&gt;3    &lt;/span&gt;&lt;/a&gt;    Foo foo = &lt;/span&gt;&lt;span class=&#034;s0&#034;&gt;new &lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;Foo(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;100&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;, &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;15&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;, &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;50&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;, &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;3&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;); 
&lt;a name=&#034;l4&#034;&gt;&lt;span class=&#034;ln&#034;&gt;4    &lt;/span&gt;&lt;/a&gt;    System.out.println(foo.P()); 
&lt;a name=&#034;l5&#034;&gt;&lt;span class=&#034;ln&#034;&gt;5    &lt;/span&gt;&lt;/a&gt;    System.out.println(Foo.P(&lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;100&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;, &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;15&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;, &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;3&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;, &lt;/span&gt;&lt;span class=&#034;s2&#034;&gt;3&lt;/span&gt;&lt;span class=&#034;s1&#034;&gt;)); 
&lt;a name=&#034;l6&#034;&gt;&lt;span class=&#034;ln&#034;&gt;6    &lt;/span&gt;&lt;/a&gt;  } 
&lt;a name=&#034;l7&#034;&gt;&lt;span class=&#034;ln&#034;&gt;7    &lt;/span&gt;&lt;/a&gt;} 
&lt;a name=&#034;l8&#034;&gt;&lt;span class=&#034;ln&#034;&gt;8    &lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;According to my calculation, the probability of the event that Jeff described is 0.0028138528138528123 or 0.28% assuming there are 100 JVM programmers.&lt;/p&gt;

&lt;p&gt;There are two points that&#039;s worth mentioning:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The condition of there being 50 SpringSource programmers can be actually shown to be superfluous.  And the calculation can therefore be simplified to merely that of &lt;i&gt;P(100, 15; 3, 3)&lt;/i&gt;&lt;/li&gt;
&lt;li&gt;It can be shown that &lt;i&gt;P(n, r, k, k)&lt;/i&gt; tends to &lt;i&gt;(r/n)^k&lt;/i&gt; as &lt;i&gt;n&lt;/i&gt; and &lt;i&gt;r&lt;/i&gt; grows to infinity while the ratio &lt;i&gt;r/n&lt;/i&gt; is kept constant.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;(&lt;em&gt;Now it&#039;s Monday morning.  After staring at the 0.28% probability for a while, I realized something...&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;What I calculated is not what Jeff wanted.  He clearly said he wanted &#034;the likelihood that 3 of the 50 would be interested in Clojure&#034;, or, in our language, &#034;the probability of selecting 50 balls that contains exactly 3 red balls.&#034;  That&#039;s &lt;i&gt;P(100, 15; 50, 3)&lt;/i&gt;, which is 0.009392308830476828 or 0.94%.&lt;/p&gt;  A simple experiment suggests that as &lt;i&gt;n&lt;/i&gt; and &lt;i&gt;r&lt;/i&gt; grows to infinity while the ratio &lt;i&gt;r/n&lt;/i&gt; is kept constant, the probability tends to 3.19%.&lt;/p&gt;

&lt;p&gt;(&lt;em&gt;It&#039;s Monday evening now.  And I again have doubts about if I solved the right problem.&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;Did Jeff mean &#034;the likelihood that at least 3 of the 50 would be interested in Clojure&#034;?  After all, from the description of the story I can&#039;t infer that the other 47 are not interested in Clojure.&lt;/p&gt;

&lt;p&gt;The probability of that would be&lt;/p&gt;

&lt;center&gt;
&lt;i&gt;&lt;big&gt;&amp;Sigma&lt;/big&gt;&lt;sub&gt;i = 3&lt;/sub&gt;&lt;sup&gt;15&lt;/sup&gt; P(100, 15; 50, i)&lt;/i&gt;
&lt;/center&gt;

&lt;p&gt;which turns out to be 99.8%.&lt;/p&gt;

&lt;p&gt;(&lt;em&gt;I think I&#039;ll turn my work in now.  Or I&#039;ll start doubt my solution again tomorrow morning.&lt;/em&gt;)&lt;/p&gt;


        </description>
      
      
    
    
    
    <comments>http://www.weiqigao.com/blog/2009/08/03/jeff_browns_probability_quiz_what_are_the_chances.html#comments</comments>
    <guid isPermaLink="true">http://www.weiqigao.com/blog/2009/08/03/jeff_browns_probability_quiz_what_are_the_chances.html</guid>
    <pubDate>Tue, 04 Aug 2009 02:40:08 GMT</pubDate>
  </item>
  
  <item>
    <title>Mark Volkmann: Clojure---Functional Programming for the JVM (And a Friday Quiz)</title>
    <link>http://www.weiqigao.com/blog/2009/05/15/mark_volkmann_clojure_functional_programming_for_the_jvm_and_a_friday_quiz.html</link>
    
      
        <description>
          &lt;p&gt;Just want to let everyone know that &lt;a href= &#034;http://www.ociweb.com/mark/&#034; &gt;Mark Volkmann&lt;/a&gt; did the &lt;a href= &#034;http://www.ociweb.com/javasig/knowledgebase/2009-05/index.html&#034; &gt;Clojure presentation&lt;/a&gt; at the &lt;a href= &#034;http://www.ociweb.com/javasig/&#034; &gt;St. Louis Java Users Group&lt;/a&gt; yesterday evening.&lt;/p&gt;

&lt;p&gt;The slides are here:&lt;/p&gt;

&lt;center&gt;
&lt;a href= &#034;http://www.ociweb.com/javasig/knowledgebase/2009-05/ClojureSlides.pdf&#034; &gt;
&lt;img src=&#034;http://www.weiqigao.com/blog/images/clojure-slides.png&#034; title=&#034;Clojure Slides&#034; alt=&#034;Clojure Slides&#034; /&gt;
&lt;/a&gt;
&lt;/center&gt;

&lt;p&gt;Please also check out &lt;a href= &#034;http://java.ociweb.com/mark/clojure/&#034; &gt;Mark&#039;s Clojure page&lt;/a&gt; for an constantly updated version of his February 2009 JNB article on the same topic.&lt;/p&gt;

&lt;p&gt;&lt;a href= &#034;&#034; &gt;CodeToJoy&lt;/a&gt; did a &lt;a href= &#034;http://codetojoy.blogspot.com/2009/05/case-for-clojure.html&#034; &gt;pre-game analysis&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In honor of the presentation, today&#039;s quiz is a clojure one:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Q&lt;/b&gt;: What would the following code produce:&lt;/p&gt;

&lt;pre style=&#034;margin-left:3em&#034;&gt;(defn f [x [y z]]
        [y [z x]])
(f 1 [2 3])&lt;/pre&gt;
        </description>
      
      
    
    
    
    <comments>http://www.weiqigao.com/blog/2009/05/15/mark_volkmann_clojure_functional_programming_for_the_jvm_and_a_friday_quiz.html#comments</comments>
    <guid isPermaLink="true">http://www.weiqigao.com/blog/2009/05/15/mark_volkmann_clojure_functional_programming_for_the_jvm_and_a_friday_quiz.html</guid>
    <pubDate>Fri, 15 May 2009 15:16:54 GMT</pubDate>
  </item>
  
  </channel>
</rss>

