<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2012573949883447809</id><updated>2012-02-22T01:14:05.115+01:00</updated><category term='rivieradev'/><category term='ejb2'/><category term='scala'/><category term='sieve'/><category term='scalaz'/><category term='java'/><category term='euler'/><category term='clojure'/><category term='groovy'/><category term='logic'/><category term='devoxx'/><category term='haskell'/><category term='fold'/><category term='hadoop'/><title type='text'>void-main-args</title><subtitle type='html'>Personal notes on anything about programming.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>46</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-5539603397003848357</id><published>2012-02-22T01:13:00.002+01:00</published><updated>2012-02-22T01:14:05.122+01:00</updated><title type='text'>Gatling Tool at Riviera Scala Clojure</title><content type='html'>This evening, at &lt;a href="http://www.meetup.com/riviera-scala-clojure"&gt;Riviera Scala Clojure&lt;/a&gt;, we received Stéphane Landelle and Romain Sertelon, Gatling Tools developers.&lt;br /&gt;&lt;br /&gt;It's been quite some times I look for stress test tool that is intended for developers, instead of testers.&amp;nbsp;You know, soemething like Caliper, but for stress tests.For a simple reason, I would like to profile my application and identify the problem on the load before even going to QA testing. Profiling can be done using a tool like Yourkit. But for load test, I haven't found anything convincing.&lt;br /&gt;&lt;br /&gt;Of course, I used JMeter in the past. But I really want to have something that can be coded, but stays simple. JMeter, well...., it's &amp;nbsp;not that.&lt;br /&gt;&lt;br /&gt;So, Gatling caught my attention. It has already caught my attention since Stéphane mentioned it in Paris Scala User Group mailing list back in December. So, here they are, &amp;nbsp;presenting the product in front of Riviera Scala Clojure group.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Performance, DSL, and Reporting&lt;/b&gt;&lt;br /&gt;One thing that makes Gatling different is its performance. The performance of Gatling is claimed to be superior than JMeter. This is because Gatling uses Akka and AsyncHttp that allow optimization of concurrent execution of scenarios. The argument really makes sense to me, and I would really love to see it in real. Check their wiki to see the performance benchmark compared to JMeter:&amp;nbsp;&lt;a href="https://github.com/excilys/gatling/wiki/Benchmarks"&gt;https://github.com/excilys/gatling/wiki/Benchmarks&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The second important point in Gatling is DSL. The DSL is intended to allow programmatic scenario to be expressed easily. DSL in Gatling, &amp;nbsp;is nothing more than a fluent API, so, no need to worry, it's Scala. Check &lt;a href="https://github.com/excilys/gatling/wiki/Benchmarks"&gt;here &lt;/a&gt;to see the examples of the API.&lt;br /&gt;&lt;br /&gt;You may notice that the API is quite simple -- maybe too simple? I found the DSL too imperative, but maybe this is a pragmatic choice by Gatling developer. I would have preferred the DSL to be a little bit more functional with use of pattern matching, partial function, and function composition, and other functional programming concepts instead of sequencing, the use of doIf, and loop.&lt;br /&gt;&lt;br /&gt;The reporting is actually an interesting part of Gatling. Unfortunately, we were not lucky enough to have a full blown presentation on it, only an after presentation discussion. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Conclusion&amp;nbsp;&lt;/b&gt;&lt;br /&gt;Gatling is an interesting tool to see if you"re interested in having a lightweight load test tool &amp;nbsp;like me. Its performance benchmark is quite promising. I have had a look at the codes, played with it, and it looks quite promising. The wiki page contains quite extensive information. But, I give you a puzzle: please find me in the wiki a scala file that shows the scenario example. Go ahead, if you can find it in 30 minutes, you're better than I am :-)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2012573949883447809-5539603397003848357?l=voidmainargs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/5539603397003848357/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2012573949883447809&amp;postID=5539603397003848357&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/5539603397003848357'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/5539603397003848357'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/2012/02/this-evening-at-riviera-scala-clojure.html' title='Gatling Tool at Riviera Scala Clojure'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-6806467388740804103</id><published>2012-01-18T13:27:00.000+01:00</published><updated>2012-01-18T13:27:49.531+01:00</updated><title type='text'>Against SOPA.</title><content type='html'>&lt;span style="font-size: large;"&gt;To show my support all protest movements against SOPA, I close for today my blog. Will be back tomorrow.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2012573949883447809-6806467388740804103?l=voidmainargs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/6806467388740804103/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2012573949883447809&amp;postID=6806467388740804103&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/6806467388740804103'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/6806467388740804103'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/2012/01/sopa-protestation.html' title='Against SOPA.'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-675962740612499804</id><published>2011-12-31T16:30:00.001+01:00</published><updated>2012-01-19T07:15:03.803+01:00</updated><title type='text'>On 2011 and 2012</title><content type='html'>&lt;b&gt;The good things&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Joining &lt;a href="http://www.amadeus.com/"&gt;Amadeus&lt;/a&gt; is the best thing that happened to me this year. It was a little bit complicated, but I'm happy that it's finally done. The travel industry has a lot of&amp;nbsp; very very interesting applications of computer science. And what's better than joining the leader in the field ?&lt;br /&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;br /&gt;&lt;i&gt;Needless to say, however, that the rest of the blog content will be all mines, not my employer.&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;When I started the year 2011, I had used Scala for a year, mainly for my&amp;nbsp; part-time master degree. 2012 is then my second year only with Scala.&amp;nbsp;I introduced Scala two times this year, one for Amadeus and the other was for SII. In both occasions, the public showed strong interests in this language.&lt;a href="http://manning.com/suereth/"&gt; Scala in Depth &lt;/a&gt;is the new book on Scala I read this year(well, the only one, actually). It's an interesting book, and can't wait to read the final version.&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;a href="http://3.bp.blogspot.com/-kTqYTweKvJM/Tv87WMTQ_bI/AAAAAAAAAEw/ewycJjDt7v4/s1600/scala.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="113" src="http://3.bp.blogspot.com/-kTqYTweKvJM/Tv87WMTQ_bI/AAAAAAAAAEw/ewycJjDt7v4/s320/scala.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;I started learning Clojure also in 2010 using Stuart Halloway, &lt;a href="http://www.amazon.com/Programming-Clojure-Pragmatic-Programmers-Halloway/dp/1934356336/ref=sr_1_2?ie=UTF8&amp;amp;qid=1325349827&amp;amp;sr=8-2"&gt;Programming in Clojure&lt;/a&gt;. But in 2011, reading The Joy of Clojure and Clojure in Action brought me to better understanding of this very interesting language. Clojure has also a very interesting community, many of them are very intelligent. Most of all, Clojure community do not seem to care on whether or not they become Java killer or Java replacements. In this regard, compared to Scala community, they are cooler.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;a href="http://1.bp.blogspot.com/-5xkAUci_UUw/Tv87k22adZI/AAAAAAAAAE8/NtwlBrJUC18/s1600/clojure.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-5xkAUci_UUw/Tv87k22adZI/AAAAAAAAAE8/NtwlBrJUC18/s1600/clojure.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;What to say about Haskell? Simply the best mainstream language exist out there. Compared to 2010, I read a lot of papers on Haskell. And what superlative to be used to describe Monad Reader? Simply the best.&lt;br /&gt;&lt;br /&gt;If there is only one conference I would like to attend, I would name "Strange Loop". I read almost all slides, check almost all videos that made online. Of course, I didn't miss the best presentation of the year for me, which is &lt;a href="http://www.infoq.com/presentations/Simple-Made-Easy"&gt;Rich Hickey's Simple Made Easy&lt;/a&gt;. It's really my dream to be able to attend the conference some day.&lt;br /&gt;&lt;br /&gt;Participating in organization of &lt;a href="http://www.rivieradev.fr/"&gt;RivieraDev &lt;/a&gt;conference is another interesting moment. It's a small conference with a quality comparable to bigger European conferences. I loved the high and low, non stop thinking about the contents and the corresponding speakers were very interesting experience. Would love to do it again in 2012.&lt;br /&gt;&lt;br /&gt;This year, I also started to be interested in machine learning . I started with &lt;a href="http://www.manning.com/owen/"&gt;Mahout in Action&lt;/a&gt;, followed by &lt;a href="http://www.manning.com/pharrington/"&gt;Machine Learning in Action&lt;/a&gt;, two excellent books from Manning. I also read &lt;a href="http://www.manning.com/alag/"&gt;Collective Intelligence in Action&lt;/a&gt; and &lt;a href="http://www.manning.com/marmanis/"&gt;Algorithms of Intelligent Web&lt;/a&gt;, also from Manning. Finally, without surprise, I've also finished Standford ML-Class. That was an excellent class and surely will help me better understand the subject. If you didn't attend this class, please do for the next term.&lt;br /&gt;&lt;br /&gt;Finally, with Nicolas Bousquet and Tobo Atchou, we manage to found the long-term project of organizing user group around Scala and e. We named it Riviera Scala Clojure, following the name pattern given by Riviera Ruby and Riviera User Group. I love this group, and am optimistic on its future.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;a href="http://4.bp.blogspot.com/-J0gIKiK45U8/Tv89zj2OaXI/AAAAAAAAAFI/g_RKYvq0Nc0/s1600/logoriviera.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="131" src="http://4.bp.blogspot.com/-J0gIKiK45U8/Tv89zj2OaXI/AAAAAAAAAFI/g_RKYvq0Nc0/s200/logoriviera.png" width="200" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;The bad things&lt;/b&gt; &lt;br /&gt;&lt;br /&gt;If I have to mention one bad thing this year, it would be a repetitive attack against Scala. Some of them were really unfounded. I really wonder why Scala had such difficult moments this year. Maybe this is because there are more and more developers use Scala and they expect to have the same supports as Java. Or maybe, because functional programming is indeed not an easy thing to understand. Well, I found that the Scala leaders (Typesafe, for example) have reacted quite positively.&lt;br /&gt;&lt;br /&gt;Other thing that I regret is that I didn't manage to go to either Devoxx or Fosdem, two European interesting conferences. Not to mention Scala Days 2011 that was not in Europe.&lt;br /&gt;&lt;br /&gt;I also regret the fact that I didn't manage to learn more on distributed systems like Hadoop, &lt;a href="http://www.spark-project.org/"&gt;Spark&lt;/a&gt;, and two interesting projects that address BSP like &lt;a href="http://incubator.apache.org/giraph/"&gt;Apache Giraph&lt;/a&gt; and &lt;a href="http://incubator.apache.org/hama/"&gt;Hama&lt;/a&gt;. I didn't really have enough time to learn those interesting things. I would say that, put aside Mahout, I didn't really add my knowledge on Hadoop (and its ecosystem) from what I have learned in 2010.&lt;br /&gt;&lt;br /&gt;Somewhere in October, I was in nostalgic mode, and started re-learn Prolog. Unfortunately, it was very quickly abandoned due to also the lack of time.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;2012&lt;/b&gt;&lt;br /&gt;I would like to "make a comeback" to Hadoop&amp;nbsp; and Lucene&amp;nbsp; world :-). I will start my personal project around Lucene starting this year. I also hope that the project can benefit something from Spark and/or Mahout.&lt;br /&gt;&lt;br /&gt;Indeed, I think I will do a lot of things around text processing, machine learning, and all those things. Without surprise, I will register to Standford &lt;a href="http://www.nlp-class.org/"&gt;NLP&lt;/a&gt; and &lt;a href="http://www.pgm-class.org/"&gt;PGM &lt;/a&gt;classes.&lt;br /&gt;&lt;br /&gt;I also expect that in 2012, I'll learn a lot on building distributed system. That's why I will put some efforts on Akka's world, or Actor's world in general. This includes of course Erlang. Yes, I would like to seriously learn Erlang in 2012.&lt;br /&gt;&lt;br /&gt;I've learned functional programming a lot from Haskell and Clojure --&amp;nbsp; in addition to Scala. I plan to learn Erlang, and to complete the figure of functional programming, I would like to learn OCaml, one of the most important language in functional programming.&lt;br /&gt;&lt;br /&gt;That said, of course,&amp;nbsp; Scala, Clojure, Haskell, and yes: Java (I still love my aging Java) will be my main  concerns in 2012. &lt;br /&gt;&lt;br /&gt;If I manage to have everything in time, I will attend &lt;a href="http://days2012.scala-lang.org/"&gt;Scala Days 2012&lt;/a&gt;. Hopefully on or two other meetings in Scala/Clojure in Europe, and why not participating in French Scala Days organization or something similar? (If you plan to organize one and look for volunteers, you know who to contact :-)&lt;br /&gt;&lt;br /&gt;Riviera Scala Clojure will make sure that I consume my free time.&amp;nbsp; I hope the group to become bigger and will give significant contribution to Scala or Clojure community in general.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Happy new year !&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2012573949883447809-675962740612499804?l=voidmainargs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/675962740612499804/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2012573949883447809&amp;postID=675962740612499804&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/675962740612499804'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/675962740612499804'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/2011/12/on-2011-and-2012.html' title='On 2011 and 2012'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-kTqYTweKvJM/Tv87WMTQ_bI/AAAAAAAAAEw/ewycJjDt7v4/s72-c/scala.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-4538106783520028517</id><published>2011-11-23T02:55:00.001+01:00</published><updated>2012-01-19T07:15:03.692+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ejb2'/><category scheme='http://www.blogger.com/atom/ns#' term='scalaz'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Scalaz Seven Functor Feels like Seven Samurai</title><content type='html'>&lt;span style="font-size: small;"&gt;Jason Zaugg and all his scalaz folks are working on scalaz seven at the moment. Although they have started quite a couple of times ago, I only had a chance to play with the librar&lt;/span&gt;y only today.&lt;br /&gt;&lt;br /&gt;In case you don't know scalaz, it's actually a popular but often misunderstood library written in scala. It's not haskell standard library ported to scala, but it's highly inspired by the haskell standard library though. I recommend you to have a look here &lt;a href="https://github.com/scalaz/scalaz"&gt;scalaz&lt;/a&gt; . One recommendation: take your time, don't be hurry to understand, otherwise you will end up associate scalaz to banana [1], to rabbit [2], or to ejb2 [3]. Of course, scalaz is not ready for production, because you know ... you cannot persist a scalaz object using Hibernate. &lt;br /&gt;&lt;br /&gt;I start with one of the simplest scalaz type class, called Functor. It sounds scarry, right ? Don't worry, think of Functor as a way to lift a function that maps A to B to something that map "container" of A to "container" of B. If you have a function that maps, from int to int, say, a functor allows mapping from List of integer to a list of integer by mapping each element of the list.&lt;br /&gt;&lt;br /&gt;Say, you have a function incr:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val incr:Int=&amp;gt;Int= x =&amp;gt; x + 1&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;a Functor[List] allows you to map every element in a list using that function like this:&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val xs = List(5, 6, 7)&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Functor[List].map(xs, incr)&amp;nbsp;&amp;nbsp; // 6, 7, 8&lt;/div&gt;&lt;br /&gt;Well, yeah, is that all? No ! I can also have this:&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val eitX:Either[String, Int] = Right(6)&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val eitY:Either[String, Int] = Left("Err") &lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Functor[Either].map(eitX, incr) // Right(7)&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;Functor[Either].map(eitY, incr) // Left("Err")&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;It starts to be interesting right, because you don't have map for Either in Scala standard library. Fine, is it great ?&amp;nbsp; No, it sucks. Function[Either] or Function[List] suck. We don't want it. Fortunately, scalaz comes wi th some magics that simplify the thing&lt;br /&gt;&lt;br /&gt;(wait a minute? Scalaz ? Simplifies something? You must be kidding ?) &lt;br /&gt;&lt;br /&gt;With the super complex highly intelligent Scalaz (as it is perceived&amp;nbsp; by many), indeed life is simpler, we can directly write:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val eitX:Either[String, Int] = Right(6)&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val eitY:Either[String, Int] = Left("Err") &lt;/div&gt;eitX.map(incr)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Right(7)&lt;br /&gt;eitY.map(incr)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Left("Err")&lt;br /&gt;&lt;br /&gt;That works, because Either, List, Option are instances of Functor (you know, the container that allows you to lift a function).&lt;br /&gt;&lt;br /&gt;Is that all ? No. There are something even more interesting. Imagine if we have a List of Option, is it a functor ? Oh yes, it turns out that the composition of functors is a functor. Let's check:&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val listCompOpt = Functor[List].compose(Functor[Option])&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val ys = List(Some(1), Some(3), None, Some(6))&lt;br /&gt;val zs = listCompOpt.map(ys)(incr)&amp;nbsp; // List(Some(2), Some(4), None, Some(7))&lt;/div&gt;&lt;br /&gt;Yeah! What about the product ?&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val listOptProdF = Functor[List].product(Functor[Option])&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val ts = (List(1,2),Some(1))&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val us = (List(3,7, 2, 1),None)&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;listOptProdF.map(ts)(incr)&amp;nbsp;&amp;nbsp; // (List(2, 3),Some(2))&lt;/div&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;listOptProdF.map(us)(incr)&amp;nbsp;&amp;nbsp; // (List(4, 8, 3, 2),None)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Oh, Cool.&lt;br /&gt;&lt;br /&gt;OK, to finish this post, scalaz-seven (at least the version I was playing with) provides a couple of interesting functions like strengthL, strengthR, and fpair.&lt;br /&gt;&lt;br /&gt;Here they are:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val xEit:Either[String, Int] = Right(3)&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val yEit:Either[String, Int] = Right(4)&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;println(xEit.strengthL("Help"))&amp;nbsp;&amp;nbsp;&amp;nbsp; // Right("Help",4)&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;println(yEit.strengthL("Help"))&amp;nbsp;&amp;nbsp;&amp;nbsp; // Right(("Help",3))&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val xOpt:Option[Int] = Some(4)&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val yOpt:Option[Int] = None&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;println(xOpt.strengthR("my godness"))&amp;nbsp;&amp;nbsp; // Some(4, "my godness") println(yOpt.strengthR("My godness"))&amp;nbsp; // None&lt;br /&gt;&lt;br /&gt;println(xOpt.fpair)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Some(4,4)&lt;br /&gt;println(xEit.fpair)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Right(3,3)&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;br /&gt;&lt;/div&gt;I&amp;nbsp; hope to be able to come with other posts in this series. No promise, but stay tuned.&lt;br /&gt;&lt;br /&gt;----------------&lt;br /&gt;[1] Banana in this article context is not a scala or a java library, it is a fruit name. In case you're not familiar with it, check this &lt;a href="http://en.wikipedia.org/wiki/Banana"&gt;article&lt;/a&gt;. You may wonder how could one wrongly associate scalaz with banana. Well, that may happen, who knows. &lt;br /&gt;&lt;br /&gt;[2] Rabbit in this article context is not a scala or a java library, not even a cool javascript library, it is a name of an animal. Again, &lt;a href="http://en.wikipedia.org/wiki/Rabbit"&gt;wikipedia&lt;/a&gt; is helpful in case you're not familiar with it. Well, again, it may happen that you associate scalaz to rabbit, who knows.&lt;br /&gt;&lt;br /&gt;[3] &lt;a href="http://www.oracle.com/technetwork/java/javaee/ejb/index.html"&gt;EJB2&lt;/a&gt; is a server side component, usually managed by the application server. It is used mainly in enterprise application, and by the way the 'E' is Enterprise. You should not wonder why you might associate scalaz to EJB2, it &lt;a href="http://blog.joda.org/2011/11/scala-feels-like-ejb-2-and-other.html"&gt;happened&lt;/a&gt; !&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2012573949883447809-4538106783520028517?l=voidmainargs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/4538106783520028517/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2012573949883447809&amp;postID=4538106783520028517&amp;isPopup=true' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/4538106783520028517'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/4538106783520028517'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/2011/11/scalaz-seven-functor-feels-like-seven.html' title='Scalaz Seven Functor Feels like Seven Samurai'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-1730572780789128458</id><published>2011-10-21T22:01:00.001+02:00</published><updated>2012-01-19T07:15:03.644+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='rivieradev'/><title type='text'>RivieraDev Day 2 Wrap Up - With Epilog Too</title><content type='html'>Another interesting day today. There were less participants than yesterday, although the talks are actually at least as interesting as yesterday.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Hibernate OGM by Emmanuel Bernard&lt;/b&gt;&lt;br /&gt;We might wonder (well, I'm still wondering even after the talk) why we need JPA for NoSQL. Even worse, a JPQL for NoSQL, quite contradictory isn't it? &lt;br /&gt;&lt;br /&gt;That was actually Emmanuel Bernard's presentation I attended this morning (in French). At the beginning, the goal of OGM is to use JPA for Infinispan. But, the Hibernate team found out that the JPA could be generalized to NoSQL in general. That's why they start to implement Hibernate OGM to No SQL databases.&lt;br /&gt;&lt;br /&gt;Emmanuel explained four types of NoSQL databases: key value, big table (e.g. HBase), graph database (Neo4J), or document based (MongoDB) one. At the moment, OGM &amp;nbsp;supports Neo4J and MongoDB that are apparently keen to participate in OGM development.&lt;br /&gt;&lt;br /&gt;All in all, I find the presentation not that interesting and not that convincing. A good presentation to start the morning though.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Scala by Fredrik Ekholdt&lt;/b&gt;&lt;br /&gt;Fredrik explained a lot of things on Scala in this talk. Too many, in my opinion. For&amp;nbsp;some, that might be interesting, because we can see a lot of things in once, but for some others that might be too hard to follow, hence not interesting.&lt;br /&gt;&lt;br /&gt;The presentation started with quick introduction to Scala, before going into detail to trait and loan pattern, higher order function, duck typing, functional programming and data structure, parallel collection, implicit, and pimp my library. All in 50 minutes presentation !&lt;br /&gt;&lt;br /&gt;I couldn't really judge the content, since nothing really new in his presentation today, after all it's a Scala introduction talk. I think it should have been better if Fredrik focused on some important features and not to go to many things.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Play! by Nicolas Leroux and Nicolas Martignoles (A little GQuery)&lt;/b&gt;&lt;br /&gt;Well, I'm sorry not to be able to say a lot for this presentation. I had to leave the session doing some buffet lunch preparation after the first 20 minutes.&lt;br /&gt;&lt;br /&gt;But, from the part that I saw, I really think that Play! is a good idea and helpful. I would love to see the framework more detail. Even better, Play! 2.0 core will become Scala, what can be better than that ?&lt;br /&gt;&lt;br /&gt;Oh, yes. I could also spend some 5 minutes or so in Manolo's presentation on GQuery. It looks like to be an interesting thing to see. Also, at the end of the talk, he announced the release of GQuery 1.1 today ! Thanks to choose RivieraDev to make the announce.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Ceylon by Stephane Epardaud and Cucumber by Frederick Ros&amp;nbsp;&lt;/b&gt;&lt;br /&gt;To be honest, the presentation on three JVM languages are not that impressive: Scala, Kotlin, and Ceylon. The presenters went too detail into the detail of languages, but less in philosophical choices (if any). But, that was not the reason I left the session after 30 minutes: it was planned that I would see the two presentations.&lt;br /&gt;&lt;br /&gt;Ceylon as a language is quite boring, actually. Nothing specially mind-blowing in Ceylon (Scala: well, it's mind-blowing, reified generic in Kotlin is actually interesting) I have an impression Ceylon is there to reduce a little bit the frustration ones have with Java, but still keep all the rest, especially the possibility to work with famous java frameworks (yeah, yeah: hibernate). Oh, I have to say nevertheless that Union Class is insteresting.&lt;br /&gt;&lt;br /&gt;I missed the beginning of Cucumber presentation. When I arrived in the room, Frederick was doing some demos. Then, he presented some interesting use cases of Cucumber, including the BDD, or even the use of Cucumber to monitor production (not sure to understand what this meant though). &amp;nbsp;There were only around twenty people in the room, but the discussion that followed the presentation was quite excellent (that might be the benefit of small public). One question I love much was "Is there really helpful for somebody without computer science all this specification written in natural language, but still expressed at the end in programming language? " That's exactly my doubt on BDD though. I still feel the "jump to solution, without real requirements analysis" is in the BDD approach. Is it really the best way to make "life specification?" . BDD is still programmer oriented from my point of view.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Coffee Script by Bodil Stokke&lt;/b&gt;&lt;br /&gt;This talk is the best talk of the day. It actually saved the day that at the end relatively less interesting than Thursday.&lt;br /&gt;&lt;br /&gt;Bodil Stokke presented the Coffee Script excellently. She showed really the essence of the coffee script: make clean solution, a lot of use of spaces, nice syntaxes, and all those things. Nothing really mind-blowing in coffee script though, it's just a better syntax than java script. There's no, for example, the concept of isolate, that quite interesting in dart.&lt;br /&gt;&lt;br /&gt;It was not the content who made Bodil's presentation interesting though. It was the way she presented the subject. The jokes about the programming language creator were very funny. From Bodil's presentation we can see however that Coffee Script is a serious attempt to improve java script. The syntaxes are indeed very nice, like destructuring [sum, diff] = (a,b) -&amp;gt; [a + b, a -b].&lt;br /&gt;&lt;br /&gt;All in all, the presentation was excellent, she succeded to show how nice coffee script was. But honestly, if you stick to use Java Script in a discipline way (see eloquent java script book), I don't think we really need Coffee Script. Dart is clearly something else -- unfortunately, it does not have nice syntaxes of Coffee Script though.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;OPA by Louis Gesbert&lt;/b&gt;&lt;br /&gt;For me, the mind-blowing approach of web development is incarnated by OPA. They really attack the fundamental problem of web development: heterogeneity of the system, starting from client, server, and the database. They are really heterogen, for example, the programming languages are different, the presentation of the data is different. For example relational database is yet another form of representation of data in the system that uses object oriented and java script in the client.&lt;br /&gt;&lt;br /&gt;Those heterogeneity is indeed the problem that every body seems to try to solve, starting from Dart that wants to be a language that can be used in big application as well as a client one, node js, or even GWT.&lt;br /&gt;&lt;br /&gt;So, OPA tries to provide a comprehensive solution that unifies the way to develop web application. Not surprisingly, functional programming language is used for this purpose. Indeed, with functional programming, you can define what the system is, and not how the system should be architected. For example, the automatic slicing client/server applications provided in OPA is a very cool idea.&lt;br /&gt;&lt;br /&gt;In his presentation, however, Louis was not really able to show the concrete solution that OPA tried to propose. &amp;nbsp;I did not really get how the problem of&amp;nbsp;heterogeneity &amp;nbsp;could really be solved by OPA. Maybe I should check OPA more to know better the product.&lt;br /&gt;&lt;br /&gt;Oh, yes. It was quite ashamed to see very few attendees were in this session. Maybe it was too hard to stay in a conference on Friday evening.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Epilog&lt;/b&gt;&lt;br /&gt;Finally, I really think that RivieraDev was a good conference, with nice programs. The programs can still be improved , but the important things might be the number of attendees , especially on the second day. But, small number of participants could also be positive as I saw in Cucumber session that I found to have really interesting discussions.&lt;br /&gt;&lt;br /&gt;I'm quite disappointed with Ceylon, Scala, and Kotlin presentations. Not on the content, but how they were presented. I don't really care about the exact syntaxes or features, because in one hour, I could not do anything. In one hour presentation, one should attack more on philosophical point of view of the language, instead of syntax things.&lt;br /&gt;&lt;br /&gt;Surprisingly, all Java scripts related sessions were quite interesting. I love Dart and CoffeeScript sessions, and heard a lot of nice things on JQuery. I had also a good impression on GQuery presentation. I would have been happier however if a real Java Script supporter to defend the languages. I still believe that it is possible to write Java Script cleanly (like it is the case in Eloquent Java Script book). Maybe, next year, the supporter of "clean" and plain Java Script should be invited.&lt;br /&gt;&lt;br /&gt;See you next year.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2012573949883447809-1730572780789128458?l=voidmainargs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/1730572780789128458/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2012573949883447809&amp;postID=1730572780789128458&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/1730572780789128458'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/1730572780789128458'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/2011/10/rivieradev-day-2-wrap-up-with-epilog.html' title='RivieraDev Day 2 Wrap Up - With Epilog Too'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-2850770321229577496</id><published>2011-10-21T06:51:00.000+02:00</published><updated>2012-01-19T07:15:03.663+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='rivieradev'/><title type='text'>RivieraDev Day 1 Wrap Up</title><content type='html'>It was an interesting day we had on Thursday at Riviera Dev. &lt;br /&gt;&lt;br /&gt;The conference started with a keynote from Stephane Epardaud, Inria, and some sponsors. Nothing much interesting in the keynote content, except that now I know that Inria puts (even) more efforts on programming languages. This is very cool.&amp;nbsp; &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Dart, by Florian Loitsch&lt;/b&gt;&lt;br /&gt;Then, the talks started. At the first session, Florian Loitsch from Google presented Dart. I was sooo sceptical about Dart, but I decided to come to his session anyway. To my surprise, his presentation reduced my scepticism on Dart. Not on the infamous "optional typing" on which I still have my reserves, but on the fact that Dart is not actually that boring. The Isolate concept, interface with factory, and couple other things are very neat. Florian actually did an excellent job in his presentation to demo all these things live.&lt;br /&gt;&lt;br /&gt;I had also some interesting discussions with him at lunch time about Dart, the role of Gilad Bracha, how Dart team works, and so on. All in all, his talk was almost my favorite talk at the conference.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Kotlin, by Dmitry Jemerov&lt;/b&gt;&lt;br /&gt;All right. After Dart, another language, Kotlin by Dmitry Jemerov from JetBrain. His talk was actually interesting, but because his session must compete with NodeJS session, the number of participants are not that many, but still he had a lot of audiences.&lt;br /&gt;&lt;br /&gt;Dmitry did not present many new things on Kotlin compared to the Kotlin web site or to Strange Loop presentation on the language. One thing I like in Kotlin is actually reified generics and also on String interpolation, that quite neat. I wonder how type check costs to Kotlin performance though. One other interesting thing from his presetnation was the IDEA demo on Kotlin. It was so short&amp;nbsp; unfortunately, he could have made longer and slower demo though.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;GWT, by Nicolas de Loof&lt;/b&gt; &lt;br /&gt;Nicolas de Loof was a good presenter. His presentation was fluid, and by the way, it was the only French talk I attended.&lt;br /&gt;&lt;br /&gt;In his talk, he presented GWT from very high level point of view. Why GWT, the environments around GWT, and so on and so forth. For me, the talk was too high level though, I expected something more technical or strategic, like GWT after dart (oops, I almost said: GWT after dark). But after all, it was a quite interesting session.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Java 7 / 8, by Simon Ritter&lt;/b&gt;&lt;br /&gt;This talk was supposed to be the talk of the day. And indeed, it was talk of the day in term of audience. The INRIA amphi was quite full.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;He started with long history about Oracle and Sun, JCP and all those things. Then, quickly re, viewed Java 7 features, and finally Java 8. There were not many things in Java 8 though ..., oops sorry, there were a lot of things new in Java 8 of course, but I heard most of them. He showed examples on "closures", which was quite nice, and he talked about the possibility to have parallel collection in Java 8 (sounds familiar, isn't it?).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;How GitHub Uses GitHub to Build GitHub&lt;/b&gt; &lt;br /&gt;The star of the day is Zach Holman. His talk (was it a rant ? ) was astonishing and inspiring. &lt;br /&gt;&lt;br /&gt;He started with agile-bashing (I love a presenter that starts his presentation by bashing agile) and then started to explain on how GitHub work, something he called working asynchronously. Then, about the crazy practices of branching of branch of branch of branch, and how he sees the things should have been done simpler. He also explained about pull requests as a communication tool, about Hubot, and all those things.&lt;br /&gt;&lt;br /&gt;His rant was finally quite depressing for those who work with a lot of processes and less real things. The idea of Zach is to put to maximum the possibility of doing real things instead of those process stuffs like meetings, complicated problem report, and all other things that finally counter-productive.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Speaker Dinners / Open Café&lt;/b&gt;&lt;br /&gt;Open Café at the end of the day was also interesting. I discussed a lot with Henri Gomez (DevOps),&amp;nbsp; Nicolas Leroux (Play!), Bodil Stokke (Coffee Script), Fredrik Ekholdt (Scala, TypeSafe) and my colleague Nicolas Bousquet. We had a lot of interesting things discussed, including what differences between Norwegians and Swedish :-) (Fredrik and Bodil are Norwegians) , Scala and TypeSafe, and all other things.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Summary&lt;/b&gt; &lt;br /&gt;That was an interesting inspiring day. I hope to have similar experience today. Hope to see another depressing presentation again :-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2012573949883447809-2850770321229577496?l=voidmainargs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/2850770321229577496/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2012573949883447809&amp;postID=2850770321229577496&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/2850770321229577496'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/2850770321229577496'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/2011/10/rivieradev-day-1-wrap-up.html' title='RivieraDev Day 1 Wrap Up'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-6501597842764428230</id><published>2011-09-26T07:24:00.010+02:00</published><updated>2012-01-19T07:15:03.796+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='logic'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Semantic Tableaux in Less than 90 Lines of Scala</title><content type='html'>This week challenge for me was to write a code that can check a propositional logic formula like&lt;br /&gt;(¬(p ⋀ q) ↔ (¬p &amp;nbsp;∨ ¬q)) ∧ ( ¬r ∧ q) &amp;nbsp;and check if the formula is valid or satisfiable. A formula is valid when it is always true under any interpretations of all its atoms. A formula is satisfiable when there is some interpretation of its atoms that can make the proposition true.&lt;br /&gt;&lt;br /&gt;The complete code for this post is available &lt;a href="https://gist.github.com/1239945"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Semantic Tableaux&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;For example, p&amp;nbsp;⋀ q is satisfiable, since if p and q are both true, then the formula is true. (p &amp;nbsp;∨&amp;nbsp;¬p) &amp;nbsp;⋀ (q&amp;nbsp;∨ ¬q) is valid, because regardless the interpretation of p and q, the formula is always true.&lt;br /&gt;&lt;br /&gt;One way to implement the satisfiability and validity check is by creating truth table. But, the use of truth table is always exponential in number of atoms. There is fortunately a simple technique to check the validity and satisfiability a propositional logic formula. The technique is called Semantic Tableaux. I need to get my logic book I used back to the time of 2nd year of university (Ben-Ari, 1992 ) to remind me how it works. Basically, there are 9 rules, 5 α&amp;nbsp;rules, and 4&amp;nbsp;&lt;span class="Apple-style-span" style="font-size: 12px;"&gt;β&amp;nbsp;&lt;/span&gt;rules. &lt;br /&gt;&lt;br /&gt;The following are the 9 rules:&lt;br /&gt;&lt;br /&gt;α Rules&lt;br /&gt;&lt;br /&gt;&lt;table border="2"&gt;&lt;tbody&gt;&lt;tr&gt;      &lt;th&gt;α&lt;/th&gt; &lt;th&gt;α1&lt;/th&gt; &lt;th&gt;α2&lt;/th&gt; &lt;/tr&gt;&lt;tr&gt;     &lt;td&gt;¬ ¬A&lt;/td&gt;  &lt;td&gt;A&lt;/td&gt;   &lt;td&gt;&lt;/td&gt;    &lt;/tr&gt;&lt;tr&gt;     &lt;td&gt;A1 ∧ A2&lt;/td&gt;  &lt;td&gt;A1&lt;/td&gt;   &lt;td&gt;A2&lt;/td&gt;    &lt;/tr&gt;&lt;tr&gt;     &lt;td&gt;¬(A1 ∨ A2)&lt;/td&gt; &lt;td&gt;¬A1&lt;/td&gt; &lt;td&gt;¬A2&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;     &lt;td&gt;¬(A1 → A2)&lt;/td&gt; &lt;td&gt;A1&lt;/td&gt; &lt;td&gt;¬A2&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;     &lt;td&gt;(A1 ↔ A2)&lt;/td&gt; &lt;td&gt;A1 → A2 &lt;/td&gt; &lt;td&gt;A2 → A1&lt;/td&gt;  &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;β Rules&lt;br /&gt;&lt;table border="2"&gt;&lt;tbody&gt;&lt;tr&gt;      &lt;th&gt;β&lt;/th&gt; &lt;th&gt;β1&lt;/th&gt; &lt;th&gt;β2&lt;/th&gt; &lt;/tr&gt;&lt;tr&gt;     &lt;td&gt;B1 ∨ B2&lt;/td&gt;  &lt;td&gt;B1&lt;/td&gt;   &lt;td&gt;B2&lt;/td&gt;    &lt;/tr&gt;&lt;tr&gt;     &lt;td&gt;¬(B1 ∧ B2)&lt;/td&gt; &lt;td&gt;¬B1&lt;/td&gt; &lt;td&gt;¬B2&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;     &lt;td&gt;B1 → B2&lt;/td&gt; &lt;td&gt;¬B1&lt;/td&gt; &lt;td&gt;B2&lt;/td&gt;  &lt;/tr&gt;&lt;tr&gt;     &lt;td&gt;¬(B1 ↔ B2)&lt;/td&gt; &lt;td&gt;¬(B1 → B2)&lt;/td&gt; &lt;td&gt;¬(B2 → B1)&lt;/td&gt;  &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;I will not explain the Semantic Tableaux algorithms in detail. You can check a more detail in (Ben-Ari ) or in (&lt;a href="http://imps.mcmaster.ca/courses/CAS-701-02/contributions/sem-tableaux.pdf"&gt;Issawi, 92&lt;/a&gt;). Here is an example of tableau creation for&amp;nbsp;¬(p ⋀ q) ↔ (¬p &amp;nbsp;∨ ¬q) :&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-rMIsJBpAVQk/ToAOwY1_PbI/AAAAAAAAADs/4ICfUYZA65c/s1600/semantictableau.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="472" src="http://4.bp.blogspot.com/-rMIsJBpAVQk/ToAOwY1_PbI/AAAAAAAAADs/4ICfUYZA65c/s640/semantictableau.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/--CACYMxtCDg/ToAObYstM6I/AAAAAAAAADo/hjFGmXJacOo/s1600/semantictableau.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Code&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;Ok, let's start coding now. &amp;nbsp;The target is to have the following application working:&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;object &lt;/span&gt;&lt;span class="s1"&gt;Run { &lt;br /&gt;    &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;main(args:Array[String]):Unit = { &lt;br /&gt;&lt;/span&gt;      &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;import &lt;/span&gt;&lt;span class="s1"&gt;Formulas._ &lt;br /&gt;&lt;/span&gt;      &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;val &lt;/span&gt;&lt;span class="s1"&gt;formula1 = (¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'r&lt;/span&gt;&lt;span class="s1"&gt;) ∧ &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'q&lt;/span&gt;&lt;span class="s1"&gt;) ∧ (¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'p &lt;/span&gt;&lt;span class="s1"&gt;∧ &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'q&lt;/span&gt;&lt;span class="s1"&gt;) ↔ (¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'p&lt;/span&gt;&lt;span class="s1"&gt;) ∨ ¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'q&lt;/span&gt;&lt;span class="s1"&gt;) )) &lt;br /&gt;&lt;/span&gt;      println(isSatisfiable(formula1) + &lt;span class="s2" style="color: green; font-weight: bold;"&gt;"," &lt;/span&gt;&lt;span class="s1"&gt;+ isValid(formula1)) &lt;/span&gt;&lt;span class="s3" style="color: grey; font-style: italic;"&gt;// true, false&lt;/span&gt;&lt;span class="s1"&gt; &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;val &lt;/span&gt;&lt;span class="s1"&gt;formula2 = ¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'p &lt;/span&gt;&lt;span class="s1"&gt;∧ &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'q&lt;/span&gt;&lt;span class="s1"&gt;) ↔ (¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'p&lt;/span&gt;&lt;span class="s1"&gt;) ∨ ¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'q&lt;/span&gt;&lt;span class="s1"&gt;) ) &lt;br /&gt;&lt;/span&gt;      println(isSatisfiable(formula2) + &lt;span class="s2" style="color: green; font-weight: bold;"&gt;"," &lt;/span&gt;&lt;span class="s1"&gt;+ isValid(formula2)) &lt;/span&gt;&lt;span class="s3" style="color: grey; font-style: italic;"&gt;// true, true&lt;/span&gt;&lt;span class="s1"&gt; &lt;br /&gt;&lt;/span&gt;    } &lt;br /&gt;  }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I used a sealed class Formula to represent all formulas. Atom, Conjunction, Disjunction, Implication, Equivalence, and Xor are classes that extends the trait. In addition, I introduced class &amp;nbsp;¬ to represent the negation. Here is how it looks like.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;sealed abstract class &lt;/span&gt;&lt;span class="s1"&gt;Formula  &lt;br /&gt;&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case class &lt;/span&gt;&lt;span class="s1"&gt;Atom(symbol:Symbol) &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;extends &lt;/span&gt;&lt;span class="s1"&gt;Formula &lt;br /&gt;&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case class &lt;/span&gt;&lt;span class="s1"&gt;Conjunction(p:Formula, q:Formula) &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;extends &lt;/span&gt;&lt;span class="s1"&gt;Formula &lt;br /&gt;&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case class &lt;/span&gt;&lt;span class="s1"&gt;Disjunction(p:Formula, q:Formula) &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;extends &lt;/span&gt;&lt;span class="s1"&gt;Formula &lt;br /&gt;&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case class &lt;/span&gt;&lt;span class="s1"&gt;Implication(p:Formula, q:Formula) &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;extends &lt;/span&gt;&lt;span class="s1"&gt;Formula &lt;br /&gt;&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case class &lt;/span&gt;&lt;span class="s1"&gt;Equivalence(p:Formula, q:Formula) &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;extends &lt;/span&gt;&lt;span class="s1"&gt;Formula &lt;br /&gt;&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case class &lt;/span&gt;&lt;span class="s1"&gt;Xor(p:Formula,q:Formula) &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;extends &lt;/span&gt;&lt;span class="s1"&gt;Formula &lt;br /&gt;&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case class &lt;/span&gt;&lt;span class="s1"&gt;¬(p:Formula) &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;extends &lt;/span&gt;&lt;span class="s1"&gt;Formula&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Note that Atom class takes a symbol as its property, so that it allows us to write Atom('p) for example.&lt;br /&gt;&lt;br /&gt;I let the Formula class empty in the example above just to make the examples clear. The content of the class is actually the boolean operation to a formula, like&amp;nbsp;∧,∨, →,&amp;nbsp;↔, and&amp;nbsp;⊕. I benefit from Scala that allows special characters to be used as function name. Here is the class Formula looks like:&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;sealed abstract class &lt;/span&gt;&lt;span class="s1"&gt;Formula { &lt;br /&gt;  &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;∧(q:Formula) = Conjunction(&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;this&lt;/span&gt;&lt;span class="s1"&gt;, q) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;  &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;∨(q:Formula) = Disjunction(&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;this&lt;/span&gt;&lt;span class="s1"&gt;,q) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;  &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;→(q:Formula) = Implication(&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;this&lt;/span&gt;&lt;span class="s1"&gt;,q) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;  &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;↔(q:Formula) = Equivalence(&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;this&lt;/span&gt;&lt;span class="s1"&gt;,q) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;  &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;⊕(q:Formula) = Xor(&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;this&lt;/span&gt;&lt;span class="s1"&gt;, q) &lt;br /&gt;&lt;/span&gt;}&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;br /&gt;With this, we can write&lt;span class="Apple-style-span" style="background-color: white; font-family: monospace; white-space: pre;"&gt;&lt;span class="s0"&gt;(Atom(&lt;/span&gt;&lt;span class="s1" style="color: green; font-weight: bold;"&gt;'p&lt;/span&gt;&lt;span class="s0"&gt;) → Atom(&lt;/span&gt;&lt;span class="s1" style="color: green; font-weight: bold;"&gt;'q&lt;/span&gt;&lt;span class="s0"&gt;)) ∨ Atom(r)&lt;/span&gt;&lt;/span&gt;. Not bad, but better to have directly&amp;nbsp;&lt;span class="Apple-style-span" style="background-color: white; font-family: monospace; white-space: pre;"&gt;&lt;span class="s0"&gt;(&lt;/span&gt;&lt;span class="s1" style="color: green; font-weight: bold;"&gt;'p &lt;/span&gt;&lt;span class="s0"&gt;→ &lt;/span&gt;&lt;span class="s1" style="color: green; font-weight: bold;"&gt;'q&lt;/span&gt;&lt;span class="s0"&gt;) ∨ r . &lt;/span&gt;&lt;/span&gt; For that purpose, implicit comes to rescue:&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;implicit def &lt;/span&gt;&lt;span class="s1"&gt;symbolToAtom(sym:Symbol) = Atom(sym)&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;We can now define how the validity and satisfiability work. This code illustrates the example of the application for testing our program:&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;object &lt;/span&gt;&lt;span class="s0"&gt;Run { &lt;br /&gt;&lt;/span&gt;    &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s0"&gt;main(args:Array[String]):Unit = { &lt;br /&gt;&lt;/span&gt;      &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;import &lt;/span&gt;&lt;span class="s0"&gt;Formulas._ &lt;br /&gt;&lt;/span&gt;      &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;val &lt;/span&gt;&lt;span class="s0"&gt;formula1 = (¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'r&lt;/span&gt;&lt;span class="s0"&gt;) ∧ &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'q&lt;/span&gt;&lt;span class="s0"&gt;) ∧ (¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'p &lt;/span&gt;&lt;span class="s0"&gt;∧ &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'q&lt;/span&gt;&lt;span class="s0"&gt;) ↔ (¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'p&lt;/span&gt;&lt;span class="s0"&gt;) ∨ ¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'q&lt;/span&gt;&lt;span class="s0"&gt;) )) &lt;br /&gt;&lt;/span&gt;      println(isSatisfiable(formula1) + &lt;span class="s2" style="color: green; font-weight: bold;"&gt;"," &lt;/span&gt;&lt;span class="s0"&gt;+ isValid(formula1)) &lt;/span&gt;&lt;span class="s3" style="color: grey; font-style: italic;"&gt;// true, false&lt;/span&gt;&lt;span class="s0"&gt; &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;val &lt;/span&gt;&lt;span class="s0"&gt;formula2 = ¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'p &lt;/span&gt;&lt;span class="s0"&gt;∧ &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'q&lt;/span&gt;&lt;span class="s0"&gt;) ↔ (¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'p&lt;/span&gt;&lt;span class="s0"&gt;) ∨ ¬(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;'q&lt;/span&gt;&lt;span class="s0"&gt;) ) &lt;br /&gt;&lt;/span&gt;      println(isSatisfiable(formula2) + &lt;span class="s2" style="color: green; font-weight: bold;"&gt;"," &lt;/span&gt;&lt;span class="s0"&gt;+ isValid(formula2)) &lt;/span&gt;&lt;span class="s3" style="color: grey; font-style: italic;"&gt;// true, true&lt;/span&gt;&lt;span class="s0"&gt; &lt;br /&gt;&lt;/span&gt;    } &lt;br /&gt;  }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now let's see the implementation of the 9 rules (you remember 5&amp;nbsp;α&amp;nbsp;rules, and 4&amp;nbsp;&lt;span class="Apple-style-span" style="font-size: 12px;"&gt;β&amp;nbsp;&lt;/span&gt;rules ?). It's quite simple actually. An application of rule actually retuns a list of leaf, and a leaf is actually a list of formula. Here it is:&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;  type &lt;/span&gt;&lt;span class="s1"&gt;Leaf = Set[Formula] &lt;br /&gt; &lt;br /&gt;  &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;applyRule(f:Formula):List[Leaf] = &lt;br /&gt;&lt;/span&gt;    f &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;match &lt;/span&gt;&lt;span class="s1"&gt;{ &lt;br /&gt;&lt;/span&gt;      &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;f &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;if &lt;/span&gt;&lt;span class="s1"&gt;isLiteral(f) =&amp;gt; List(Set(f)) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;¬(¬(a)) =&amp;gt; List(Set(a)) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;Conjunction(a,b) =&amp;gt; List(Set(a,b)) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;¬(Disjunction(a,b)) =&amp;gt; List(Set(¬(a), ¬(b))) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;¬(Implication(a,b)) =&amp;gt; List(Set(a, ¬(b))) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;Disjunction(a,b) =&amp;gt; List(Set(a), Set(b)) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;¬(Conjunction(a,b)) =&amp;gt; List(Set(¬(a)), Set(¬(b))) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;(Implication(a,b)) =&amp;gt; List(Set(¬(a)), Set(b)) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;Equivalence(a,b) =&amp;gt; List( Set(a,b) , Set(¬(a), ¬(b))) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;¬(Equivalence(a,b)) =&amp;gt; List(Set(a,¬(b)),Set(¬(a), b)) &lt;br /&gt;&lt;/span&gt;  }&lt;/pre&gt;&lt;br /&gt;Nothing should be surprising except the&amp;nbsp;&lt;span class="Apple-style-span" style="background-color: white; font-family: monospace; white-space: pre;"&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;f &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;if &lt;/span&gt;&lt;span class="s1"&gt;isLiteral(f) =&amp;gt; List(Set(f)). &lt;/span&gt;&lt;/span&gt;&amp;nbsp;Just disregard this line at the moment, it's only useful in semantic tableau generation.&amp;nbsp;Another surprise maybe for the equivalence rules. &amp;nbsp;The two equivalence rules in the code above are actually equivalent to the one in the rule table (leave as an exercise :-) ). Note how close the rules definition to its coding implementation. Isn't it nice ?&lt;br /&gt;&lt;br /&gt;The most interesting part is of course the implementation of semantic tableau generation. In this naive implementation, the semantic tableau generation is embarrassingly simple, barely 10 lines of codes.&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s0"&gt;    &lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s0"&gt;semanticTableau(f:Formula):List[Leaf] = { &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s0"&gt;combine(rec:List[Leaf], f:Formula):List[Leaf] = &lt;br /&gt;&lt;/span&gt;               &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;for &lt;/span&gt;&lt;span class="s0"&gt;( a &amp;lt;- applyRule(f); b &amp;lt;- rec) &lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;yield &lt;/span&gt;&lt;span class="s0"&gt;(a ++ b) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s0"&gt;openLeaf(leaf:Leaf):List[Leaf] = &lt;br /&gt;&lt;/span&gt;        &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;if &lt;/span&gt;&lt;span class="s0"&gt;(leaf forall isLiteral) &lt;br /&gt;&lt;/span&gt;          List(leaf) &lt;br /&gt;        &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;else&lt;/span&gt;&lt;span class="s0"&gt; &lt;br /&gt;&lt;/span&gt;          leaf.foldLeft(List(Set.empty:Leaf))(combine) flatMap(openLeaf) &lt;br /&gt; &lt;br /&gt;      openLeaf(Set(f)) &lt;br /&gt;    }&lt;/pre&gt;&lt;br /&gt;Although short, the code is actually very dense. As you can see, the generation of semantic tableau above uses an auxiliary method openLeaf that does exactly that: opening a leaf. Opening a leaf is a recursive function that stops when all formulas in the leaf is a literal. A literal is either an atom or a negation of an atom. The else part is much more complex. But here is the idea: for each formula in a leaf, we apply one of the 9 rules defined above. The result of the application is combined using the combine function above. This will end up with a list of Leaf. For each leaf, then we recursively calls openLeaf using flatMap. This may be unclear, but try to play with the codes, hopefully it'll be clearer.&lt;br /&gt;&lt;br /&gt;Finally, we need to implement validity and satisfiability check. A formula is f said to be valid if the semantic tableau for&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;¬&lt;/span&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;f is closed. A semantic tableau is closed when all its leaves are closed, and finally a leaf is closed when it contains a formula in form of p and &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;¬&lt;/span&gt;p. Here is the implementation:&lt;br /&gt;&lt;pre&gt;&lt;span class="s0"&gt;    &lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s0"&gt;isClosedLeaf(f:Leaf):Boolean = &lt;br /&gt;&lt;/span&gt;      &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;if &lt;/span&gt;&lt;span class="s0"&gt;(f.isEmpty) &lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;false&lt;/span&gt;&lt;span class="s0"&gt; &lt;br /&gt;&lt;/span&gt;      &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;else &lt;/span&gt;&lt;span class="s0"&gt;{ &lt;br /&gt;&lt;/span&gt;        (f.head &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;match &lt;/span&gt;&lt;span class="s0"&gt;{ &lt;br /&gt;&lt;/span&gt;           &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s0"&gt;Atom(_)    =&amp;gt; f.tail.exists( _ == ¬(f.head)) &lt;br /&gt;&lt;/span&gt;           &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s0"&gt;¬(Atom(a)) =&amp;gt; f.tail.exists( _ == Atom(a)) &lt;br /&gt;&lt;/span&gt;           &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s0"&gt;_ =&amp;gt; &lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;false&lt;/span&gt;&lt;span class="s0"&gt; &lt;br /&gt;&lt;/span&gt;         })  || isClosedLeaf(f.tail) &lt;br /&gt;      } &lt;br /&gt; &lt;br /&gt;    &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s0"&gt;isOpenLeaf(f:Leaf) = !isClosedLeaf(f) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;    &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s0"&gt;isValid(f:Formula):Boolean = semanticTableau(¬(f)) forall isClosedLeaf &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;    &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s0"&gt;isSatisfiable(f:Formula):Boolean = semanticTableau(f) exists isOpenLeaf&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;We're done.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Summary&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;In this post, I showed my weekend hacking to implement naive satisfiability and validity checking of a propositional formula using semantic tableau. The implementation uses intensively Scala concepts like operator overriding, implicit, and fold and flatMap. All this concepts are helpful to implement the semantic tableaux techniques in relatively short Scala codes (less than 90 lines). &amp;nbsp;The use of operator helps readibility of the implementation (imagine how it looks like if I didn't use operators&amp;nbsp;∧,∨, →,&amp;nbsp;↔, and&amp;nbsp;⊕).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Reference&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;span style="font-size: small;"&gt;Ben-Ari, M (1992) &lt;i&gt;Mathematical Logic for Computer Science&lt;/i&gt;. Prentice Hall.&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2012573949883447809-6501597842764428230?l=voidmainargs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/6501597842764428230/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2012573949883447809&amp;postID=6501597842764428230&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/6501597842764428230'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/6501597842764428230'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/2011/09/semantic-tableaux-in-less-than-90-lines.html' title='Semantic Tableaux in Less than 90 Lines of Scala'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-rMIsJBpAVQk/ToAOwY1_PbI/AAAAAAAAADs/4ICfUYZA65c/s72-c/semantictableau.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-301554014520332924</id><published>2011-09-06T00:21:00.003+02:00</published><updated>2012-01-19T07:15:03.667+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='fold'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Fix from Fold</title><content type='html'>My two previous posts, &lt;a href="http://voidmainargs.blogspot.com/2011/08/fold-again-fold-left-using-fold-right.html"&gt;Fold Right from Fold Left&lt;/a&gt;&amp;nbsp;and &lt;a href="http://voidmainargs.blogspot.com/2011/08/folding-stream-with-scala.html"&gt;Folding Stream with Scala&lt;/a&gt;&amp;nbsp;are actually my interpretation to (Hutton, 1999) paper. Now, I would like to continue with another paper (Pope, 2010 ?) that also talks about fold. Note that, Pope's work cites of &amp;nbsp;Hutton's work.&lt;br /&gt;&lt;br /&gt;This post ends my fold trilogy. It has been an exciting and fun to play with. &amp;nbsp;I would love to continue with scalaz Fold (Foldr, Foldl, FoldMap Foldable are interesting), but I think I have to stop having fun :-)&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: #999999; font-size: large;"&gt;Yet Another dropWhile Implementation&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In&amp;nbsp;&lt;a href="http://voidmainargs.blogspot.com/2011/08/folding-stream-with-scala.html"&gt;Folding Stream with Scala&lt;/a&gt;&amp;nbsp;, I implemented dropWhile using fold using (Hutton, 1999) paper. Pope proposes two more implementations, both work very well with infinite stream.&lt;br /&gt;&lt;br /&gt;First, a reminder of fold implementation:&lt;br /&gt;&lt;pre&gt;&lt;span class="s2" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;foldr[A, B](combine:(A, =&amp;gt;B) =&amp;gt; B, base:B)(xs:Stream[A]): B = { &lt;br /&gt;    &lt;span class="s2" style="color: navy; font-weight: bold;"&gt;if &lt;/span&gt;&lt;span class="s0"&gt;(xs.isEmpty) base &lt;br /&gt;&lt;/span&gt;    &lt;span class="s2" style="color: navy; font-weight: bold;"&gt;else &lt;/span&gt;&lt;span class="s0"&gt;combine(xs.head, foldr(combine, base)(xs.tail)) &lt;br /&gt;&lt;/span&gt;  } &lt;/pre&gt;&lt;br /&gt;And here is the solution:&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;span class="s0"&gt;  &lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s0"&gt;dwHo[A](pred:A=&amp;gt;Boolean, xs:Stream[A]):Stream[A]=&amp;gt;Stream[A] = { &lt;br /&gt;&lt;/span&gt;    &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;val &lt;/span&gt;&lt;span class="s0"&gt;id =(s:Stream[A])=&amp;gt;s &lt;br /&gt;&lt;/span&gt;    &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;val &lt;/span&gt;&lt;span class="s0"&gt;tail= (s:Stream[A])=&amp;gt;s.tail &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;    &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s0"&gt;combine(next:A, rec: =&amp;gt;Stream[A]=&amp;gt;Stream[A]) = { &lt;br /&gt;&lt;/span&gt;     &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;if &lt;/span&gt;&lt;span class="s0"&gt;(pred(next)) (rec compose tail) &lt;br /&gt;&lt;/span&gt;     &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;else &lt;/span&gt;&lt;span class="s0"&gt;id &lt;br /&gt;&lt;/span&gt;    } &lt;br /&gt;    foldr(combine, id)(xs) &lt;br /&gt;  }&lt;/span&gt;&lt;/pre&gt;Example:&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s0"&gt;  scala&amp;gt; &lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;val &lt;/span&gt;&lt;span class="s0"&gt;xs = Stream.range(&lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;20&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;120&lt;/span&gt;&lt;span class="s0"&gt;) &lt;br /&gt;&lt;/span&gt;  xs: scala.collection.immutable.Stream[Int] = Stream(&lt;span class="s2" style="color: blue;"&gt;20&lt;/span&gt;&lt;span class="s0"&gt;, ?) &lt;br /&gt;&lt;/span&gt;  scala&amp;gt; dwHo( (_:Int)&amp;lt;&lt;span class="s2" style="color: blue;"&gt;100&lt;/span&gt;&lt;span class="s0"&gt;, xs)(xs).toList &lt;br /&gt;&lt;/span&gt;  res33: List[Int] = List(&lt;span class="s2" style="color: blue;"&gt;100&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;101&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;102&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;103&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;104&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;105&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;106&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;107&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;108&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;109&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;110&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;111&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;112&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;113&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;114&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;1&lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;15&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;116&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;117&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;118&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;119&lt;/span&gt;&lt;span class="s0"&gt;) &lt;/span&gt;&lt;/pre&gt;It also works with infinite Stream:&lt;span class="Apple-style-span" style="background-color: white; font-family: monospace; white-space: pre;"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=301554014520332924" name="l37"&gt; &lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;span class="s0"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=301554014520332924" name="l38"&gt;scala&amp;gt; &lt;/a&gt;&lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;val &lt;/span&gt;&lt;span class="s0"&gt;ys = Stream.from(&lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;1&lt;/span&gt;&lt;span class="s0"&gt;) &lt;br /&gt;&lt;/span&gt;ys: scala.collection.immutable.Stream[Int] = Stream(&lt;span class="s2" style="color: blue;"&gt;1&lt;/span&gt;&lt;span class="s0"&gt;, ?) &lt;br /&gt;&lt;/span&gt;scala&amp;gt; dwHo( (_:Int)&amp;lt;&lt;span class="s2" style="color: blue;"&gt;5&lt;/span&gt;&lt;span class="s0"&gt;, ys)(ys).take(&lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;10&lt;/span&gt;&lt;span class="s0"&gt;).toList &lt;br /&gt;&lt;/span&gt;res38: List[Int] = List(&lt;span class="s2" style="color: blue;"&gt;5&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;6&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;7&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;8&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;9&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;10&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;11&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;12&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;13&lt;/span&gt;&lt;span class="s0"&gt;, &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;14&lt;/span&gt;&lt;span class="s0"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;Under the hood, here is what happen:&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s0"&gt;foldr combine id [&lt;/span&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=301554014520332924" name="l44"&gt;&lt;span class="s1" style="color: blue;"&gt;1.&lt;/span&gt;&lt;span class="s0"&gt;.] &lt;br /&gt;&lt;/span&gt;&lt;/a&gt; =combine &lt;span class="s1" style="color: blue;"&gt;1 &lt;/span&gt;&lt;span class="s0"&gt;(foldr combine id [&lt;/span&gt;&lt;span class="s1" style="color: blue;"&gt;2.&lt;/span&gt;&lt;span class="s0"&gt;.]) = &lt;br /&gt;&lt;/span&gt; =(foldr combine id [&lt;span class="s1" style="color: blue;"&gt;2.&lt;/span&gt;&lt;span class="s0"&gt;.]) . tail &lt;br /&gt;&lt;/span&gt; =(combine &lt;span class="s1" style="color: blue;"&gt;2 &lt;/span&gt;&lt;span class="s0"&gt;(foldr combine id [&lt;/span&gt;&lt;span class="s1" style="color: blue;"&gt;3.&lt;/span&gt;&lt;span class="s0"&gt;.]) . tail &lt;br /&gt;&lt;/span&gt; =(foldr combine id [&lt;span class="s1" style="color: blue;"&gt;3.&lt;/span&gt;&lt;span class="s0"&gt;.]) .tail . tail &lt;br /&gt;&lt;/span&gt; =(combine &lt;span class="s1" style="color: blue;"&gt;3 &lt;/span&gt;&lt;span class="s0"&gt;(foldr combine id [&lt;/span&gt;&lt;span class="s1" style="color: blue;"&gt;4.&lt;/span&gt;&lt;span class="s0"&gt;.]) . tail . tail &lt;br /&gt;&lt;/span&gt; =(foldr combine id [&lt;span class="s1" style="color: blue;"&gt;4.&lt;/span&gt;&lt;span class="s0"&gt;.]) .tail . tail . tail &lt;br /&gt;&lt;/span&gt; =(combine &lt;span class="s1" style="color: blue;"&gt;4 &lt;/span&gt;&lt;span class="s0"&gt;(foldr combine id [&lt;/span&gt;&lt;span class="s1" style="color: blue;"&gt;5.&lt;/span&gt;&lt;span class="s0"&gt;.]) . tail . tail . tail &lt;br /&gt;&lt;/span&gt; =(foldr combine id [&lt;span class="s1" style="color: blue;"&gt;5.&lt;/span&gt;&lt;span class="s0"&gt;.]) . tail . tail . tail . tail &lt;br /&gt;&lt;/span&gt; =(combine &lt;span class="s1" style="color: blue;"&gt;5 &lt;/span&gt;&lt;span class="s0"&gt;(foldr combine id [&lt;/span&gt;&lt;span class="s1" style="color: blue;"&gt;6.&lt;/span&gt;&lt;span class="s0"&gt;.]) . tail . tail . tail &lt;br /&gt;&lt;/span&gt; =id . tail . tail . tail . tail&lt;/pre&gt;And (id.tail.tail.tail.tail)[1..] = [5..].&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: #999999; font-size: large;"&gt;Fix from Fold&lt;/span&gt;&lt;br /&gt;The most interesting part of the (Pope, 2010) is not on dropWhile though, but on an implementation of a function using fold. The function is called &lt;i&gt;fix&lt;/i&gt;, also known as Y combinator. Check this page to have an idea of what fix function is.&lt;br /&gt;&lt;br /&gt;Basically, using fix, we can encode recursion function. The following is an example of factorial function using fix in haskell:&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;span class="s0"&gt;Prelude&amp;gt; :m Control.Monad.Fix &lt;br /&gt;Prelude Control.Monad.Fix&amp;gt; fix (\rec n -&amp;gt; &lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;if &lt;/span&gt;&lt;span class="s0"&gt;n == &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;0 &lt;/span&gt;&lt;span class="s0"&gt;then &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;1 &lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;else &lt;/span&gt;&lt;span class="s0"&gt;n * rec (n-&lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;1&lt;/span&gt;&lt;span class="s0"&gt;)) &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;5&lt;/span&gt;&lt;span class="s0"&gt; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: monospace; white-space: pre;"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=301554014520332924" name="l51"&gt;&lt;span class="s2" style="color: blue;"&gt;120&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;In Scala, fix function is implemented in a much trickier way. The difficulties come from the Scala strictness. Here is the implementation of fix I found after googling a little bit: &lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;fix[A](f: (A=&amp;gt;A)=&amp;gt;(A=&amp;gt;A)): A=&amp;gt;A = f(fix(f))(_)&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="s1"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;Example:&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="s0"&gt;scala&amp;gt; fix[Long](f=&amp;gt;x=&amp;gt; &lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;if &lt;/span&gt;&lt;span class="s0"&gt;(x == &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;0&lt;/span&gt;&lt;span class="s0"&gt;) &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;1 &lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;else &lt;/span&gt;&lt;span class="s0"&gt;x * f(x - &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;1&lt;/span&gt;&lt;span class="s0"&gt;))(&lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;5&lt;/span&gt;&lt;span class="s0"&gt;) &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s0"&gt;res5: Long = &lt;/span&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=301554014520332924" name="l67"&gt;&lt;span class="s2" style="color: blue;"&gt;120&lt;/span&gt;&lt;/a&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;One question that may arise is whether fold can be implemented using fix. Well, apparently, yes, after all, it's a recursion, but I haven't tried it yet. A more interesting question would be if fix can be implemented using fold. The answer is yes, and that's the essence of (Pope, 2010) article. Here is the implementation of fix using foldr in Scala:&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s0"&gt;fix[A](f: (A=&amp;gt;A)=&amp;gt;(A=&amp;gt;A)): A=&amp;gt;A = { &lt;br /&gt;&lt;/span&gt;  &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s0"&gt;combine( a:(A=&amp;gt;A)=&amp;gt;(A=&amp;gt;A), b: =&amp;gt;A=&amp;gt;A):A=&amp;gt;A = f(b)(_) &lt;br /&gt;&lt;/span&gt;  foldr(combine, &lt;span class="s1" style="color: navy; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span class="s0"&gt;)(Stream.continually(&lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span class="s0"&gt;)) &lt;br /&gt;&lt;/span&gt;}&lt;/pre&gt;&lt;pre&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=301554014520332924" name="l73"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/pre&gt;&lt;br /&gt;Give a try:&lt;span class="Apple-style-span" style="background-color: white; font-family: monospace; white-space: pre;"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=301554014520332924" name="l66"&gt; &lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;span class="s0"&gt;scala&amp;gt; fix[Long](f=&amp;gt;x=&amp;gt; &lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;if &lt;/span&gt;&lt;span class="s0"&gt;(x == &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;0&lt;/span&gt;&lt;span class="s0"&gt;) &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;1 &lt;/span&gt;&lt;span class="s1" style="color: navy; font-weight: bold;"&gt;else &lt;/span&gt;&lt;span class="s0"&gt;x * f(x - &lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;1&lt;/span&gt;&lt;span class="s0"&gt;))(&lt;/span&gt;&lt;span class="s2" style="color: blue;"&gt;5&lt;/span&gt;&lt;span class="s0"&gt;) &lt;br /&gt;&lt;/span&gt;res6: Long = &lt;span class="s2" style="color: blue;"&gt;120&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Youpi..., isn't it awesome? This shows how expressive fold is, since it can now be used to implement (any?) recursion functions.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;References&lt;/b&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;ul style="line-height: 1.4; margin-bottom: 0.5em; margin-left: 0px; margin-right: 0px; margin-top: 0.5em; padding-bottom: 0px; padding-left: 2.5em; padding-right: 2.5em; padding-top: 0px;"&gt;&lt;li style="margin-bottom: 0.25em; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-indent: 0px;"&gt;Hutton G (1999) A tutorial on the universality and expressiveness of fold. Available from&amp;nbsp;&lt;a href="http://www.cs.nott.ac.uk/~gmh/fold.pdf" style="color: #2288bb; text-decoration: none;"&gt;http://www.cs.nott.ac.uk/~gmh/fold.pdf&lt;/a&gt;&amp;nbsp;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style="line-height: 1.4; margin-bottom: 0.5em; margin-left: 0px; margin-right: 0px; margin-top: 0.5em; padding-bottom: 0px; padding-left: 2.5em; padding-right: 2.5em; padding-top: 0px;"&gt;&lt;li style="margin-bottom: 0.25em; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-indent: 0px;"&gt;Pope B (2010 ?) &amp;nbsp;Getting a Fix from the Right Fold. Available from:&amp;nbsp;&lt;a href="http://www.haskell.org/wikiupload/1/14/TMR-Issue6.pdf" style="color: #2288bb; text-decoration: none;"&gt;http://www.haskell.org/wikiupload/1/14/TMR-Issue6.pdf&lt;/a&gt;&amp;nbsp;. There is one other dropWhile solution in this paper that I dumped here:&amp;nbsp;&lt;a href="https://gist.github.com/1195982"&gt;https://gist.github.com/1195982&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2012573949883447809-301554014520332924?l=voidmainargs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/301554014520332924/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2012573949883447809&amp;postID=301554014520332924&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/301554014520332924'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/301554014520332924'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/2011/09/fix-from-fold.html' title='Fix from Fold'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-1392708263190079957</id><published>2011-08-29T13:24:00.002+02:00</published><updated>2012-01-19T07:15:03.640+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='fold'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Fold Again: Fold Left using Fold Right</title><content type='html'>In my &lt;a href="http://voidmainargs.blogspot.com/2011/08/folding-stream-with-scala.html"&gt;previous post&lt;/a&gt;, I have shown how fold can be used to implement other collection methods, like filter, map, length, reverse, or even dropWhile and break. &lt;br /&gt;&lt;br /&gt;We wonder now whether it is possible to implement fold left using fold right. The article (Hutton, 1999) shows indeed that it is possible. The objective of this post is then to show it in Scala and discuss a little bit about fold universality.&lt;br /&gt;&lt;br /&gt;Before implementing fold left using fold right, let's implement something simpler. Let's implement suml, a function that is similar to sum we discussed in the &lt;a href="http://voidmainargs.blogspot.com/2011/08/folding-stream-with-scala.html"&gt;previous post&lt;/a&gt;. Instead of summing from right to left, we want suml to sum from left to right. &lt;br /&gt;&lt;br /&gt;The following illustrates the difference between sum and suml:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;sum&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;, &lt;span style="color: red;"&gt;2&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;, &lt;span style="color: red;"&gt;4&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;1&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;+&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;+&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;3&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;+&lt;/b&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;suml&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;, &lt;span style="color: red;"&gt;2&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;, &lt;span style="color: red;"&gt;4&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;+&lt;/b&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;+&lt;/b&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;3&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;+&lt;/b&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;As described in (Hutton, 1999), it turns out that we can't directly implement suml using fold. What possible is to define suml_ that returns a function Int=&amp;gt; Int. Here is the implementation:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: scroll; background-color: #dbdbdb; background-image: none; background-position: 0% 0%; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;suml_&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs:&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: scroll; background-color: #dbdbdb; background-image: none; background-position: 0% 0%; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x:&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;, g: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;acc:&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;g&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;acc &lt;span style="color: black;"&gt;&lt;b&gt;+&lt;/b&gt;&lt;/span&gt;x&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: scroll; background-color: #dbdbdb; background-image: none; background-position: 0% 0%; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x:&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;x&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: scroll; background-color: #dbdbdb; background-image: none; background-position: 0% 0%; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: scroll; background-color: #dbdbdb; background-image: none; background-position: 0% 0%; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   5 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;sum&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs:&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;suml_&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;When suml_(Stream(1, 3, 4, 5)) is called, it returns actually  &lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;((_:Int) + 5) compose ((_:Int) + 4)&amp;nbsp;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;compose ((_:Int) + 3) compose ((_:Int)+ 1)&amp;nbsp;&lt;/div&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;compose ( (x:Int)=&amp;gt;x)&lt;/div&gt;Note that, the returned function is actually an Int=&amp;gt;Int function, and when it receives 0 as its input, it returns (((1 + 3) + 4) + 5) = 13. Also note a special function, the identity function (x:Int)=&amp;gt;x.&lt;br /&gt;&lt;br /&gt;OK, Great. What about foldl? Well, it is the generalization of suml above. Here is the foldl implementation:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; foldl_&lt;span style="color: #9900cc;"&gt;[A,B]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;f: &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;A,B&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;B, xs:&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x: A, g: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;B&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;B&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;acc:B&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;g&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;f&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x,acc&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;a:B&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;a&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   5 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; foldl&lt;span style="color: #9900cc;"&gt;[A,B]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;f:&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;A,B&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;B, base:B, xs:&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;foldl_&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;f,xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;base&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   6 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Let's give a shot:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;scala&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;foldl&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;_:&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;+&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;_:&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: red;"&gt;0&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;, &lt;span style="color: red;"&gt;4&lt;/span&gt;, &lt;span style="color: red;"&gt;5&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;res1: &lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;13&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;scala&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;foldl&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;_:&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;*&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;_:&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: red;"&gt;1&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;, &lt;span style="color: red;"&gt;4&lt;/span&gt;, &lt;span style="color: red;"&gt;5&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   5 &lt;/span&gt;&lt;/span&gt;res2: &lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;60&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Excellent. But, all this looks like a magic, right? Is there a systematic way to derive a implementation of a function using fold? Fortunately, yes. Here we come to the most interesting part of (Hutton, 1999).&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Fold Universality and &amp;nbsp;Fusion Property of Fold&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Two &amp;nbsp;important concepts explained in (Hutton, 1999) is fold universality and fusion property of fold.&lt;br /&gt;&lt;br /&gt;The fold universality states that the two Scala code below are equivalent:&lt;br /&gt;&lt;b&gt;Code 1&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; g&lt;span style="color: #9900cc;"&gt;[A,B]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;, f:&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;A, B&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;B, v:B&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;: B &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;  &lt;span style="color: #006699;"&gt;&lt;b&gt;if&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs.isEmpty&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; v &lt;span style="color: #006699;"&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;  &lt;span style="color: #9966ff;"&gt;f&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs.head, &lt;span style="color: #9966ff;"&gt;g&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs.tail, f, v&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Code 2&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; g&lt;span style="color: #9900cc;"&gt;[A,B]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs:&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;, f:&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;A,B&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;B, v:B&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;f,v&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Or more concise, in haskell symbols:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;g[]     &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; v&lt;br /&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;                      &lt;span style="color: black;"&gt;&lt;b&gt;&amp;lt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;   g &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; fold f v&lt;br /&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;g(x:xs) &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; f x (g xs)    &lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;And the fusion property of fold states the following:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;1 &lt;/span&gt;&lt;/span&gt;h w &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; v&lt;br /&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;2 &lt;/span&gt;&lt;/span&gt;                         &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; h . fold g w &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; fold f v&lt;br /&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;3 &lt;/span&gt;&lt;/span&gt;h(g x y) &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; f x (h y)&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Let's have examples.&lt;br /&gt;&lt;br /&gt;First, let's see how universal property of fold is useful to derive an implementation of a function using fold.&lt;br /&gt;&lt;br /&gt;We will start with the recursive definition of foldl:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;1 &lt;/span&gt;&lt;/span&gt;foldl     &lt;span style="color: #9900cc;"&gt;[]&lt;/span&gt; f v  &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; v&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;2 &lt;/span&gt;&lt;/span&gt;foldl &lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;x:xs&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; f v  &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; foldl xs f &lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;f v x&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;In scala:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt; foldl&lt;span style="color: #9900cc;"&gt;[A,B]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;xs:&lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;, f:&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;B,A&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt;B, v:B&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;: B &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;2 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;strong&gt;if&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;xs.isEmpty&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; v&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;3 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;strong&gt;else&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;foldl&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;xs.tail, f, &lt;span style="color: #9966ff;"&gt;f&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;v, xs.head&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;4 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Unfortunately, foldl does not match directly with universal property definition. We need then an auxiliary method foldl_&amp;nbsp;:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt; foldl_&lt;span style="color: #9900cc;"&gt;[A,B]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;xs:&lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; foldl&lt;span style="color: #9900cc;"&gt;[A,B]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;xs:&lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;, &lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;_:&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;B,A&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt;B&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;, &lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;_:B&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;That is, foldl function without the last two parameters.&lt;br /&gt;&lt;br /&gt;We're ready to use universal property now:&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;foldl_ []     = v&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;foldl_ (x:xs) = f x (foldl_ xs)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here, v is the identity function =id.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;{Functions}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;foldl_ (x:xs) g a = f x (foldl_ xs) g a&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;lt;=&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;{Definition of foldl}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;foldl_ xs g (g a x) = f x (foldl_ xs) g a&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;lt;=&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;{Generalizing foldl_ xs = h}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;h g (g a x) = f x h g a&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;lt;=&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;f = (λx h -&amp;gt; (λa -&amp;gt; h(g a x)))&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So, we got:&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;foldl xs f v = foldr((λx h -&amp;gt; (λa -&amp;gt; h(g a x)))) id xs v&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;When translated to Scala, we obtain the code explained at the beginning of the post.&lt;br /&gt;&lt;br /&gt;***&lt;br /&gt;&lt;br /&gt;Now, go for fusion property. Recall our map definition defined in the previous post (I modified a little bit):&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; map&lt;span style="color: #9900cc;"&gt;[A,B]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;f1:A&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;B&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x:A, xs:&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[B]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;f&lt;span style="color: black;"&gt;&lt;b&gt;1&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; #:: xs&lt;br /&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.empty&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: scroll; background-clip: initial; background-color: #dbdbdb; background-image: none; background-origin: initial; background-position: 0% 0%; background-repeat: repeat repeat; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;span class="Apple-style-span"&gt;Note that the combine function can be represented as&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;λx xs-&amp;gt;f1 x : xs .&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We would like to (a little bit informally) prove that &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;map(f1) compose map(f2) &lt;/span&gt; = &amp;nbsp;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;map(f1 compose f2)&lt;/span&gt;.&lt;br /&gt;From the equation, we can substitute:&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;h = map(f1)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;g =&amp;nbsp;λx xs-&amp;gt;f2(x):xs&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;w = v = Stream.empty = []&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;f = f1 compose f2&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;But,&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;map(f1) [] = []&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;map(f1) (g(x,xs)) = map(f1) ( f2(x):xs )&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; = (f1 compose f2)(x):map(f1)(xs)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So, the equation&amp;nbsp;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;map(f1) compose map(f2)&amp;nbsp;&lt;/span&gt;= &amp;nbsp;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;map(f1 compose f2)&lt;/span&gt;indeed holds.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2012573949883447809-1392708263190079957?l=voidmainargs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/1392708263190079957/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2012573949883447809&amp;postID=1392708263190079957&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/1392708263190079957'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/1392708263190079957'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/2011/08/fold-again-fold-left-using-fold-right.html' title='Fold Again: Fold Left using Fold Right'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-2608266554810592218</id><published>2011-08-15T23:48:00.005+02:00</published><updated>2012-01-19T07:15:03.659+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='haskell'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Folding Stream with Scala</title><content type='html'>&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Introduction&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;Couple of weeks ago, I read a well known paper on expressiveness of fold (Hutton, 1999). The paper is a very interesting paper that explains how fold is a very powerful construct. &lt;br /&gt;&lt;br /&gt;The codes in the paper are written in Haskell and therefore assumes laziness. This means that the execution of function in Haskell is delayed until it is really needed. This is not the case for Scala, where the operation is executed eagerly, or in other word, it's strict instead of lazy.&lt;br /&gt;&lt;br /&gt;Stream is the implementation of lazy list in Scala. Using Stream, the elements are only evaluated when they are needed. So, we may expect that Stream is close to Haskell's list.&lt;br /&gt;&lt;br /&gt;The use of scala Stream and fold right are the topic of this post.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Fold Right&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;Let's back to fold. The paper I mentioned above (Hutton, 1999) showed how expressive fold construct, especially foldRight. The objective of this post is to show the same things as the paper, but in Scala. &lt;br /&gt;&lt;br /&gt;Let's see the problem we may encounter. The following Haskell code works:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;Prelude&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; foldr &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;|&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;|&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;b&gt;False &lt;/b&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;repeat True&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;True&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;But not the following Scala code:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;continually&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #cc00cc;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;foldRight&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #cc00cc;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;_ &lt;span style="color: black;"&gt;&lt;b&gt;|&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;|&lt;/b&gt;&lt;/span&gt; _&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;because the code above throws StackOverflowError.&lt;br /&gt;&lt;br /&gt;The reason of the error is because Stream.foldRight is not actually lazy. When I read the article and wondered how the non-laziness of Stream.foldRight could be solved, I received Tony Morris' tweet telling that scalaz fixes the non-laziness. The following is the reimplementation of lazy foldRight for Stream inspired by Foldr scalaz implementation:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;1 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;2 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; foldr&lt;span style="color: #9900cc;"&gt;[A,&lt;/span&gt;&lt;span style="color: #9900cc;"&gt; &lt;/span&gt;&lt;span style="color: #9900cc;"&gt;B]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; combine: &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;A, &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;B&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; B, base: B &lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;: B &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;3 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;if&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs.isEmpty&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;4 &lt;/span&gt;&lt;/span&gt;     base&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;5 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;6 &lt;/span&gt;&lt;/span&gt;     &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs.head,  &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, base&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs.tail&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;7 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;The following code works without StackOverflowError:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x: &lt;span style="color: #009966;"&gt;&lt;b&gt;Boolean&lt;/b&gt;&lt;/span&gt;, y: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;b&gt;Boolean&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; x &lt;span style="color: black;"&gt;&lt;b&gt;|&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;|&lt;/b&gt;&lt;/span&gt; y&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, &lt;span style="color: #cc00cc;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;continually&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #cc00cc;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;  &lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;The key of the foldr implementation above is in thetype  definition of combine parameter: (A, =&amp;gt;B) instead of (A,B). This makes the second parameter evaluated lazily. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Some Fold Right Uses&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;The following shows some familiar functions implemented using foldr.&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;sum&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x: &lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;, y: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; x &lt;span style="color: black;"&gt;&lt;b&gt;+&lt;/b&gt;&lt;/span&gt; y&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, &lt;span style="color: red;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   5 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;product&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   6 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x: &lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;, y: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; x &lt;span style="color: black;"&gt;&lt;b&gt;*&lt;/b&gt;&lt;/span&gt; y&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   7 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, &lt;span style="color: red;"&gt;1&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   8 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   9 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;and&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Boolean]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  10 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x: &lt;span style="color: #009966;"&gt;&lt;b&gt;Boolean&lt;/b&gt;&lt;/span&gt;, y: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;b&gt;Boolean&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; x &lt;span style="color: black;"&gt;&lt;b&gt;&amp;amp;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;amp;&lt;/b&gt;&lt;/span&gt; y&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  11 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, &lt;span style="color: #cc00cc;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;  &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  12 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  13 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;or&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Boolean]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  14 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x: &lt;span style="color: #009966;"&gt;&lt;b&gt;Boolean&lt;/b&gt;&lt;/span&gt;, y: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;b&gt;Boolean&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; x &lt;span style="color: black;"&gt;&lt;b&gt;|&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;|&lt;/b&gt;&lt;/span&gt; y&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  15 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, &lt;span style="color: #cc00cc;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;  &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  16 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Using Fold Right to Implement Scala Collection Functions&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;As in (Hutton, 1999), the following section shows how Fold Right can be used to implement some functions. At the first time, let's see length, filter, reverse, flatten, and map implementations.&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; map&lt;span style="color: #9900cc;"&gt;[A,B]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;f:A&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;B, xs:&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x:A, xs: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[B]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;f&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;  #:: xs&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;val&lt;/b&gt;&lt;/span&gt; base:&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[B]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.empty&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, base&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   5 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   6 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; length&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   7 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x: A, len: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; len &lt;span style="color: black;"&gt;&lt;b&gt;+&lt;/b&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;1&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   8 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, &lt;span style="color: red;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   9 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  10 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; reverse&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  11 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x: A, xs: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;concat&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  12 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;b&gt;val&lt;/b&gt;&lt;/span&gt; base:&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.empty&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  13 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, base&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  14 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  15 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; filter&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;p:A&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;b&gt;Boolean&lt;/b&gt;&lt;/span&gt;, xs: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  16 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x: A, xs: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;b&gt;if&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; x #:: xs &lt;span style="color: #006699;"&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt; xs &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  17 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine , &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.empty&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  18 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  19 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; filterNot&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;p:A&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;b&gt;Boolean&lt;/b&gt;&lt;/span&gt;, xs: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  20 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x: A, xs: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;b&gt;if&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; xs &lt;span style="color: #006699;"&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt; x #:: xs &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  21 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;val&lt;/b&gt;&lt;/span&gt; base:&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.empty  &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  22 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, base&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  23 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  24 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; flatten&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xss: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Stream[A]]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  25 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;, ys: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; xs #::: ys&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  26 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;val&lt;/b&gt;&lt;/span&gt; base: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.empty&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  27 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, base&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xss&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  28 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  29 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; partition&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;p:A&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;b&gt;Boolean&lt;/b&gt;&lt;/span&gt;, xs:&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  30 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;filter&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;p, xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: #9966ff;"&gt;filterNot&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;p, xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  31 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  32 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Note that, the functions like flatten, filter, and map, work with infinite Streams. See the following examples:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;scala&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;map&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;_:&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;+&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;res2: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;2&lt;/span&gt;, ?&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;scala&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; res2.&lt;span style="color: #9966ff;"&gt;take&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;4&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;.toList&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   5 &lt;/span&gt;&lt;/span&gt;res3: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;List&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;List&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;2&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;, &lt;span style="color: red;"&gt;4&lt;/span&gt;, &lt;span style="color: red;"&gt;5&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   6 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   7 &lt;/span&gt;&lt;/span&gt;scala&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;filter&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;_:&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;%&lt;/b&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;0&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;take&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;5&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;.toList&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   8 &lt;/span&gt;&lt;/span&gt;res5: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;List&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;List&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;10&lt;/span&gt;, &lt;span style="color: red;"&gt;12&lt;/span&gt;, &lt;span style="color: red;"&gt;14&lt;/span&gt;, &lt;span style="color: red;"&gt;16&lt;/span&gt;, &lt;span style="color: red;"&gt;18&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   9 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  10 &lt;/span&gt;&lt;/span&gt;scala&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;partition&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;_:&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;%&lt;/b&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;0&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;20&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  11 &lt;/span&gt;&lt;/span&gt;res6: &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;scala.collection.immutable.&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;20&lt;/span&gt;, ?&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;,&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;21&lt;/span&gt;, ?&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  12 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  13 &lt;/span&gt;&lt;/span&gt;scala&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;  &lt;span style="color: #9966ff;"&gt;flatten&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;continually&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;, &lt;span style="color: red;"&gt;4&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;take&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;.toList&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  14 &lt;/span&gt;&lt;/span&gt;res13: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;List&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;List&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;, &lt;span style="color: red;"&gt;4&lt;/span&gt;, &lt;span style="color: red;"&gt;1&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;, &lt;span style="color: red;"&gt;4&lt;/span&gt;, &lt;span style="color: red;"&gt;1&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;, &lt;span style="color: red;"&gt;4&lt;/span&gt;, &lt;span style="color: red;"&gt;1&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;Implementing dropWhile and break&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;One of the most interesting part of (Hutton, 1999) is the dropWhile implementation using foldr. The implementation is shown in the following code:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; dropWhile&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;pred: A &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #009966;"&gt;&lt;b&gt;Boolean&lt;/b&gt;&lt;/span&gt;, xs: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x: A, xs: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;      &lt;span style="color: #006699;"&gt;&lt;b&gt;if&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;pred&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;         &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs._1, x #:: xs._2&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   5 &lt;/span&gt;&lt;/span&gt;      &lt;span style="color: #006699;"&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   6 &lt;/span&gt;&lt;/span&gt;         &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x #:: xs._2, x #:: xs._2&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   7 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;val&lt;/b&gt;&lt;/span&gt; base:&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.empty, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.empty&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   8 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, base&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; _1&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   9 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Instead of directly producing the result, a tuple of Streams is returned. The first Stream is the result and the second one is used for bookkeeping purpose.  &lt;br /&gt;The following illustrates an execution of dropWhile:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;dropWhile&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;_:&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;lt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;5&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;,&lt;span style="color: red;"&gt;2&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;3&lt;/span&gt;,&lt;span style="color: red;"&gt;4&lt;/span&gt;,&lt;span style="color: red;"&gt;5&lt;/span&gt;,&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;10&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;9&lt;/span&gt;  &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;8&lt;/span&gt;  &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   5 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;7&lt;/span&gt;  &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   6 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;6&lt;/span&gt;  &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   7 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;5&lt;/span&gt;  &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;5&lt;/span&gt;,&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;5&lt;/span&gt;,&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   8 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;4&lt;/span&gt;  &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;5&lt;/span&gt;,&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;4&lt;/span&gt;,&lt;span style="color: red;"&gt;5&lt;/span&gt;,&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   9 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;3&lt;/span&gt;  &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;5&lt;/span&gt;,&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;3&lt;/span&gt;,&lt;span style="color: red;"&gt;4&lt;/span&gt;,&lt;span style="color: red;"&gt;5&lt;/span&gt;,&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  10 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;7&lt;/span&gt;  &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;7&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;,&lt;span style="color: red;"&gt;4&lt;/span&gt;,&lt;span style="color: red;"&gt;5&lt;/span&gt;,&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;3&lt;/span&gt;,&lt;span style="color: red;"&gt;4&lt;/span&gt;,&lt;span style="color: red;"&gt;5&lt;/span&gt;,&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  11 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;2&lt;/span&gt;  &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;7&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;,&lt;span style="color: red;"&gt;4&lt;/span&gt;,&lt;span style="color: red;"&gt;5&lt;/span&gt;,&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;2&lt;/span&gt;, &lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;3&lt;/span&gt;,&lt;span style="color: red;"&gt;4&lt;/span&gt;,&lt;span style="color: red;"&gt;5&lt;/span&gt;,&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  12 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;  &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;7&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;,&lt;span style="color: red;"&gt;4&lt;/span&gt;,&lt;span style="color: red;"&gt;5&lt;/span&gt;,&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;, &lt;span style="color: red;"&gt;2&lt;/span&gt;, &lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;3&lt;/span&gt;,&lt;span style="color: red;"&gt;4&lt;/span&gt;,&lt;span style="color: red;"&gt;5&lt;/span&gt;,&lt;span style="color: red;"&gt;6&lt;/span&gt;,&lt;span style="color: red;"&gt;7&lt;/span&gt;,&lt;span style="color: red;"&gt;8&lt;/span&gt;,&lt;span style="color: red;"&gt;9&lt;/span&gt;,&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;What surprising is that the solution actually works for infinite Stream too as illustrated in the following code:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;scala&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;dropWhile&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;_:&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;lt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;5&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;take&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;.toList&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;res17: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;List&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;List&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;5&lt;/span&gt;, &lt;span style="color: red;"&gt;6&lt;/span&gt;, &lt;span style="color: red;"&gt;7&lt;/span&gt;, &lt;span style="color: red;"&gt;8&lt;/span&gt;, &lt;span style="color: red;"&gt;9&lt;/span&gt;, &lt;span style="color: red;"&gt;10&lt;/span&gt;, &lt;span style="color: red;"&gt;11&lt;/span&gt;, &lt;span style="color: red;"&gt;12&lt;/span&gt;, &lt;span style="color: red;"&gt;13&lt;/span&gt;, &lt;span style="color: red;"&gt;14&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;The last interesting function implemented here is break. The &amp;nbsp;equivalent of Haskell's break in Scala collection is called span (Update, Oct 24 thanks to oxbow). The break function creates a tuple of two lists separated at condition boundary. Example:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;scala&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;b&gt;val&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;a,b&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;break&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;_:&lt;span style="color: #009966;"&gt;&lt;b&gt;Int&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;lt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;5&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;a: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;, ?&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;b: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;5&lt;/span&gt;, ?&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   5 &lt;/span&gt;&lt;/span&gt;scala&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; a.toList&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   6 &lt;/span&gt;&lt;/span&gt;res22: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;List&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;List&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;, &lt;span style="color: red;"&gt;2&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;, &lt;span style="color: red;"&gt;4&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   7 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   8 &lt;/span&gt;&lt;/span&gt;scala&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; b.&lt;span style="color: #9966ff;"&gt;take&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;5&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;.toList&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   9 &lt;/span&gt;&lt;/span&gt;res23: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;List&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;List&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;5&lt;/span&gt;, &lt;span style="color: red;"&gt;6&lt;/span&gt;, &lt;span style="color: red;"&gt;7&lt;/span&gt;, &lt;span style="color: red;"&gt;8&lt;/span&gt;, &lt;span style="color: red;"&gt;9&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  10 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;The implementation of break here, of course, highly inspired from dropWhile one:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; break&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;pred: A&lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;b&gt;Boolean&lt;/b&gt;&lt;/span&gt;, xs: &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;def&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;combine&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x: A, xs: &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;      &lt;span style="color: #006699;"&gt;&lt;b&gt;if&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;pred&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;        &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs._1, x #:: xs._2, x #:: xs._3&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;             &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   5 &lt;/span&gt;&lt;/span&gt;      &lt;span style="color: #006699;"&gt;&lt;b&gt;else&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   6 &lt;/span&gt;&lt;/span&gt;        &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;x #:: xs._3, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.empty, x #:: xs._3&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   7 &lt;/span&gt;&lt;/span&gt;             &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   8 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;val&lt;/b&gt;&lt;/span&gt; base:&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.empty, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.empty, &lt;span style="color: #0099ff;"&gt;&lt;b&gt;Stream&lt;/b&gt;&lt;/span&gt;.empty&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   9 &lt;/span&gt;&lt;/span&gt;   &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  10 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;b&gt;val&lt;/b&gt;&lt;/span&gt; result &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;foldr&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;combine, base&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;xs&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  11 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;result._2, result._1&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  12 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Summary&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;In this blog post, I showed the result of playing around with Stream and fold right. First, I rewrote the implementation of foldRight, showed the use of foldRight, expressed some collection functions using foldRight, and finally provided dropWhile and break.&lt;br /&gt;&lt;br /&gt;The blog post does not have intention to overcome the non-laziness of Scala language.&amp;nbsp;Scala is not a lazy language, and I think it's not appropriate to use lazy evaluation as the main stream of the codes.&lt;br /&gt;&lt;br /&gt;Pope (Pope, 2010 ?) in Monad Reader 6 discussed further the implementation of dropWhile. He presented two further solutions based on higher order function composition. He also presented the fold implementation using what is so called fix function. See&amp;nbsp;&lt;a href="http://en.wikibooks.org/wiki/Haskell/Fix_and_recursion"&gt;http://en.wikibooks.org/wiki/Haskell/Fix_and_recursion&lt;/a&gt;&amp;nbsp;to have an idea of what Fix function is.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;References&lt;/b&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: large; font-weight: bold;"&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Hutton G (1999) A tutorial on the universality and expressiveness of fold. Available from&amp;nbsp;&lt;a href="http://www.cs.nott.ac.uk/~gmh/fold.pdf"&gt;http://www.cs.nott.ac.uk/~gmh/fold.pdf&lt;/a&gt;&amp;nbsp;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Pope B (2010 ?) &amp;nbsp;Getting a Fix from the Right Fold. Available from:&amp;nbsp;&lt;a href="http://www.haskell.org/wikiupload/1/14/TMR-Issue6.pdf"&gt;http://www.haskell.org/wikiupload/1/14/TMR-Issue6.pdf&lt;/a&gt;&amp;nbsp;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2012573949883447809-2608266554810592218?l=voidmainargs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/2608266554810592218/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2012573949883447809&amp;postID=2608266554810592218&amp;isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/2608266554810592218'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/2608266554810592218'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/2011/08/folding-stream-with-scala.html' title='Folding Stream with Scala'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-3683965472846044878</id><published>2011-07-13T01:09:00.004+02:00</published><updated>2012-01-19T07:15:03.675+01:00</updated><title type='text'>A List containing elements with consecutive types A and B</title><content type='html'>Just wondering how we can create a "heterogenous" list of two types, where an object of the first type must be inserted after the second.&lt;br /&gt;&lt;br /&gt;Examples: &lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;5 :: "help" :: 8 :: "ldf"&lt;/span&gt; to create a list with type (Int, String)&lt;br /&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;"help" :: 8 :: "ldf" :: 9&lt;/span&gt; to create a list with type (String, Int)&lt;br /&gt;&lt;br /&gt;Here is what I got:&lt;br /&gt;&lt;script src="https://gist.github.com/1079163.js"&gt; &lt;/script&gt;Wondering if I can do better.&lt;br /&gt;&lt;br /&gt;-&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2012573949883447809-3683965472846044878?l=voidmainargs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/3683965472846044878/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2012573949883447809&amp;postID=3683965472846044878&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/3683965472846044878'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/3683965472846044878'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/2011/07/list-containing-elements-with.html' title='A List containing elements with consecutive types A and B'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-1960306855176892058</id><published>2011-01-24T18:56:00.010+01:00</published><updated>2012-01-19T07:15:03.648+01:00</updated><title type='text'>Implementing Bird's Make Century Pearl</title><content type='html'>Book of Bird (2000) contains 30 pearls of functional programming. I haven't finished this very inspiring book yet, but I would love to share one of the pearl, the pearl number 6, making century.&lt;br /&gt;&lt;br /&gt;The problem that the pearl solves is given a number (1, 2, 3, 4, 5, 6, 7, 8, 9) return all expressions containing + and * that make 100. In this case, the output is:&lt;br /&gt;&lt;br /&gt;1 * 2 * 3 + 4 + 5 + 6 + 7 + 8 * 9&lt;br /&gt;1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 * 9&lt;br /&gt;1 * 2 * 3 * 4 + 5 + 6 + 7 * 8 + 9&lt;br /&gt;12 + 3 * 4 + 5 + 6 + 7 * 8 + 9&lt;br /&gt;1 + 2 * 3 + 4 + 5 + 67 + 8 + 9&lt;br /&gt;1 * 2 + 34 + 5 + 6 * 7 + 8 + 9&lt;br /&gt;12 + 34 + 5 * 6 + 7 + 8 + 9&lt;br /&gt;&lt;br /&gt;The following is the specification written using BDD library in Scala:&lt;br /&gt;&lt;pre&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;class  &lt;/span&gt;&lt;span class="s1"&gt;MakeNumberSpec &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;extends &lt;/span&gt;&lt;span class="s1"&gt;FlatSpec &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;with &lt;/span&gt;&lt;span class="s1"&gt;ShouldMatchers { &lt;br /&gt; &lt;br /&gt;  &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;"Make Number" &lt;/span&gt;&lt;span class="s1"&gt;should &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;"return all expressions that make a number" &lt;/span&gt;&lt;span class="s1"&gt;in { &lt;br /&gt;&lt;/span&gt;&lt;span class="s1"&gt;     &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;val &lt;/span&gt;&lt;span class="s1"&gt;xs = (&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;1 &lt;/span&gt;&lt;span class="s1"&gt;to &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;9&lt;/span&gt;&lt;span class="s1"&gt;).toList &lt;br /&gt;     solutions(xs, &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;100&lt;/span&gt;&lt;span class="s1"&gt;).map(displayE(_)) should equal( &lt;br /&gt;        List(&lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;"1 * 2 * 3 + 4 + 5 + 6 + 7 + 8 * 9"&lt;/span&gt;&lt;span class="s1"&gt;, &lt;br /&gt;             &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;"1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 * 9"&lt;/span&gt;&lt;span class="s1"&gt;, &lt;br /&gt;             &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;"1 * 2 * 3 * 4 + 5 + 6 + 7 * 8 + 9"&lt;/span&gt;&lt;span class="s1"&gt;, &lt;br /&gt;             &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;"12 + 3 * 4 + 5 + 6 + 7 * 8 + 9"&lt;/span&gt;&lt;span class="s1"&gt;, &lt;br /&gt;             &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;"1 + 2 * 3 + 4 + 5 + 67 + 8 + 9"&lt;/span&gt;&lt;span class="s1"&gt;, &lt;br /&gt;             &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;"1 * 2 + 34 + 5 + 6 * 7 + 8 + 9"&lt;/span&gt;&lt;span class="s1"&gt;, &lt;br /&gt;             &lt;/span&gt;&lt;span class="s2" style="color: green; font-weight: bold;"&gt;"12 + 34 + 5 * 6 + 7 + 8 + 9"&lt;/span&gt;&lt;span class="s1"&gt;)) &lt;br /&gt;  }&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="s1"&gt;&lt;/span&gt;}&lt;/pre&gt;&lt;br /&gt;The complete solution described in this blog is available at my github&amp;nbsp;&lt;a href="https://github.com/anrizal06/rizier/blob/master/src/main/scala/org/rizier/functions/MakeNumber.scala"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Let's first define the expression, term, and factor. A factor is a list of digit, like List(1, 2) is interpreted as 12. Then, term is a multiplication of factors, like 4 * 15, and expression is sum of terms.&lt;br /&gt;&lt;br /&gt;It might be tempted to introduce class Expression, Term, or Factor. But I prefer to make the solution simple by using type alias instead:&lt;br /&gt;&lt;pre&gt;&lt;span class="s1"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l18"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt; &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;type &lt;/span&gt;&lt;span class="s1"&gt;Factor = List[Int] &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l19"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt; &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;type &lt;/span&gt;&lt;span class="s1"&gt;Term = List[Factor] &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l20"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt; &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;type &lt;/span&gt;&lt;span class="s1"&gt;Expr = List[Term] &lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;To display factor, term, and expression, I introduced displayF, displayT, and displayE as follow:&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="s1"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="s1"&gt;displayF(f: Factor) = f.reduceLeft(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="s3" style="color: blue;"&gt;10 &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="s1"&gt;* _ + _).toString&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="s1"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="s0"&gt;&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;displayT(t: Term) = t.map(displayF(_)).mkString(&lt;/span&gt;&lt;span class="s4" style="color: green; font-weight: bold;"&gt;" * "&lt;/span&gt;&lt;span class="s1"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="s1"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="s0"&gt;&lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;displayE(e: Expr) = e.map(displayT(_)).mkString(&lt;/span&gt;&lt;span class="s4" style="color: green; font-weight: bold;"&gt;" + "&lt;/span&gt;&lt;span class="s1"&gt;) &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let's test first the displayE method:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s1"&gt;   scala&amp;gt; displayE(List(List(List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;1&lt;/span&gt;&lt;span class="s1"&gt;), List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;2&lt;/span&gt;&lt;span class="s1"&gt;), List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;3&lt;/span&gt;&lt;span class="s1"&gt;)), &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l51"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;       List(List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;4&lt;/span&gt;&lt;span class="s1"&gt;)), List(List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;5&lt;/span&gt;&lt;span class="s1"&gt;)), List(List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;6&lt;/span&gt;&lt;span class="s1"&gt;)), List(List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;7&lt;/span&gt;&lt;span class="s1"&gt;)), &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l52"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;       List(List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;8&lt;/span&gt;&lt;span class="s1"&gt;), List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;9&lt;/span&gt;&lt;span class="s1"&gt;)))) &lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman'; white-space: normal;"&gt;&lt;pre&gt;&lt;span class="s3" style="color: blue;"&gt;   1 &lt;/span&gt;&lt;span class="s1"&gt;* &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;2 &lt;/span&gt;&lt;span class="s1"&gt;* &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;3 &lt;/span&gt;&lt;span class="s1"&gt;+ &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;4 &lt;/span&gt;&lt;span class="s1"&gt;+ &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;5 &lt;/span&gt;&lt;span class="s1"&gt;+ &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;6 &lt;/span&gt;&lt;span class="s1"&gt;+ &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;7 &lt;/span&gt;&lt;span class="s1"&gt;+ &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;8 &lt;/span&gt;&lt;span class="s1"&gt;* &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;9&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The book (Bird, 2010) provides an excellent theory of the solution described here. I will try to be more practical here.&lt;br /&gt;&lt;br /&gt;The problem can be attacked by exhaustive search, which means for each two consecutive number, either we add '+', '*' or simply concatenating the numbers.&lt;br /&gt;For example, for 8 and 9 we can have 89 by concatenation, 8 + 9 by summation, and 8 * 9 by multiplication. Given the description of the problem, there will be then 3&lt;sup&gt;n&lt;/sup&gt; expression candidates to evaluate. For the problem of make century here, it is not that big, only&amp;nbsp;3&lt;sup&gt;8&lt;/sup&gt; = 6561 candidates. &lt;br /&gt;&lt;br /&gt;To do the exhaustive search, we need the following function that expands an expression to a list of expressions when a number is introduced to the list of candidates so far. Three possibilities: concatenating, summing, and multiplying. The code is (with xs is the factor, xss is the term, and xsss is the rest of the expression).&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;    case &lt;/span&gt;&lt;span class="s1"&gt;(xs::xss):: xsss =&amp;gt; &lt;br /&gt;      List( ((x::xs)::xss)::xsss,         // concatenating &lt;br /&gt;            List(x) :: xs::xss) :: xsss,  // multiplying &lt;br /&gt;           ( List(List(x)) :: (xs::xss):: xsss)) // sum &lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;We need of course to take into account the basic case, which is inserting to an empty expression. The empty expression itself is List(List(Nil)) . The complete function becomes:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s1"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l28"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;glue(expr: Expr, x: Int) : List[Expr]= &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l29"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;    expr &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;match &lt;/span&gt;&lt;span class="s1"&gt;{ &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l30"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;(Nil::Nil)::Nil =&amp;gt; List(List(List(List(x)))) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l31"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;(xs::xss):: xsss =&amp;gt; &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l32"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;        List( ((x::xs)::xss)::xsss, &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l33"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;              (List(x) :: xs::xss) :: xsss, &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l34"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;    &lt;/span&gt;&lt;/a&gt;              ( List(List(x)) :: (xs::xss):: xsss)) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l35"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;    &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;_ =&amp;gt; Nil // or throw IllegalArgumentException ?&lt;br /&gt;       }&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Let's do some tests:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s1"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l40"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;  scala&amp;gt; glue(List(List(Nil)), &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;9&lt;/span&gt;&lt;span class="s1"&gt;).map(displayE(_)) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l41"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;  res3: List[String] = List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;9&lt;/span&gt;&lt;span class="s1"&gt;) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l42"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;   &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l43"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;  scala&amp;gt; glue( List(List(List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;9&lt;/span&gt;&lt;span class="s1"&gt;))), &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;2&lt;/span&gt;&lt;span class="s1"&gt;).map(displayE(_)) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l44"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;  res4: List[String] = List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;29&lt;/span&gt;&lt;span class="s1"&gt;, &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;2 &lt;/span&gt;&lt;span class="s1"&gt;* &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;9&lt;/span&gt;&lt;span class="s1"&gt;, &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;2 &lt;/span&gt;&lt;span class="s1"&gt;+ &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;9&lt;/span&gt;&lt;span class="s1"&gt;) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l45"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;   &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l46"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;  scala&amp;gt; glue( List(List(List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;2&lt;/span&gt;&lt;span class="s1"&gt;), List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;9&lt;/span&gt;&lt;span class="s1"&gt;))), &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;1&lt;/span&gt;&lt;span class="s1"&gt;).map(displayE(_)) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l47"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;  res5: List[String] = List(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;12 &lt;/span&gt;&lt;span class="s1"&gt;* &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;9&lt;/span&gt;&lt;span class="s1"&gt;, &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;1 &lt;/span&gt;&lt;span class="s1"&gt;* &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;2 &lt;/span&gt;&lt;span class="s1"&gt;* &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;9&lt;/span&gt;&lt;span class="s1"&gt;, &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;1 &lt;/span&gt;&lt;span class="s1"&gt;+ &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;2 &lt;/span&gt;&lt;span class="s1"&gt;* &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;9&lt;/span&gt;&lt;span class="s1"&gt;) &lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div&gt;&lt;span class="s1"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;The exhaustive solution can then be implemented by continuously applying the glue for each insertion of new number taken into account. This is done by using foldRight of flatMap functions. Then, of course we filter only the one that makes the number to be kept. The following is the code that executes the exhaustive search:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s1"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l75"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt; &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;valueF(f: Factor) = f.reduceLeft(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;10 &lt;/span&gt;&lt;span class="s1"&gt;* _ + _) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l76"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt; &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;valueT(t: Term) = t.foldLeft(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;1&lt;/span&gt;&lt;span class="s1"&gt;)( _ * valueF(_)) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l77"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt; &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;valueE(e: Expr) = e.foldLeft(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;0&lt;/span&gt;&lt;span class="s1"&gt;)(_ + valueT(_)) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l79"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="s1"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l79"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt; &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;val &lt;/span&gt;&lt;span class="s1"&gt;emptyExpression: Expr = List(List(Nil)) &lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l81"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt; &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;solutions(xs: List[Int], n: Int): List[Expr] = { &lt;br /&gt;         &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;good(exp: Expr) = valueE(exp) == n &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l83"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l84"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;      xs.foldRight(List(emptyExpression))( &lt;br /&gt;           (x, ys) =&amp;gt; ys.flatMap(glue(_,x))). &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l86"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;      filter(good) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l87"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt; &lt;/span&gt;&lt;/a&gt;    } &lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;The code can be optimized using a simple enhancement, that is, by using an ok method that has the property good(candidate) =&amp;gt; ok(candidate). In this case, we can use ok definition as follow:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;   def &lt;/span&gt;&lt;span class="s1"&gt;ok(exp: Expr) = valueE(exp) &amp;lt;= n&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;and use it as the the filter of the glue operation. The implementation of solutions method becomes:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s1"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l49"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt; &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;solutions(xs: List[Int], n: Int): List[Expr] = { &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l50"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l51"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;ok(exp: Expr) = valueE(exp) &amp;lt;= n &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l52"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;good(exp: Expr) = valueE(exp) == n &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l53"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l54"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;    xs.foldRight(List(emptyExpression))( &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l55"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;   &lt;/span&gt;&lt;/a&gt;       (x, ys) =&amp;gt;&amp;nbsp;&lt;/span&gt;ys.flatMap(&lt;span class="Apple-style-span" style="color: red;"&gt;glue(_,x).filter(ok)&lt;/span&gt;)).filter(good)&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;pre&gt;&lt;span class="s1"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l58"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt; &lt;/span&gt;&lt;/a&gt;  } &lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;The solution above can still be optimized by observing that value method executes the same expressions several times. To avoid recalculation, we propagate the last factor, term, and expression.&lt;br /&gt;The following is the improved code, a little bit obscure, I admit.&lt;br /&gt;&lt;br /&gt;First, the factor, term, and expression is represented by Intermediate class:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s1"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l103"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case class &lt;/span&gt;&lt;span class="s1"&gt;Intermediate(k: Int, f: Int, t: Int, e: Int) { &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l104"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;    &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;value = f * t + e &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l105"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;  } &lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div&gt;&lt;span class="s1"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="s1"&gt;And, finally the modified glue and solution:&lt;/span&gt;&lt;br /&gt;&lt;span class="s1"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s1"&gt;&lt;span class="s1"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l107"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;  &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;glue(expr: Expr, x: Int, im: Intermediate): &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l108"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;      List[(Expr,Intermediate)]=  { &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l109"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l110"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;    expr &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;match &lt;/span&gt;&lt;span class="s1"&gt;{ &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l111"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;(Nil::Nil)::Nil =&amp;gt; &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l112"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;        &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;val &lt;/span&gt;&lt;span class="s1"&gt;exp = List(List(List(x))) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l113"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;        List( (exp, Intermediate(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;10&lt;/span&gt;&lt;span class="s1"&gt;, x, &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;1&lt;/span&gt;&lt;span class="s1"&gt;, &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;0&lt;/span&gt;&lt;span class="s1"&gt;)) ) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l114"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l115"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;(xs::xss):: xsss =&amp;gt; &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l116"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;        List( ( ((x::xs)::xss)::xsss, &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l117"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;                Intermediate(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;10&lt;/span&gt;&lt;span class="s1"&gt;*im.k, im.k * x + im.f , im.t, im.e)), &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l118"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;              ( (List(x) :: xs::xss) :: xsss, &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l119"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;                Intermediate(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;10&lt;/span&gt;&lt;span class="s1"&gt;, x, im.f * im.t, im.e)), &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l120"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;              ( (List(List(x)) :: (xs::xss):: xsss), &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l121"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;                Intermediate(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;10&lt;/span&gt;&lt;span class="s1"&gt;, x, &lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;1&lt;/span&gt;&lt;span class="s1"&gt;, im.f*im.t + im.e))) &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l122"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;      &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;case &lt;/span&gt;&lt;span class="s1"&gt;_ =&amp;gt; Nil &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l123"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l124"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;    } &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l125"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="s1"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l129"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span class="s1"&gt;solutions(xs: List[Int], n: Int): List[Expr] = { &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span class="s1"&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l130"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l131"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;     &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;ok(exp: (Expr, Intermediate)) = exp._2.value  &amp;lt;= n &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l132"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;     &lt;/span&gt;&lt;span class="s0" style="color: navy; font-weight: bold;"&gt;def &lt;/span&gt;&lt;span class="s1"&gt;good(exp: (Expr, Intermediate)) = exp._2.value == n &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l133"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt; &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l134"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;     xs.foldRight(List( (emptyExpression, Intermediate(&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;0&lt;/span&gt;&lt;span class="s1"&gt;,&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;0&lt;/span&gt;&lt;span class="s1"&gt;,&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;0&lt;/span&gt;&lt;span class="s1"&gt;,&lt;/span&gt;&lt;span class="s3" style="color: blue;"&gt;0&lt;/span&gt;&lt;span class="s1"&gt;))))( &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l135"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;        (x: Int, ys: List[(Expr, Intermediate)]) =&amp;gt; &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l136"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;          ys.flatMap( y =&amp;gt; &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l137"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;            glue(y._1, x, y._2).filter(ok))). &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l138"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;        filter(good).unzip._1 &lt;br /&gt;&lt;a href="http://www.blogger.com/post-edit.g?blogID=2012573949883447809&amp;amp;postID=1960306855176892058" name="l140"&gt;&lt;span class="ln" style="color: black; font-style: normal; font-weight: normal;"&gt;  &lt;/span&gt;&lt;/a&gt;  } &lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="s1"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;b&gt;Reference&lt;/b&gt;&lt;br /&gt;Bird R(2010), &lt;i&gt;Pearls of Functional Algorithm Design&lt;/i&gt;.&amp;nbsp;Cambridge University Press.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2012573949883447809-1960306855176892058?l=voidmainargs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/1960306855176892058/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2012573949883447809&amp;postID=1960306855176892058&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/1960306855176892058'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/1960306855176892058'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/2011/01/implementing-birds-make-century-pearl.html' title='Implementing Bird&apos;s Make Century Pearl'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-1137419858854024521</id><published>2010-12-29T09:54:00.008+01:00</published><updated>2012-01-19T07:15:03.636+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='clojure'/><category scheme='http://www.blogger.com/atom/ns#' term='sieve'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Lazy Sieve Eratosthenes in Scala and Clojure</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;Sieve Eratosthenes is a popular prime number generation algorithm. &amp;nbsp;Start with 2 and cross out all even numbers until a limit number. Then, doing the same for 3 to cross out all factors of 3, like 3, 6, 9. &amp;nbsp;The goal of the blog is to show some lazy sieve algorithms using clojure and scala and show the benchmark result of the algorithms.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;The complete code is available at &lt;a href="https://github.com/anrizal06/rizier"&gt;https://github.com/anrizal06/rizier&lt;/a&gt;&amp;nbsp;.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;It is easy to implement eager Sieve algorithms, using scala, this can be achieved as follow:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span style="color: #9966ff;"&gt;sieve&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;limit: &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span style="color: #009966;"&gt;&lt;strong&gt;Int&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt; &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: monospace; white-space: pre;"&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;   &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   3 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;strong&gt;val&lt;/strong&gt;&lt;/span&gt; composites &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;new&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Array&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Boolean]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;limit&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;   &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   5 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;2&lt;/span&gt; to math.&lt;span style="color: #9966ff;"&gt;sqrt&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;limit&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;.toInt&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;foreach&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;i &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   6 &lt;/span&gt;&lt;/span&gt;       &lt;span style="color: #006699;"&gt;&lt;strong&gt;if&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;!&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;composites&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;i&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   7 &lt;/span&gt;&lt;/span&gt;           &lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;i &lt;span style="color: black;"&gt;&lt;strong&gt;*&lt;/strong&gt;&lt;/span&gt; i  until limit by i&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;foreach&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;composites&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;_&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #cc00cc;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   8 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   9 &lt;/span&gt;&lt;/span&gt;   &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  10 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;strong&gt;for&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; i &lt;span style="color: black;"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;-&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; until limit&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  11 &lt;/span&gt;&lt;/span&gt;         &lt;span style="color: #006699;"&gt;&lt;strong&gt;if&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;!&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;composites&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;i&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  12 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;yield&lt;/strong&gt;&lt;/span&gt; i&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  13 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;div style="color: black; font-weight: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;On of the problem with the solution above is that the limit parameter that must be used. The parameter is OK if we want to get prime numbers below a certain limit, but it is not OK when we want, for example to get the 1000th prime.&lt;/span&gt;&lt;/div&gt;&lt;div style="color: black; font-weight: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="color: black; font-weight: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;The solution for the problem above presented here is by using lazy evaluation provided in both scala and clojure. Using lazy algorithms, we generate a prime number, only when necessary. For example, if we want to get the 26th prime number, we only generate 26 prime number, not more.&lt;/span&gt;&lt;/div&gt;&lt;div style="color: black; font-weight: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="color: black;"&gt;&lt;/div&gt;&lt;div style="color: black; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;The lazy algorithms for sieve are presented in the work of Melissa ONeill (2009) and the idea are adopted by for example clojure contrib API. The paper also showed that the naive functional solution (not exposed here) is very limited. Indeed, I made some tests with the solution and reached easily StackOverflow error for relatively small number.&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;pre&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;pre&gt;&lt;b&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="color: black;"&gt;&lt;/div&gt;&lt;div style="color: black; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;Not So Naive Solution&lt;/span&gt;&lt;/div&gt;&lt;/b&gt;&lt;/pre&gt;&lt;pre&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="color: black;"&gt;&lt;/div&gt;&lt;div style="color: black; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;Let's start first with the still naive, but optimized somehow, lazy sieve eratosthenes. First in clojure:&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;pre&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="color: #990066; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;div style="display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;/div&gt;&lt;/span&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="color: #990066; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="color: black; font-family: 'Times New Roman'; white-space: normal;"&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;defn&lt;/span&gt; naive-primes&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #ff00cc;"&gt;"&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;Implementation&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;of&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;lazy&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;but&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;naive&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;sieve&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;algorithms&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;&lt;span style="color: #02b902;"&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;  &lt;span style="color: blue;"&gt;(&lt;/span&gt;letfn &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   5 &lt;/span&gt;&lt;/span&gt;          &lt;span style="color: blue;"&gt;(&lt;/span&gt;next-prime &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;x xs&lt;span style="color: #02b902;"&gt;]&lt;/span&gt; &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   6 &lt;/span&gt;&lt;/span&gt;             &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;strong&gt;if&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;some&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;#(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;zero?&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;rem&lt;/strong&gt;&lt;/span&gt; x &lt;span style="color: #66ccff;"&gt;&lt;strong&gt;%&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   7 &lt;/span&gt;&lt;/span&gt;                    &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;take-while&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;#(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;&amp;lt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;*&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #66ccff;"&gt;&lt;strong&gt;%&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #66ccff;"&gt;&lt;strong&gt;%&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt; x&lt;span style="color: #9966ff;"&gt;)&lt;/span&gt; xs&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   8 &lt;/span&gt;&lt;/span&gt;             &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;strong&gt;recur&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; x &lt;span style="color: red;"&gt;2&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt; xs&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   9 &lt;/span&gt;&lt;/span&gt;             &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;cons&lt;/span&gt; x &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;lazy-seq&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;next-prime &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; x &lt;span style="color: red;"&gt;2&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;conj&lt;/strong&gt;&lt;/span&gt; xs x&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: #02b902;"&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  10 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;cons&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;lazy-seq&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;next-prime &lt;span style="color: red;"&gt;3&lt;/span&gt; &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;&lt;span style="color: #02b902;"&gt;]&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;))&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; white-space: normal;"&gt;Line 10 starts the implementation by consing 2 to the lazy sequence of prime number starting from 3. Then, starting from 3, all odd numbers are examined if they are prime or not. This is done at line 6-7 of the code. If it is a composite number, then it recursively calls next prime to examine the next odd number (line 8) . Otherwise, if it is a prime, then the prime number is returned, and the list of prime numbers discovered so far is updated.&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; white-space: normal;"&gt;To get the 100th prime, one can use&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; white-space: normal;"&gt;(nth (naive-primes) 100)&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;The equivalent Scala program is the following:&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 1 &lt;/span&gt;&lt;/span&gt;  &lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;primes&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;: &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt; 2 &lt;/span&gt;&lt;/span&gt;     &lt;span style="color: #006699;"&gt;&lt;strong&gt;lazy&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;val&lt;/strong&gt;&lt;/span&gt; ps &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; #:: &lt;span style="color: #9966ff;"&gt;sieve&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;3&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 3 &lt;/span&gt;&lt;/span&gt;     &lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;sieve&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;p: &lt;span style="color: #009966;"&gt;&lt;strong&gt;Int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;: &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 4 &lt;/span&gt;&lt;/span&gt;         p #:: &lt;span style="color: #9966ff;"&gt;sieve&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 5 &lt;/span&gt;&lt;/span&gt;            &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;.&lt;span style="color: #9966ff;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;p &lt;span style="color: black;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt;, &lt;span style="color: red;"&gt;2&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;.&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 6 &lt;/span&gt;&lt;/span&gt;                &lt;span style="color: #9966ff;"&gt;find&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;i&lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt; ps.&lt;span style="color: #9966ff;"&gt;takeWhile&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;j &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt; j &lt;span style="color: black;"&gt;&lt;strong&gt;*&lt;/strong&gt;&lt;/span&gt; j &lt;span style="color: black;"&gt;&lt;strong&gt;&amp;lt;=&lt;/strong&gt;&lt;/span&gt; i&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;.&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt; 7 &lt;/span&gt;&lt;/span&gt;                &lt;span style="color: #9966ff;"&gt;forall&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;i &lt;span style="color: black;"&gt;&lt;strong&gt;%&lt;/strong&gt;&lt;/span&gt; _ &lt;span style="color: black;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;.get&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 8 &lt;/span&gt;&lt;/span&gt;     &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 9 &lt;/span&gt;&lt;/span&gt;     ps&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;10 &lt;/span&gt;&lt;/span&gt;  &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt; &lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;Stream is the Scala equivalent of Clojure lazy-seq. Note the interesting use of lazy val. To get the 100th prime number, one can use &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;primes().take(100).last&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;At&lt;a href="http://stackoverflow.com/questions/4534230/why-stream-lazy-val-implementation-using-is-faster-than-listbuffer-one"&gt; stack overflow&lt;/a&gt;&amp;nbsp;, I exposed another solution using mutable list as the alternative solution to the lazy stream above. It turned out that the mutable list solution was faster than the solution above.&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;b&gt;With Wheel&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;The Clojure Contrib library implements Sieve algorithm using wheel. The idea of the algorithm is to discard as many as possible the composite number that is factor of first prime numbers (3, 5, 7 in addition to 2). Indeed, the first solution already discards 2. To discard the numbers, a circular list of step -- or wheel -- is usd. For (2, 3, 5, 7), the wheel is&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;(2 , 4 , 2 , 4 , 6 , 2 , 6 , 4 , 2 , &lt;br /&gt;       4 , 6 , 6 , 2 , 6 , 4 , 2, 6 , 4 , 6 , &lt;br /&gt;    8 , 4 , 2 , 4 , 2 , 4 , 8 , 6 , 4 , 6 , &lt;br /&gt;    2 , 4 , 6, 2 , 6 , 6 , 4 , 2 , 4 , 6 , &lt;br /&gt;    2 , 6 , 4 , 2 , 4 , 2 , 10 , 2 , 10).&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;To make the list circular, clojure has a very convenient method call cycle that can then be used to create the circular list:&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font-size: 12px; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC38" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;cycle &lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt;  &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt;  &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt;&lt;/div&gt;&lt;div class="line" id="LC39" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;8&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;8&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt;  &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt;  &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt;&lt;/div&gt;&lt;div class="line" id="LC40" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;6&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;4&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;10&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt; &lt;span class="mi" style="color: #009999; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;10&lt;/span&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: small; line-height: normal; white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="p" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: small; line-height: normal; white-space: normal;"&gt;Scala does not have built-in cycle, but it is quite straight forward to implement cycle in scala:&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"&gt;&lt;pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font-size: 12px; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;div class="line" id="LC19" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;def&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;cycle[A]&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;xs&lt;/span&gt;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt; &lt;span class="kt" style="color: #445588; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;List&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="kt" style="color: #445588; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;A&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;])&lt;/span&gt;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt; &lt;span class="kt" style="color: #445588; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Stream&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;/span&gt;&lt;span class="kt" style="color: #445588; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;A&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;]&lt;/span&gt; &lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class="line" id="LC20" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;lazy&lt;/span&gt; &lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;val&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;result&lt;/span&gt;&lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;:&lt;/span&gt; &lt;span class="kt" style="color: #445588; font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Stream&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;[&lt;span class="Apple-style-span" style="color: #445588;"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;]&lt;/span&gt; &lt;span class="k" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;=&lt;/span&gt; &lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;xs&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;toStream&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;.&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;append&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;result&lt;/span&gt;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;div class="line" id="LC21" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;result&lt;/span&gt;&lt;/div&gt;&lt;div class="line" id="LC22" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o" style="font-weight: bold; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;The complete implementation of the algorithm in Clojure is:&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;defn&lt;/span&gt; wheel-primes &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #ff00cc;"&gt;"&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;The&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;optimization&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;of&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;lazy&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;naive&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;sieve&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;algorithm&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;by&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;discarding&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;the&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;number&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;divisible&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;by&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;2&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;,&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;3&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;,&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;5&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;,&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;and&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;7&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;This&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;is&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;done&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;by&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;adding&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;the&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;next&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;number&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;to&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;be&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;examined&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   5 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;by&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;the&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;number&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;given&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;by&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;the&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;wheel&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;.&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   6 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;&lt;span style="color: #02b902;"&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   7 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;let&lt;/span&gt; &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;next-prime&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   8 &lt;/span&gt;&lt;/span&gt;         &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;fn&lt;/span&gt; next-prime &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;x xs &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;f &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;&amp;amp;&lt;/strong&gt;&lt;/span&gt; r&lt;span style="color: #02b902;"&gt;]&lt;/span&gt;&lt;span style="color: #02b902;"&gt;]&lt;/span&gt; &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   9 &lt;/span&gt;&lt;/span&gt;             &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;strong&gt;if&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;some&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;#(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;zero?&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;rem&lt;/strong&gt;&lt;/span&gt; x &lt;span style="color: #66ccff;"&gt;&lt;strong&gt;%&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  10 &lt;/span&gt;&lt;/span&gt;                    &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;take-while&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;#(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;&amp;lt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;*&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #66ccff;"&gt;&lt;strong&gt;%&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #66ccff;"&gt;&lt;strong&gt;%&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt; x&lt;span style="color: #9966ff;"&gt;)&lt;/span&gt; xs&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  11 &lt;/span&gt;&lt;/span&gt;             &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;strong&gt;recur&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; x f&lt;span style="color: blue;"&gt;)&lt;/span&gt; xs r&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  12 &lt;/span&gt;&lt;/span&gt;             &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;cons&lt;/span&gt; x &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;lazy-seq&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;next-prime &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; x f&lt;span style="color: blue;"&gt;)&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;conj&lt;/strong&gt;&lt;/span&gt; xs x&lt;span style="color: blue;"&gt;)&lt;/span&gt; r&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  13 &lt;/span&gt;&lt;/span&gt;         wheel &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;cycle&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;&lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt; &lt;span style="color: red;"&gt;6&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;6&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt; &lt;span style="color: red;"&gt;6&lt;/span&gt; &lt;span style="color: red;"&gt;6&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;6&lt;/span&gt;  &lt;span style="color: red;"&gt;4&lt;/span&gt;  &lt;span style="color: red;"&gt;2&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  14 &lt;/span&gt;&lt;/span&gt;                        &lt;span style="color: red;"&gt;6&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt; &lt;span style="color: red;"&gt;6&lt;/span&gt; &lt;span style="color: red;"&gt;8&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt; &lt;span style="color: red;"&gt;8&lt;/span&gt; &lt;span style="color: red;"&gt;6&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt; &lt;span style="color: red;"&gt;6&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt;  &lt;span style="color: red;"&gt;4&lt;/span&gt;  &lt;span style="color: red;"&gt;6&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  15 &lt;/span&gt;&lt;/span&gt;                        &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;6&lt;/span&gt; &lt;span style="color: red;"&gt;6&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt; &lt;span style="color: red;"&gt;6&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;6&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;4&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;10&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: #02b902;"&gt;]&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: #02b902;"&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  16 &lt;/span&gt;&lt;/span&gt;         &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;concat&lt;/strong&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  17 &lt;/span&gt;&lt;/span&gt;             &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;&lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: red;"&gt;3&lt;/span&gt; &lt;span style="color: red;"&gt;5&lt;/span&gt; &lt;span style="color: red;"&gt;7&lt;/span&gt;&lt;span style="color: #02b902;"&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  18 &lt;/span&gt;&lt;/span&gt;             &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;lazy-seq&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;next-prime &lt;span style="color: red;"&gt;11&lt;/span&gt; &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;&lt;span style="color: #02b902;"&gt;]&lt;/span&gt; wheel&lt;span style="color: blue;"&gt;)))&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;pre style="line-height: 18px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;The second solution is then close to the first one, except that instead of incrementing with 2, the next number to be examined is obtained by incrementing with the number in the wheel.&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="line-height: 18px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="line-height: 18px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;The Scala version of the solution is:&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="color: #666666; line-height: 18px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="color: #666666; font-family: 'Trebuchet MS', Trebuchet, sans-serif; font-size: 13px; line-height: 18px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;span class="Apple-style-span" style="color: black; font-family: monospace; white-space: pre;"&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   1 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt; cycle&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;xs: &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;List&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;: &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   2 &lt;/span&gt;&lt;/span&gt;     &lt;span style="color: #006699;"&gt;&lt;strong&gt;lazy&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;val&lt;/strong&gt;&lt;/span&gt; result: &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[A]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; xs.toStream.&lt;span style="color: #9966ff;"&gt;append&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;result&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;     result&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   5 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;wheel&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;: &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   6 &lt;/span&gt;&lt;/span&gt;     &lt;span style="color: #9966ff;"&gt;cycle&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;strong&gt;List&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   7 &lt;/span&gt;&lt;/span&gt;       &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;6&lt;/span&gt; , &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;span style="color: red;"&gt;6&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   8 &lt;/span&gt;&lt;/span&gt;       &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;6&lt;/span&gt; , &lt;span style="color: red;"&gt;6&lt;/span&gt; , &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;span style="color: red;"&gt;6&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;2&lt;/span&gt;, &lt;span style="color: red;"&gt;6&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;6&lt;/span&gt; , &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   9 &lt;/span&gt;&lt;/span&gt;       &lt;span style="color: red;"&gt;8&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;8&lt;/span&gt; , &lt;span style="color: red;"&gt;6&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;6&lt;/span&gt; , &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  10 &lt;/span&gt;&lt;/span&gt;       &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;6&lt;/span&gt;, &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;span style="color: red;"&gt;6&lt;/span&gt; , &lt;span style="color: red;"&gt;6&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;6&lt;/span&gt; , &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  11 &lt;/span&gt;&lt;/span&gt;       &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;span style="color: red;"&gt;6&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;span style="color: red;"&gt;4&lt;/span&gt; , &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;span style="color: red;"&gt;10&lt;/span&gt; , &lt;span style="color: red;"&gt;2&lt;/span&gt; , &lt;span style="color: red;"&gt;10&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  12 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  13 &lt;/span&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  14 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;x: &lt;span style="color: #009966;"&gt;&lt;strong&gt;Int&lt;/strong&gt;&lt;/span&gt;, stepIt: Iterator&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;: &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  15 &lt;/span&gt;&lt;/span&gt;       x #:: &lt;span style="color: #9966ff;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;x &lt;span style="color: black;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; stepIt.next, stepIt&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  16 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  17 &lt;/span&gt;&lt;/span&gt;   &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  18 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;primes&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;: &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  19 &lt;/span&gt;&lt;/span&gt;       &lt;span style="color: #006699;"&gt;&lt;strong&gt;lazy&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;val&lt;/strong&gt;&lt;/span&gt; ps &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; #:: &lt;span style="color: red;"&gt;3&lt;/span&gt; #:: &lt;span style="color: red;"&gt;5&lt;/span&gt; #:: &lt;span style="color: red;"&gt;7&lt;/span&gt; #:: &lt;span style="color: #9966ff;"&gt;sieve&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;11&lt;/span&gt;, wheel.iterator&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  20 &lt;/span&gt;&lt;/span&gt;       &lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;sieve&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;p: &lt;span style="color: #009966;"&gt;&lt;strong&gt;Int&lt;/strong&gt;&lt;/span&gt;, it: Iterator&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;: &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  21 &lt;/span&gt;&lt;/span&gt;           p #:: &lt;span style="color: #9966ff;"&gt;sieve&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;p &lt;span style="color: black;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; it.next, it&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;.&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  22 &lt;/span&gt;&lt;/span&gt;                           &lt;span style="color: #9966ff;"&gt;find&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;i &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt; ps.&lt;span style="color: #9966ff;"&gt;takeWhile&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;j &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt; j &lt;span style="color: black;"&gt;&lt;strong&gt;*&lt;/strong&gt;&lt;/span&gt; j &lt;span style="color: black;"&gt;&lt;strong&gt;&amp;lt;=&lt;/strong&gt;&lt;/span&gt; i&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;.&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  23 &lt;/span&gt;&lt;/span&gt;                            &lt;span style="color: #9966ff;"&gt;forall&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;i &lt;span style="color: black;"&gt;&lt;strong&gt;%&lt;/strong&gt;&lt;/span&gt; _ &lt;span style="color: black;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;.get,&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  24 &lt;/span&gt;&lt;/span&gt;                         it&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  25 &lt;/span&gt;&lt;/span&gt;       &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  26 &lt;/span&gt;&lt;/span&gt;       ps&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  27 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;pre style="color: #666666; font-family: 'Times New Roman'; font-size: medium; font-weight: normal; line-height: 18px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-size: medium; font-weight: normal; line-height: 18px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: monospace; font-weight: normal; line-height: normal; white-space: pre;"&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;b&gt;Hash Map Based Solution&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;The problem with the first two solutions is that both solutions require the check whether a number is factor of the prime number discovered so far. &amp;nbsp;For example, when examining 121, the first two algorithms verify if 121 are factor 3, 5, and 7. &amp;nbsp;It turns out, that the check is not necessary, and this leads to the third solution of lazy sieve.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;Like the two solutions above, the algorithm receives a stream of odd number and returns a stream of prime number. &amp;nbsp;The algorithm uses a hash map that stores the last discovered composite number. Each composite number that is stored in the hash map is a factor of prime numbers discovered so far. The key of the hash map is the composite number and the value is the double of the corresponding prime number.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;When a number is under examination, it is checked against the hash map.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; line-height: 18px; white-space: normal;"&gt;If the number is in the hash map, obviously, it is a composite number. In this case, the number is removed from the hash map and replaced by new composite number that is factor of the corresponding prime. The new composite number must not be in the hash map, therefore it continues to increment the number until discovering a brand new composite number.&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;On the other hand, when the number is not in the hash map since the beginning, it is a prime. A new composite corresponding to the prime is then inserted to the hashmap; the number is the square of the prime.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-size: 13px; line-height: 18px;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;pre style="display: inline !important; font-weight: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal; white-space: normal;"&gt;&lt;pre style="display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal; white-space: normal;"&gt;&lt;pre style="display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman'; white-space: normal;"&gt;&lt;pre style="display: inline !important;"&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  1 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;defn&lt;/span&gt; hashtable-primes &lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: monospace; font-size: 13px; white-space: pre;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;pre style="font-weight: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal; white-space: normal;"&gt;&lt;pre style="display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-weight: normal; white-space: normal;"&gt;&lt;pre style="display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman'; white-space: normal;"&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  2 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #ff00cc;"&gt;"&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;An&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;implementation&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;of&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;lazy&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;sieve&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;algorithm&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;by&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;storing&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;in&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;a&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;hastable&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  3 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;the&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;next&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;composite&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;number&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;corresponding&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;to&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;a&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;prime&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt; &lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;number&lt;/span&gt;&lt;span style="color: #ff00cc;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  4 &lt;/span&gt;&lt;/span&gt;   &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;&lt;span style="color: #02b902;"&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  5 &lt;/span&gt;&lt;/span&gt;  &lt;span style="color: blue;"&gt;(&lt;/span&gt;letfn &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  6 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: blue;"&gt;(&lt;/span&gt;next-composite &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;x step sieve&lt;span style="color: #02b902;"&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  7 &lt;/span&gt;&lt;/span&gt;       &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;strong&gt;if&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;sieve x&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  8 &lt;/span&gt;&lt;/span&gt;          &lt;span style="color: #cc0000;"&gt;;&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;the&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;composite&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;number&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; has &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;already been&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;found&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;previously&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  9 &lt;/span&gt;&lt;/span&gt;          &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;strong&gt;recur&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; x step&lt;span style="color: blue;"&gt;)&lt;/span&gt; step sieve&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 10 &lt;/span&gt;&lt;/span&gt;          &lt;span style="color: #cc0000;"&gt;;&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; brand &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;new&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;composite&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;number&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 11 &lt;/span&gt;&lt;/span&gt;          &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;assoc&lt;/strong&gt;&lt;/span&gt; sieve x step&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 12 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: blue;"&gt;(&lt;/span&gt;next-prime &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;x sieve&lt;span style="color: #02b902;"&gt;]&lt;/span&gt; &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 13 &lt;/span&gt;&lt;/span&gt;      &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;let&lt;/span&gt; &lt;span style="color: #02b902;"&gt;[&lt;/span&gt;step &lt;span style="color: blue;"&gt;(&lt;/span&gt;sieve x&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: #02b902;"&gt;]&lt;/span&gt;         &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt; 14 &lt;/span&gt;&lt;/span&gt;       &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;strong&gt;if&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;sieve x&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 15 &lt;/span&gt;&lt;/span&gt;          &lt;span style="color: #cc0000;"&gt;;&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;found&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;a&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;composite&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 16 &lt;/span&gt;&lt;/span&gt;          &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #009966;"&gt;&lt;strong&gt;recur&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; x &lt;span style="color: red;"&gt;2&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;next-composite &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; x step&lt;span style="color: blue;"&gt;)&lt;/span&gt; step &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;dissoc&lt;/strong&gt;&lt;/span&gt; sieve x&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 17 &lt;/span&gt;&lt;/span&gt;          &lt;span style="color: #cc0000;"&gt;;&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;found&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;a&lt;/span&gt;&lt;span style="color: #cc0000;"&gt; &lt;/span&gt;&lt;span style="color: #cc0000;"&gt;prime&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 18 &lt;/span&gt;&lt;/span&gt;          &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;cons&lt;/span&gt; x &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt; 19 &lt;/span&gt;&lt;/span&gt;            &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;lazy-seq&lt;/span&gt; &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 20 &lt;/span&gt;&lt;/span&gt;               &lt;span style="color: blue;"&gt;(&lt;/span&gt;next-prime &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; x &lt;span style="color: red;"&gt;2&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;strong&gt;assoc&lt;/strong&gt;&lt;/span&gt; sieve &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;*&lt;/strong&gt;&lt;/span&gt; x x&lt;span style="color: blue;"&gt;)&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;*&lt;/strong&gt;&lt;/span&gt; x &lt;span style="color: red;"&gt;2&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: #02b902;"&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 21 &lt;/span&gt;&lt;/span&gt;  &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;cons&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;lazy-seq&lt;/span&gt; &lt;span style="color: blue;"&gt;(&lt;/span&gt;next-prime &lt;span style="color: red;"&gt;3&lt;/span&gt; &lt;span style="color: #02b902;"&gt;{&lt;/span&gt;&lt;span style="color: #02b902;"&gt;}&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;span style="color: blue;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;The Scala version of the solution is the following. I used mutable map to gain better performance without really sacrifying the functional nature of the code:&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman'; line-height: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman'; white-space: normal;"&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 1 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;strong&gt;import&lt;/strong&gt;&lt;/span&gt; scala.collection.mutable.Map&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt; 2 &lt;/span&gt;&lt;/span&gt;    &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 3 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;primes&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; : &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 4 &lt;/span&gt;&lt;/span&gt;        &lt;span style="color: red;"&gt;2&lt;/span&gt; #:: &lt;span style="color: #9966ff;"&gt;sieve&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;3&lt;/span&gt;, Map&lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;9&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;-&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;6&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 5 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 6 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;strong&gt;private&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;sieve&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;p: &lt;span style="color: #009966;"&gt;&lt;strong&gt;Int&lt;/strong&gt;&lt;/span&gt;, pQ: Map&lt;span style="color: #9900cc;"&gt;[Int,&lt;/span&gt;&lt;span style="color: #9900cc;"&gt; &lt;/span&gt;&lt;span style="color: #9900cc;"&gt;Int]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;: &lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Stream&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9900cc;"&gt;[Int]&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt; 7 &lt;/span&gt;&lt;/span&gt;      p #:: &lt;span style="color: #9966ff;"&gt;sieve&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #9966ff;"&gt;nextPrime&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;p &lt;span style="color: black;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt;, pQ&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;, pQ &lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 8 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt; 9 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;strong&gt;private&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;nextCompositeNumber&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;10 &lt;/span&gt;&lt;/span&gt;          x:&lt;span style="color: #009966;"&gt;&lt;strong&gt;Int&lt;/strong&gt;&lt;/span&gt;, step: &lt;span style="color: #009966;"&gt;&lt;strong&gt;Int&lt;/strong&gt;&lt;/span&gt;, pQ: Map&lt;span style="color: #9900cc;"&gt;[Int,&lt;/span&gt;&lt;span style="color: #9900cc;"&gt; &lt;/span&gt;&lt;span style="color: #9900cc;"&gt;Int]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;: &lt;span style="color: #009966;"&gt;&lt;strong&gt;Unit&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;11 &lt;/span&gt;&lt;/span&gt;      pQ.&lt;span style="color: #9966ff;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;x&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;match&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;12 &lt;/span&gt;&lt;/span&gt;        &lt;span style="color: #006699;"&gt;&lt;strong&gt;case &lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Some&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;_&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;=&amp;gt;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;nextCompositeNumber&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;x &lt;span style="color: black;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; step, step, pQ&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;13 &lt;/span&gt;&lt;/span&gt;        &lt;span style="color: #006699;"&gt;&lt;strong&gt;case &lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;None&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;=&amp;gt;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;pQ&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;x&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; step&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;14 &lt;/span&gt;&lt;/span&gt;      &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;15 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;16 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;strong&gt;private&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;def&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;nextPrime&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;candidate: &lt;span style="color: #009966;"&gt;&lt;strong&gt;Int&lt;/strong&gt;&lt;/span&gt;, pQ: Map&lt;span style="color: #9900cc;"&gt;[Int,&lt;/span&gt;&lt;span style="color: #9900cc;"&gt; &lt;/span&gt;&lt;span style="color: #9900cc;"&gt;Int]&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;: &lt;span style="color: #009966;"&gt;&lt;strong&gt;Int&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;17 &lt;/span&gt;&lt;/span&gt;      pQ.&lt;span style="color: #9966ff;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;candidate&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;match&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;18 &lt;/span&gt;&lt;/span&gt;          &lt;span style="color: #006699;"&gt;&lt;strong&gt;case &lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #0099ff;"&gt;&lt;strong&gt;Some&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;step&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;=&amp;gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;19 &lt;/span&gt;&lt;/span&gt;              pQ &lt;span style="color: black;"&gt;&lt;strong&gt;-&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; candidate&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;20 &lt;/span&gt;&lt;/span&gt;              &lt;span style="color: #9966ff;"&gt;nextCompositeNumber&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;candidate &lt;span style="color: black;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; step, step, pQ&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;21 &lt;/span&gt;&lt;/span&gt;              &lt;span style="color: #9966ff;"&gt;nextPrime&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;candidate &lt;span style="color: black;"&gt;&lt;strong&gt;+&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt;, pQ&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;22 &lt;/span&gt;&lt;/span&gt;          &lt;span style="color: #006699;"&gt;&lt;strong&gt;case &lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;strong&gt;None&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: #006699;"&gt;&lt;strong&gt;=&amp;gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;23 &lt;/span&gt;&lt;/span&gt;              &lt;span style="color: #9966ff;"&gt;pQ&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;candidate &lt;span style="color: black;"&gt;&lt;strong&gt;*&lt;/strong&gt;&lt;/span&gt; candidate&lt;span style="color: black;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; candidate &lt;span style="color: black;"&gt;&lt;strong&gt;*&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;2&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;24 &lt;/span&gt;&lt;/span&gt;              candidate &lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;25 &lt;/span&gt;&lt;/span&gt;      &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-attachment: initial; background-clip: initial; background-color: #dbdbdb; background-image: initial; background-origin: initial; border-right-color: black; border-right-style: solid; border-right-width: 2px; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;26 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: black;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;b&gt;Benchmark&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;To complete the implementation, I did a simple benchmark, mainly with the Scala versions, but Clojure version showed also the similar results. For the implementation, I used Scala 2.8.1 and Clojure 1.2 with Oracle JVM 1.6.0_14 version. The benchmark was done in my tiny laptop with 3GB of memory, Windows, and Intel Pentium Dual Core.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;The result is shown in the following graph:&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-size: 13px; white-space: pre;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-size: 13px; white-space: pre;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_rg0ugyea8Uw/TRr0JpuZd1I/AAAAAAAAAC4/BjBScVzeoq0/s1600/primes.jpg" imageanchor="1" style="color: #ff9900; margin-left: 1em; margin-right: 1em; text-decoration: underline;"&gt;&lt;img border="0" height="198" src="http://4.bp.blogspot.com/_rg0ugyea8Uw/TRr0JpuZd1I/AAAAAAAAAC4/BjBScVzeoq0/s400/primes.jpg" style="border-bottom-style: none; border-color: initial; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; border-width: initial; position: relative;" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;Where x-axis shows the limit in million and the y axis is the execution time in ms. The blue line is for the naive solution, and magenta one for wheel solution, and the yellow one for the hash map one.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;The same curve, but not the same execution time, is also observed in the implementation using Clojure. However, The Clojure implementation execution time is longer than Scala.The wheel solution improves the performance of the naive one, but the implementation that uses hashmap performs much better.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;The maximum limit I could manage to have in my machine using the Scala implementation of the hash map algorithm was 150 000 000 that represented more than 8 million primes . The time of execution was around 270 s.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;pre style="display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;pre style="display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px;"&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;pre style="display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;Reference&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;&lt;pre style="font-weight: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;pre style="display: inline !important; font-weight: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;ONeill M (2009) The Genuine of Sieve Erathosthenes. Available at: &lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;&lt;pre style="color: #666666; font-weight: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;pre style="display: inline !important; font-weight: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;a href="http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf" style="color: #ff9900; text-decoration: none;"&gt;http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf&lt;/a&gt; &lt;/pre&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px; white-space: normal;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;pre style="display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;pre style="display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #666666; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="line-height: 18px;"&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;div style="font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: monospace;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style="display: inline !important; font-weight: normal; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span class="Apple-style-span" style="white-space: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;pre&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;pre&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;pre&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;pre&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;pre&gt;&lt;div style="color: black; display: inline !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; white-space: normal;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2012573949883447809-1137419858854024521?l=voidmainargs.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://voidmainargs.blogspot.com/feeds/1137419858854024521/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2012573949883447809&amp;postID=1137419858854024521&amp;isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/1137419858854024521'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2012573949883447809/posts/default/1137419858854024521'/><link rel='alternate' type='text/html' href='http://voidmainargs.blogspot.com/2010/12/lazy-sieve-eratosthenes-in-scala-and.html' title='Lazy Sieve Eratosthenes in Scala and Clojure'/><author><name>voidmainargs</name><uri>http://www.blogger.com/profile/00911109433888554531</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-DPohU71HWAE/Ts_oJq_fGSI/AAAAAAAAAD0/QPAqHTcMmns/s220/anrizal.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_rg0ugyea8Uw/TRr0JpuZd1I/AAAAAAAAAC4/BjBScVzeoq0/s72-c/primes.jpg' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2012573949883447809.post-44026924795134110</id><published>2010-12-06T00:02:00.000+01:00</published><updated>2012-01-19T07:15:03.684+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='hadoop'/><title type='text'>Hello Hadoop - Getting Started with Hadoop in Pseudo-Distributed Cluster Mode (Ubuntu)</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;In this blog, we will get started with Apache Hadoop by writing a map reduce job to process log files. We will start with the problem to solve, design the solution, download and install hadoop, and then execute the mapreduce solution that we design. Therefore, you can perceive this blog as "Hello Hadoop" blog.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Times,'Times New Roman',serif; font-size: large;"&gt;&lt;b&gt;Problem and Solution Sketch&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;The case that we will see is taken from a fictitious case from a web server for flight reservation. The web server logs the requests from its users, including the origin and destionation (airport code and city code), the date of departure and the date of the return.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;The log lines look like the following:&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;PARCDGFRAFRA-02022011/06022011&amp;nbsp;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;MRSMRSMUCMUC-10022011/20112011&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;NCENCEMUCMUC-11012011/19012011&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;NCENCELONLHR-21022011/03032011&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;The first three characters represent the city of origin, for example PAR to represent Paris. The second three characters represent the airport of origin, for example CDG to represent Charles de Gaulle airport. Then, the following six characters represent the city and airport of destinations. Starting from the 14th character, we have the date of departure in ddMMyy format, followed by date of the return flight in the same format.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;We can have a lot of things to do with the logs, for example to know the distribution of the request for each city, the distribution of the request per stay duration, the distribution of the request for each date departure, &amp;nbsp;and son on and so forth. For this example, we will just try to get the distribution of the requests per each origin. That is, we want to have the following consolidated output:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&lt;i&gt;LYS&amp;nbsp;80&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&lt;i&gt;MRS 42&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&lt;i&gt;NCE&amp;nbsp;834&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&lt;i&gt;PAR&amp;nbsp;838&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&lt;i&gt;TLS&amp;nbsp;296&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;Hadoop distributes the files in the cluster. The files are then served as input of the mapper nodes which maps each line of the input to a (key, value) pair. &amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;Hadoop optimizes the computation by ensuring that the node maps the data that are local to it. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;For example, the line&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: 'Times New Roman'; margin: 0px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style="font-family: 'Times New Roman'; margin: 0px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;PARCDGFRAFRA-02022011/06022011&amp;nbsp;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;may be mapped to (PAR, 1) in a mapreduce jobs that count the number of &amp;nbsp;Paris city code and 1 is to be used as the count. The same line may also be mapped to (PAR, 4) when the line is mapped to pair of &amp;lt;origin city, &amp;nbsp;duration of the stay&amp;gt;. &amp;nbsp;The line may also be mapped to (PARFRA, 4 days) when the line is mapped to &amp;lt;origin and destination city, duration of the stay&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;The result of the mapping are then delivered to the reducer. Each (key, value) pair that has the same key value is delivered to the same reducer. The delivery to the same reducer is called shuffle and sort process.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;For our problem here, we will then map each line of the log to &amp;lt;origin city, 1&amp;gt;. We map then the following lines&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: 'Times New Roman'; margin: 0px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;PARCDGFRAFRA-02022011/06022011&amp;nbsp;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style="font-family: 'Times New Roman'; margin: 0px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;MRSMRSMUCMUC-10022011/20112011&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style="font-family: 'Times New Roman'; margin: 0px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;NCENCEMUCMUC-11012011/19012011&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style="font-family: 'Times New Roman'; margin: 0px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;NCENCELONLHR-21022011/03032011&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;PARCDGFRAFRA-02022011/06022011&amp;nbsp;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;to (PAR, 1), (MRS, 1), (NCE, 1), (NCE, 1). The (PAR, [1, 1]), (MRS, [1]), and (NCE, [1, 1]) are then delivered to reducer. The reducer then reduces the three lines to&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;(PAR, 2), (MRS, 1), and (NCE, 2) which is what we need as the solution of the problem.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: 'Times New Roman'; margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Times,'Times New Roman',serif; font-size: large;"&gt;&lt;b&gt;Installing Hadoop in Pseudo-Distributed Mode&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;We are now ready to install Hadoop in our environment. To do so, you will need to have the following configuration of machines:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_rg0ugyea8Uw/TPwVVsBZdxI/AAAAAAAAACw/0xRaHOT0Kks/s1600/rack.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="238" src="http://4.bp.blogspot.com/_rg0ugyea8Uw/TPwVVsBZdxI/AAAAAAAAACw/0xRaHOT0Kks/s320/rack.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;I'm joking of course. No, you will not need those machines, you can use your personal computer. I don't even use my desktop computer for this example, only my laptop computer. However, you will need Ubuntu to be installed in your machine.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;We will download hadoop from apache site:&amp;nbsp;&lt;/span&gt;&lt;a href="http://hadoop.apache.org/"&gt;http://hadoop.apache.org/&lt;/a&gt;&amp;nbsp;. &lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;We will use the 0.21.0 version. Make sure to execute md5sum against the downloaded file. Then, copy the file to the target directory. For example to /home/arizal.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;sudo mv /home/arizal/Downloads/hadoop-0.21.0.tar.gz /home/arizal&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;Then untar the file:&lt;/div&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;tar xzf hadoop-0.21.0.tar.gz&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;Check if the installation work fine by launching&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;bin/hadoop version.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;You might get the following error:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;Error: JAVA_HOME is not set.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;To fix the error, you need to export JAVA_HOME that tells the directory of the JDK. Example:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;export JAVA_HOME=/yourjdkdirectory/jdk1.6.0_22&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;When everything is OK you will have something like:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;Hadoop 0.21.0&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;Subversion https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.21 -r 985326&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;Compiled by tomwhite on Tue Aug 17 01:02:28 EDT 2010&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;From source with checksum a1aeb15b4854808d152989ba76f90fac&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;Now, we need to configure ssh that will be used by the pseudo nodes like name nodes, data nodes, and so on. &amp;nbsp;I will not discuss what they are in this blog, you will find them in hadoop site, or I might come back later on the subject in another blog.&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;To install ssh, you can do&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;sudo apt-get install ssh&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;Once installed, we will configure passwordless ssh connection as follow:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;First, generate the key using ssh-keygen&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;ssh-keygen -t rsa -P ""&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;Generating public/private rsa key pair.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;Enter file in which to save the key (/home/arizal/.ssh/id_rsa):&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;Your identification has been saved in /home/arizal/.ssh/id_rsa.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;Your public key has been saved in /home/arizal/.ssh/id_rsa.pub.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;The key fingerprint is:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;Then, enable SSH access to the localhost using the generated key as follow:&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;cat ~/.ssh/id_rsa.pub &amp;gt;&amp;gt; ~/.ssh/authorized_keys&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;Test that the configuration works by launching:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;ssh localhost&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Times New Roman'; margin: 0px;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;If the configuration works, you will not be asked for password.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;We will modify the conf/hadoop-env.sh to add JAVA_HOME to the file.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-size: medium; margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;JAVA_HOME=/yourjdkdirectory/jdk1.6.0_22&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;div style="font-family: 'Times New Roman';"&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;We're almost there, be patient. Just couple of steps before coding. Promise.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Times New Roman';"&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;We need to modify three files conf/core-site.xml, conf/hdfs-site.xml, and conf/mapred-site.xml as follow:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Times New Roman';"&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;lt;!-- core-site.xml --&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp; &amp;lt;property&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;name&amp;gt;fs.default.name&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;value&amp;gt;hdfs://localhost/&amp;lt;/value&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp; &amp;lt;/property&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;lt;!-- hdfs-site.xml --&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp; &amp;lt;property&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;lt;name&amp;gt;dfs.replication&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;lt;value&amp;gt;1&amp;lt;/value&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;nbsp;&amp;nbsp; &amp;lt;/property&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;lt;!-- mapred-site.xml --&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp; &amp;lt;property&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;name&amp;gt;mapred.job.tracker&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;value&amp;gt;localhost:8021&amp;lt;/value&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&amp;nbsp;&amp;nbsp; &amp;lt;/property&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Times New Roman'; font-size: medium; margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;Then, we format HDFS file system. The formatting creates an empty file system.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;bin/hadoop namenode -format&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif; font-size: medium;"&gt;&lt;span class="Apple-style-span"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;When it's OK, the output will look like the following:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;STARTUP_MSG: &amp;nbsp; build = https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.21 -r 985326; compiled by 'tomwhite' on Tue Aug 17 01:02:28 EDT 2010&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;************************************************************/&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10/12/05 21:38:49 INFO namenode.FSNamesystem: defaultReplication = 1&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10/12/05 21:38:49 INFO namenode.FSNamesystem: maxReplication = 512&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10/12/05 21:38:49 INFO namenode.FSNamesystem: minReplication = 1&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10/12/05 21:38:49 INFO namenode.FSNamesystem: maxReplicationStreams = 2&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10/12/05 21:38:49 INFO namenode.FSNamesystem: shouldCheckForEnoughRacks = false&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10/12/05 21:38:49 INFO security.Groups: Group mapping impl=org.apache.hadoop.security.ShellBasedUnixGroupsMapping; cacheTimeout=300000&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10/12/05 21:38:50 INFO namenode.FSNamesystem: fsOwner=arizal&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10/12/05 21:38:50 INFO namenode.FSNamesystem: supergroup=supergroup&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10/12/05 21:38:50 INFO namenode.FSNamesystem: isPermissionEnabled=true&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10/12/05 21:38:50 INFO namenode.FSNamesystem: isAccessTokenEnabled=false accessKeyUpdateInterval=0 min(s), accessTokenLifetime=0 min(s)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10/12/05 21:38:51 INFO common.Storage: Image file of size 112 saved in 0 seconds.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10/12/05 21:38:51 INFO common.Storage: Storage directory /tmp/hadoop-arizal/dfs/name has been successfully formatted.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10/12/05 21:38:51 INFO namenode.NameNode: SHUTDOWN_MSG:&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: 'Courier New',Courier,monospace; font-size: small;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="margin: 0px;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;Perfect. We're almost there. We start everything now. We can use&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;nbsp;bin/start-all.sh&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif;"&gt;but we &amp;nbsp;will get this annoying warning message.&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;This script is Deprecated. Instead use start-dfs.sh and start-mapred.sh.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif; font-size: medium; margin: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;Don't worry, ignoring the warning looks OK. But you can try&amp;nbsp;launching start-dfs.sh or start-mapred.sh you will get something even more annoying error message:&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;Hadoop common not found.&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;The fix for the "Hadoop common not found" can be found in the following JIRA entry:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="margin: 0px;"&gt;&lt;a href="https://issues.apache.org/jira/browse/HADOOP-6953"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;https://issues.apache.org/jira/browse/HADOOP-6953&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;The solution described there solved the problem for me.&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;We can check the running nodes by using the jps command:&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;$jps&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;9530 DataNode&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;9325 NameNode&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10131 Jps&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;9750 SecondaryNameNode&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;10087 TaskTracker&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;9880 JobTracker&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif; font-size: medium;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&amp;nbsp;Finally, to stop all the nodes, we can use&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace; font-size: x-small;"&gt;&amp;nbsp;bin/stop-all.sh&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif; font-size: small;"&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: Arial,Helvetica,sans-serif; font-size: medium; margin: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;OK. That's all about installation, as promised, we're now ready to code.&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Times,'Times New Roman',serif; font-size: large;"&gt;&lt;b&gt;Coding Mapper and Reducer&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;We can code the mapper and reducer now. You can use any IDE you like, I used NetBeans 6.9.1 for this purpose.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;Make sure that hadoop-common-0.21.0.jar and hadoop-mapred-0.21.0.jar are in the project library.&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;Then, code the mapper as follow:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;/**&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;The&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;class&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;that&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;maps&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;an&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;input&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;of&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;a&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;map&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;task&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   4 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;into&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;a&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;set&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;of&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;integer&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;that&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;represents&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;the&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;   5 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;existence&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;of&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;an&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;origin&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;in&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;a&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;line&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;of&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;a&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;HDFS&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   6 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;file&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   7 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   8 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #02b902;"&gt;@author&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;arizal&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   9 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  10 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;public&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;class&lt;/b&gt;&lt;/span&gt; FlightOriginMapper&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  11 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;b&gt;extends&lt;/b&gt;&lt;/span&gt; Mapper&lt;span style="color: black;"&gt;&lt;b&gt;&amp;lt;&lt;/b&gt;&lt;/span&gt;LongWritable, Text, Text, IntWritable&lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  12 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  13 &lt;/span&gt;&lt;/span&gt;  &lt;span style="color: #6600cc;"&gt;/**&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;Maps&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;the&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;first&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;3&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;characters&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;of&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;a&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;line&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;to&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;1&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  14 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;The&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;line&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;PARCDGFRAFRA&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;-&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;02022011&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;/&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;06022011&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;maps&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;to&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;(&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;PAR&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;,&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;1&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  15 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #02b902;"&gt;@param&lt;/span&gt;&lt;span style="color: #02b902;"&gt; &lt;/span&gt;&lt;span style="color: #cc6600;"&gt;key&lt;/span&gt;&lt;span style="color: #02b902;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;the&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;key&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;,&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;it&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;is&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;not&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;used&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;in&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;the&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;mapper&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  16 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #02b902;"&gt;@param&lt;/span&gt;&lt;span style="color: #02b902;"&gt; &lt;/span&gt;&lt;span style="color: #cc6600;"&gt;value&lt;/span&gt;&lt;span style="color: #02b902;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;the&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;value&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;that&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;corresponds&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;to&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;the&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;line&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  17 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;of&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;the&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;file&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  18 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #02b902;"&gt;@param&lt;/span&gt;&lt;span style="color: #02b902;"&gt; &lt;/span&gt;&lt;span style="color: #cc6600;"&gt;context&lt;/span&gt;&lt;span style="color: #02b902;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;the&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;execution&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;context&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  19 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #02b902;"&gt;@throws&lt;/span&gt;&lt;span style="color: #02b902;"&gt; &lt;/span&gt;&lt;span style="color: #cc6600;"&gt;IOException&lt;/span&gt;&lt;span style="color: #02b902;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;the&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;exception&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;thrown&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;on&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;IO&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;problem&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  20 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #02b902;"&gt;@throws&lt;/span&gt;&lt;span style="color: #02b902;"&gt; &lt;/span&gt;&lt;span style="color: #cc6600;"&gt;InterruptedException&lt;/span&gt;&lt;span style="color: #02b902;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;thrown&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;when&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;execution&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;is&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  21 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;interrupted&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  22 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  23 &lt;/span&gt;&lt;/span&gt;  &lt;span style="color: #66ccff;"&gt;&lt;b&gt;@&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #66ccff;"&gt;&lt;b&gt;Override&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  24 &lt;/span&gt;&lt;/span&gt;  &lt;span style="color: #006699;"&gt;&lt;b&gt;public&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #0099ff;"&gt;&lt;b&gt;void&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;map&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;LongWritable key, Text value, Context context&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  25 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;b&gt;throws&lt;/b&gt;&lt;/span&gt; IOException, InterruptedException &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  26 &lt;/span&gt;&lt;/span&gt;    String line &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; value.&lt;span style="color: #9966ff;"&gt;toString&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  27 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #ff8400;"&gt;//&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;discard&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;any&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;line&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;whose&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;length&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;less&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;than&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;3.&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  28 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: #006699;"&gt;&lt;b&gt;if&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;line.&lt;span style="color: #9966ff;"&gt;length&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/span&gt; &lt;span style="color: red;"&gt;3&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt; &lt;span style="color: black;"&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  29 &lt;/span&gt;&lt;/span&gt;        &lt;span style="color: #ff8400;"&gt;//&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;pickup&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;the&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;origin&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  30 &lt;/span&gt;&lt;/span&gt;        String origin &lt;span style="color: black;"&gt;&lt;b&gt;=&lt;/b&gt;&lt;/span&gt; line.&lt;span style="color: #9966ff;"&gt;substring&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;0&lt;/span&gt;, &lt;span style="color: red;"&gt;3&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  31 &lt;/span&gt;&lt;/span&gt;        &lt;span style="color: #ff8400;"&gt;//&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;then&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;provides&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;the&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;output&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;of&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;(origin,&lt;/span&gt;&lt;span style="color: #ff8400;"&gt; &lt;/span&gt;&lt;span style="color: #ff8400;"&gt;1)&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  32 &lt;/span&gt;&lt;/span&gt;        context.&lt;span style="color: #9966ff;"&gt;write&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: #006699;"&gt;&lt;b&gt;new&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;Text&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;origin&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;, &lt;span style="color: #006699;"&gt;&lt;b&gt;new&lt;/b&gt;&lt;/span&gt; &lt;span style="color: #9966ff;"&gt;IntWritable&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;(&lt;/b&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;1&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;)&lt;/b&gt;&lt;/span&gt;;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  33 &lt;/span&gt;&lt;/span&gt;    &lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  34 &lt;/span&gt;&lt;/span&gt;  &lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: #990066;"&gt;  35 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;  36 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;The code for reducer receives the iterator of pair (origin, [ list of integer ] ). The reducer then reduces the pair to (origin, size of list).&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;   1 &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;/**&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   2 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;The&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;reducer&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;that&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;reduces&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;a&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;list&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;of&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;integer&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;corresponding&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #dbdbdb; border-right: 2px solid black; margin-right: 5px;"&gt;&lt;span style="color: black;"&gt;   3 &lt;/span&gt;&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;*&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;to&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;the&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;existence&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;of&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;an&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;origin&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;in&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;a&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;line&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;of&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;"&gt;an&lt;/span&gt;&lt;span style="color: #6600cc;"&gt; &lt;/span&gt;&lt;span style="color: #6600cc;
