<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>99 Problems in Scala</title>
	<atom:link href="http://99problemsinscala.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://99problemsinscala.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Sat, 25 Apr 2009 15:27:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='99problemsinscala.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>99 Problems in Scala</title>
		<link>http://99problemsinscala.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://99problemsinscala.wordpress.com/osd.xml" title="99 Problems in Scala" />
	<atom:link rel='hub' href='http://99problemsinscala.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Enums in Scala</title>
		<link>http://99problemsinscala.wordpress.com/2009/04/25/enums-in-scala/</link>
		<comments>http://99problemsinscala.wordpress.com/2009/04/25/enums-in-scala/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 15:19:36 +0000</pubDate>
		<dc:creator>rbpasker</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://99problemsinscala.wordpress.com/2009/04/25/114/</guid>
		<description><![CDATA[Amazingly enough, no example of how to do this is in either Programming in Scala or on the Scala-lang.org website. there are a few Scala-isms here. First, since there&#8217;s no member variables in State, a trait is more appropriate than a class. Second, by using the keyword &#8220;sealed,&#8221; you specify that no other instances of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=114&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Amazingly enough, no example of how to do this is in either Programming in Scala or on the Scala-lang.org website. </p>
<p>there are a few Scala-isms here.</p>
<p>First, since there&#8217;s no member variables in State, a trait is more appropriate than a class. </p>
<p>Second, by using the keyword &#8220;sealed,&#8221; you specify that no other instances of this trait will be found in any other source file. That means that the code generated for the match expression doesn&#8217;t need an &#8220;default&#8221;, and you are guaranteed that the match will always have a case to choose from.</p>
<p>Lastly, by specifying the case classes as &#8220;object&#8221; you get a singleton subclass for each discrete state.</p>
<p><code>sealed trait State<br />
case object Closed extends State<br />
case object Open extends State<br />
case object Interrupted extends State<br />
</code><HR><code><br />
scala&gt; val theState:State=Open<br />
theState: State = Open</p>
<p>scala&gt;  theState match<br />
      {<br />
      case Open =&gt; println("o")<br />
      case Closed =&gt; println("c")<br />
      case Interrupted =&gt; println("i")<br />
      }<br />
  o</p>
<p></code></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/99problemsinscala.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/99problemsinscala.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/99problemsinscala.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/99problemsinscala.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/99problemsinscala.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/99problemsinscala.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/99problemsinscala.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/99problemsinscala.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/99problemsinscala.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/99problemsinscala.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/99problemsinscala.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/99problemsinscala.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/99problemsinscala.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/99problemsinscala.wordpress.com/114/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=114&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://99problemsinscala.wordpress.com/2009/04/25/enums-in-scala/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/81bdf71e01e9c210fef494b1e23b31f6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rbpasker</media:title>
		</media:content>
	</item>
		<item>
		<title>P11 (*) Modified run-length encoding.</title>
		<link>http://99problemsinscala.wordpress.com/2009/04/20/p11-modified-run-length-encoding/</link>
		<comments>http://99problemsinscala.wordpress.com/2009/04/20/p11-modified-run-length-encoding/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 18:59:15 +0000</pubDate>
		<dc:creator>rbpasker</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://99problemsinscala.wordpress.com/2009/04/20/p11-modified-run-length-encoding/</guid>
		<description><![CDATA[def encodeModified[T] (l :List[T]) : List[Any] = { pack(l).map(sublist =&#62; if (sublist.length==1) sublist.head else (sublist.length, sublist.head)) } &#124; &#124; &#124; &#124; encodeModified: [T](List[T])List[Any] scala&#62; encodeModified(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) res7: List[Any] = List((4,'a), 'b, (2,'c), (2,'a), 'd, (4,'e))<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=113&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre>
 def encodeModified[T] (l :List[T]) : List[Any] = {

    pack(l).map(sublist =&gt; if (sublist.length==1) sublist.head else  (sublist.length, sublist.head))

}
     |      |      |      | encodeModified: [T](List[T])List[Any]

scala&gt; encodeModified(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e))
res7: List[Any] = List((4,'a), 'b, (2,'c), (2,'a), 'd, (4,'e))
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/99problemsinscala.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/99problemsinscala.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/99problemsinscala.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/99problemsinscala.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/99problemsinscala.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/99problemsinscala.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/99problemsinscala.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/99problemsinscala.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/99problemsinscala.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/99problemsinscala.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/99problemsinscala.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/99problemsinscala.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/99problemsinscala.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/99problemsinscala.wordpress.com/113/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=113&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://99problemsinscala.wordpress.com/2009/04/20/p11-modified-run-length-encoding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/81bdf71e01e9c210fef494b1e23b31f6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rbpasker</media:title>
		</media:content>
	</item>
		<item>
		<title>P10 =- Run-length encoding of a list.</title>
		<link>http://99problemsinscala.wordpress.com/2009/04/20/p10-run-length-encoding-of-a-list/</link>
		<comments>http://99problemsinscala.wordpress.com/2009/04/20/p10-run-length-encoding-of-a-list/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 18:41:16 +0000</pubDate>
		<dc:creator>rbpasker</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://99problemsinscala.wordpress.com/2009/04/20/p10-run-length-encoding-of-a-list/</guid>
		<description><![CDATA[scala&#62; def encode[T] (l :List[T]) : List[(Int, T)] = { pack(l).map(sublist =&#62; (sublist.length, sublist.head)) } &#124; &#124; &#124; &#124; encode: [T](List[T])List[(Int, T)] scala&#62; encode(List(&#8216;a, &#8216;a, &#8216;a, &#8216;a, &#8216;b, &#8216;c, &#8216;c, &#8216;a, &#8216;a, &#8216;d, &#8216;e, &#8216;e, &#8216;e, &#8216;e)) res3: List[(Int, Symbol)] = List((4,&#8217;a), (1,&#8217;b), (2,&#8217;c), (2,&#8217;a), (1,&#8217;d), (4,&#8217;e))<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=112&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><PRE><br />
scala&gt; def encode[T] (l :List[T]) : List[(Int, T)] = {</p>
<p>    pack(l).map(sublist =&gt; (sublist.length, sublist.head))</p>
<p>}<br />
     |      |      |      | encode: [T](List[T])List[(Int, T)]</p>
<p>scala&gt; encode(List(&#8216;a, &#8216;a, &#8216;a, &#8216;a, &#8216;b, &#8216;c, &#8216;c, &#8216;a, &#8216;a, &#8216;d, &#8216;e, &#8216;e, &#8216;e, &#8216;e))</p>
<p>res3: List[(Int, Symbol)] = List((4,&#8217;a), (1,&#8217;b), (2,&#8217;c), (2,&#8217;a), (1,&#8217;d), (4,&#8217;e))<br />
</PRE></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/99problemsinscala.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/99problemsinscala.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/99problemsinscala.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/99problemsinscala.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/99problemsinscala.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/99problemsinscala.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/99problemsinscala.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/99problemsinscala.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/99problemsinscala.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/99problemsinscala.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/99problemsinscala.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/99problemsinscala.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/99problemsinscala.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/99problemsinscala.wordpress.com/112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=112&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://99problemsinscala.wordpress.com/2009/04/20/p10-run-length-encoding-of-a-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/81bdf71e01e9c210fef494b1e23b31f6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rbpasker</media:title>
		</media:content>
	</item>
		<item>
		<title>P09 &#8211; Pack consecutive duplicates of list elements into sublists.</title>
		<link>http://99problemsinscala.wordpress.com/2009/04/20/p09-pack-consecutive-duplicates-of-list-elements-into-sublists/</link>
		<comments>http://99problemsinscala.wordpress.com/2009/04/20/p09-pack-consecutive-duplicates-of-list-elements-into-sublists/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 18:15:40 +0000</pubDate>
		<dc:creator>rbpasker</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://99problemsinscala.wordpress.com/?p=110</guid>
		<description><![CDATA[scala&#62; pack(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) res0: List[List[Symbol]] = List(List('a, 'a, 'a, 'a), List('b), List('c, 'c), List('a, 'a), List('d), List('e, 'e, 'e, 'e)) def pack[T](l : List[T]) : List[List[T]] = { def pack2(result : List[List[T]], current : List[T], l : List[T]) : List[List[T]] = l match [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=110&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><code>scala&gt; pack(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e))<br />
res0: List[List[Symbol]] = List(List('a, 'a, 'a, 'a), List('b), List('c, 'c), List('a, 'a), List('d), List('e, 'e, 'e, 'e))</p>
<p>def pack[T](l : List[T]) : List[List[T]] =  {</p>
<p>   def pack2(result : List[List[T]], current : List[T], l : List[T]) : List[List[T]] = l match {<br />
   	case h::t if (current.head == h) =&gt; pack2(result,           h::current, t)<br />
	case h::t                        =&gt; pack2(result + current, List(h),    t)<br />
	case Nil                         =&gt; result + current<br />
   }</p>
<p>   l match {<br />
     case Nil =&gt; Nil<br />
     case head::tail=&gt; pack2(List(), List(head), tail)<br />
   }</p>
<p>}<br />
</code></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/99problemsinscala.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/99problemsinscala.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/99problemsinscala.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/99problemsinscala.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/99problemsinscala.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/99problemsinscala.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/99problemsinscala.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/99problemsinscala.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/99problemsinscala.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/99problemsinscala.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/99problemsinscala.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/99problemsinscala.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/99problemsinscala.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/99problemsinscala.wordpress.com/110/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=110&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://99problemsinscala.wordpress.com/2009/04/20/p09-pack-consecutive-duplicates-of-list-elements-into-sublists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/81bdf71e01e9c210fef494b1e23b31f6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rbpasker</media:title>
		</media:content>
	</item>
		<item>
		<title>P08 &#8211; Eliminate consecutive duplicates of list elements.</title>
		<link>http://99problemsinscala.wordpress.com/2009/04/19/p08-eliminate-consecutive-duplicates-of-list-elements/</link>
		<comments>http://99problemsinscala.wordpress.com/2009/04/19/p08-eliminate-consecutive-duplicates-of-list-elements/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 18:11:22 +0000</pubDate>
		<dc:creator>rbpasker</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://99problemsinscala.wordpress.com/?p=108</guid>
		<description><![CDATA[scala&#62; compress(List(&#8216;a, &#8216;a, &#8216;a, &#8216;a, &#8216;b, &#8216;c, &#8216;c, &#8216;a, &#8216;a, &#8216;d, &#8216;e, &#8216;e, &#8216;e, &#8216;e)) res0: List[Symbol] = List(&#8216;a, &#8216;b, &#8216;c, &#8216;a, &#8216;d, &#8216;e) def compress[T](l : List[T]) : List[T] = l match { case head::next::tail if (head == next) =&#62; compress(next::tail) case head::tail =&#62; head::compress(tail) case nil =&#62; List() }<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=108&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>scala&gt; compress(List(&#8216;a, &#8216;a, &#8216;a, &#8216;a, &#8216;b, &#8216;c, &#8216;c, &#8216;a, &#8216;a, &#8216;d, &#8216;e, &#8216;e, &#8216;e, &#8216;e))<br />
res0: List[Symbol] = List(&#8216;a, &#8216;b, &#8216;c, &#8216;a, &#8216;d, &#8216;e)</p>
<p>def compress[T](l : List[T]) : List[T] = l match {<br />
    case head::next::tail if (head == next) =&gt; compress(next::tail)<br />
    case head::tail =&gt; head::compress(tail)<br />
    case nil =&gt; List()<br />
}</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/99problemsinscala.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/99problemsinscala.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/99problemsinscala.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/99problemsinscala.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/99problemsinscala.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/99problemsinscala.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/99problemsinscala.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/99problemsinscala.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/99problemsinscala.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/99problemsinscala.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/99problemsinscala.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/99problemsinscala.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/99problemsinscala.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/99problemsinscala.wordpress.com/108/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=108&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://99problemsinscala.wordpress.com/2009/04/19/p08-eliminate-consecutive-duplicates-of-list-elements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/81bdf71e01e9c210fef494b1e23b31f6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rbpasker</media:title>
		</media:content>
	</item>
		<item>
		<title>P07 — Flatten a nested list structure.</title>
		<link>http://99problemsinscala.wordpress.com/2009/04/19/p07-%e2%80%94-flatten-a-nested-list-structure/</link>
		<comments>http://99problemsinscala.wordpress.com/2009/04/19/p07-%e2%80%94-flatten-a-nested-list-structure/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 02:46:51 +0000</pubDate>
		<dc:creator>rbpasker</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://99problemsinscala.wordpress.com/?p=102</guid>
		<description><![CDATA[here&#8217;s the example: scala&#62; flatten(List(List(1, 1), 2, List(3, List(5, 8)))) res0: List[Any] = List(1, 1, 2, 3, 5, This tells me that the the List head could be itself a List that needs to be flattened, so I have to differentiate between a head that is a List and a head that is not a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=102&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>here&#8217;s the example:<br />
<code><br />
scala&gt; flatten(List(List(1, 1), 2, List(3, List(5, 8))))<br />
res0: List[Any] = List(1, 1, 2, 3, 5, <img src='http://s0.wp.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /><br />
</code></p>
<p>This tells me that the the List head could be itself a List that needs to be flattened, so I have to differentiate between a head that is a List and a head that is not a List. </p>
<p><code>def flatten[_](l: List[_]) : List[_] = l match {<br />
    case (head: List[_])::Nil  =&gt; flatten(head)<br />
    case (head: List[_])::tail =&gt; flatten(head):::flatten(tail)<br />
    case head::Nil             =&gt; List(head)<br />
    case head::tail            =&gt; head::flatten(tail)<br />
    case Nil =&gt; Nil<br />
}<br />
</code></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/99problemsinscala.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/99problemsinscala.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/99problemsinscala.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/99problemsinscala.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/99problemsinscala.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/99problemsinscala.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/99problemsinscala.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/99problemsinscala.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/99problemsinscala.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/99problemsinscala.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/99problemsinscala.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/99problemsinscala.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/99problemsinscala.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/99problemsinscala.wordpress.com/102/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=102&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://99problemsinscala.wordpress.com/2009/04/19/p07-%e2%80%94-flatten-a-nested-list-structure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/81bdf71e01e9c210fef494b1e23b31f6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rbpasker</media:title>
		</media:content>
	</item>
		<item>
		<title>P06 &#8211; Find out whether a list is a palindrome.</title>
		<link>http://99problemsinscala.wordpress.com/2009/04/16/p06-find-out-whether-a-list-is-a-palindrome/</link>
		<comments>http://99problemsinscala.wordpress.com/2009/04/16/p06-find-out-whether-a-list-is-a-palindrome/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 22:50:16 +0000</pubDate>
		<dc:creator>rbpasker</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://99problemsinscala.wordpress.com/?p=97</guid>
		<description><![CDATA[Another easy one, we just use reverse()! scala&#62; def isPalindrome[T](l: List[T]) : Boolean = l match { case Nil =&#62; true case _::Nil =&#62; true case h::t =&#62; (h == t.reverse.head) } isPalindrome: [T](List[T])Boolean scala&#62; isPalindrome(List(1, 2, 3, 2, 1)) res29: Boolean = true scala&#62; isPalindrome(List(1, 2, 3, 2, 2)) res30: Boolean = false scala&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=97&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Another easy one, we just use reverse()!</p>
<p><code>scala&gt; def isPalindrome[T](l: List[T]) : Boolean = l match {<br />
    case Nil =&gt; true<br />
    case _::Nil =&gt; true<br />
    case h::t =&gt; (h == t.reverse.head)<br />
}<br />
 isPalindrome: [T](List[T])Boolean</p>
<p>scala&gt; isPalindrome(List(1, 2, 3, 2, 1))<br />
res29: Boolean = true</p>
<p>scala&gt; isPalindrome(List(1, 2, 3, 2, 2))<br />
res30: Boolean = false</p>
<p>scala&gt; isPalindrome(List())<br />
res31: Boolean = true</p>
<p>scala&gt; isPalindrome(List(1,2,1))<br />
res32: Boolean = true</p>
<p>scala&gt; isPalindrome(List(1,2,2))<br />
res33: Boolean = false<br />
</code></p>
<p>And <a href="http://aperiodic.net/phil/scala/s-99/p06.scala">their answer</a> is:<br />
<code><br />
def isPalindrome[T](l: List[T]): Boolean = l == l.reverse<br />
</code></p>
<p>Which tells me i should have looked to see if List has a equality method, which it does: <a href="http://www.scala-lang.org/docu/files/api/scala/AnyRef.html#equals%28Any%29"><code>List.equals</code></a>. But what we see here is not <code>.equals</code> but <code>.==</code>.</p>
<p>Scala departs from Java&#8217;s idea of equality by <a href="http://www.scala-lang.org/docu/files/api/scala/AnyRef.html#%3D%3D%28AnyRef%29">defining <code>==</code></a> not as reference equality (a==b iff a and b are references to the identical object), but as a synonym for <code>.equals</code>!</p>
<p>The Java behavior of reference equality has been moved to the <a href="http://www.scala-lang.org/docu/files/api/scala/AnyRef.html#eq%28AnyRef%29"><code>.eq</code> method</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/99problemsinscala.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/99problemsinscala.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/99problemsinscala.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/99problemsinscala.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/99problemsinscala.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/99problemsinscala.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/99problemsinscala.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/99problemsinscala.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/99problemsinscala.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/99problemsinscala.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/99problemsinscala.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/99problemsinscala.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/99problemsinscala.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/99problemsinscala.wordpress.com/97/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=97&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://99problemsinscala.wordpress.com/2009/04/16/p06-find-out-whether-a-list-is-a-palindrome/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/81bdf71e01e9c210fef494b1e23b31f6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rbpasker</media:title>
		</media:content>
	</item>
		<item>
		<title>P05 — Reverse a list</title>
		<link>http://99problemsinscala.wordpress.com/2009/04/16/p05-%e2%80%94-reverse-a-list/</link>
		<comments>http://99problemsinscala.wordpress.com/2009/04/16/p05-%e2%80%94-reverse-a-list/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 21:25:45 +0000</pubDate>
		<dc:creator>rbpasker</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://99problemsinscala.wordpress.com/?p=80</guid>
		<description><![CDATA[Should be pretty simple: scala&#62; def reverse[T](l: List[T]) : List[T] = l match { &#124; case Nil =&#62; Nil &#124; case h::Nil =&#62; h &#124; case h::t =&#62; reverse(t)::h &#124; } :7: error: value :: is not a member of T case h::t =&#62; reverse(t)::h ^ Hmmm, :: isn&#8217;t the List append operator. Lets try [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=80&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Should be pretty simple:</p>
<p><code>scala&gt; def reverse[T](l: List[T]) : List[T] = l match {<br />
     | case Nil =&gt; Nil<br />
     | case h::Nil =&gt; h<br />
     | case h::t   =&gt; reverse(t)::h<br />
     | }<br />
:7: error: value :: is not a member of T<br />
       case h::t   =&gt; reverse(t)::h<br />
                                ^</code></p>
<p>Hmmm, <code>::</code> isn&#8217;t the List append operator. Lets try <code>+</code></p>
<p><code>scala&gt;  def reverse[T](l: List[T]) : List[T] = l match {<br />
      case Nil =&gt; Nil<br />
      case h::Nil =&gt; List(h)<br />
      case h::t   =&gt; reverse(t)+h<br />
      }</p>
<p>     |      |      |      | warning: there were deprecation warnings; re-run with -deprecation for details<br />
reverse: [T](List[T])List[T]</p>
<p>scala&gt; reverse(List())<br />
res13: List[Nothing] = List()</p>
<p>scala&gt; reverse(List(1))<br />
res14: List[Int] = List(1)</p>
<p>scala&gt; reverse(List(1,2,3,4,5,6))<br />
res15: List[Int] = List(6, 5, 4, 3, 2, 1)</p>
<p></code></p>
<p>Voilá, but what about the deprecation warnings?</p>
<p>It looks like <a href="http://www.scala-lang.org/docu/files/api/scala/List.html#%2B%28B%29"><CODE>+</CODE> was deprecated</a>, so I again asked on the Scala IRC channel about it, but nobody seemed to have a definitive answer why it was deprecated. Someone did suggestion using <code>:::</code> instead, and cautioned that it associates right-to-left. Normally, when you see <code>a+b</code> it means <code>a.+(b)</code>, which is like saying <code>a.add(b)</code> in Java. That&#8217;s because operators are left associative, meaning they are a function of the parameter on the left. Being right-associative, <code>:::</code> associates with the parameter with on the right, so <CODE>a:::b</code> means b.:::(a), which you might read as <code>b.append(a)</code>, if you were reading Java. It took a bit of searching in the <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf">Scala Language Reference</a> to find out why:</p>
<blockquote><p>
The associativity of an operator is determined by the operator’s last character. Operators ending in a colon ‘:’ are right-associative. All other operators are left-associative.  (6.12.3 Inﬁx Operations)
</p></blockquote>
<p>So, in fact <code>a:::b</code> is not the "append" operation on <code>a</code>, but the "prepend" operation on <code>b</code>. <a href="http://www.scala-lang.org/docu/files/api/scala/List.html#%3A%3A%28B%29">This is also true for <code>::</code></a>.  </p>
<p>Note that <code>+</code> takes a <code>List[T]</code> and appends an element of type <code>T</code>, but <code>:::</code> tales two parameters of type <code>List[T]</code>, so we'll have to wrap the matched head element in a List.</p>
<p>So let's try changing <code>+</code> to <code>:::</code>, and at the time see if we can't shorten up our solution, since <code>reverse(t)</code>, when <code>t</code> is <code>Nil</code>, is still <code>Nil</code>.</p>
<p><code>scala&gt;  def reverse[T](l: List[T]) : List[T] = l match {<br />
      case Nil =&gt; Nil<br />
      case h::t   =&gt; reverse(t):::List(h)<br />
      }</p>
<p> reverse: [T](List[T])List[T]</p>
<p>scala&gt; reverse(List(1,2,3,4,5,6,7))<br />
res24: List[Int] = List(7, 6, 5, 4, 3, 2, 1)</p>
<p>scala&gt; reverse(List(1))<br />
res25: List[Int] = List(1)</p>
<p>scala&gt; reverse(List())<br />
res26: List[Nothing] = List()<br />
</code></p>
<p>Very nice.  </p>
<p><a href="http://aperiodic.net/phil/scala/s-99/p05.scala">The first answer</a> is identical to our own, but there are two more answers. Answer two is the "tail recursive" version <a href="http://99problemsinscala.wordpress.com/2009/04/16/p04-–-find-the-number-of-elements-of-a-list/">as per the previous post</a>.</p>
<p>The third answer, however, is labelled as "Pure functional":</p>
<p><code><br />
def reverse[T](l: List[T]): List[T] = l.foldLeft(List[T]())((r, h) =&gt; h :: r)<br />
</code></p>
<p>Wow, I have no idea how to read that! </p>
<p>AND, I thought my answers were functional, but they are not functional enough! </p>
<p>According to the docs, <a href="http://www.scala-lang.org/docu/files/api/scala/List.html#foldLeft%28B%29">foldLeft is defined</a> as</p>
<blockquote><p>
Combines the elements of this list together using the binary function f, from left to right, and starting with the value z.
</p></blockquote>
<p>I take this to mean that if you have <code>List(a, b, c, d)</code> and the binary function <code>+</code>, then fold left means <code>((a+b)+c)+d)</code>. </p>
<p>So <code>List[T]()</code> is the type of the binary function which we want to use, and it returns a <code>List[T]</code>. At the end of the def is <code>((r, h) =&gt; h :: r)</code>, which is the definition of the binary function. Since <code>r</code> and <code>h</code> are swapped and appended, lets call this function <em>swap</em>.  So folding left with a swap on List(a,b,c,d), we get:<br />
<code><br />
((a swap b) swap c) swap d)<br />
((b, a) swap c) swap d)<br />
((c, b, a) swap d)<br />
(d, c, b, a)<br />
</code></p>
<p>And again we have a reversed List!</p>
<p>So even though it was an easy exercise, we learned that a trailing colon on an operator means it associates right, and we learned how to pass functions to other functions!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/99problemsinscala.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/99problemsinscala.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/99problemsinscala.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/99problemsinscala.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/99problemsinscala.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/99problemsinscala.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/99problemsinscala.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/99problemsinscala.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/99problemsinscala.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/99problemsinscala.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/99problemsinscala.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/99problemsinscala.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/99problemsinscala.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/99problemsinscala.wordpress.com/80/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=80&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://99problemsinscala.wordpress.com/2009/04/16/p05-%e2%80%94-reverse-a-list/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/81bdf71e01e9c210fef494b1e23b31f6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rbpasker</media:title>
		</media:content>
	</item>
		<item>
		<title>P04 – Find the number of elements of a list</title>
		<link>http://99problemsinscala.wordpress.com/2009/04/16/p04-%e2%80%93-find-the-number-of-elements-of-a-list/</link>
		<comments>http://99problemsinscala.wordpress.com/2009/04/16/p04-%e2%80%93-find-the-number-of-elements-of-a-list/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 17:30:30 +0000</pubDate>
		<dc:creator>rbpasker</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://99problemsinscala.wordpress.com/?p=76</guid>
		<description><![CDATA[This one should be pretty easy, since we already know from the last problem how to get to the nth element. scala&#62; def length[T](l: List[T]) : Int = l match { &#124; case Nil =&#62; 0 &#124; case e::Nil =&#62; 1 &#124; case _::t =&#62; 1 + length(t) &#124; } length: [T](List[T])Int scala&#62; length(List()) res7: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=76&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This one should be pretty easy, since we already know from the last problem how to get to the nth element.</p>
<p><code><br />
scala&gt; def length[T](l: List[T]) : Int = l match {<br />
     | case Nil =&gt; 0<br />
     | case e::Nil =&gt; 1<br />
     | case _::t =&gt; 1 + length(t)<br />
     | }<br />
length: [T](List[T])Int</p>
<p>scala&gt; length(List())<br />
res7: Int = 0</p>
<p>scala&gt; length(List(1))<br />
res8: Int = 1</p>
<p>scala&gt; length(List(1,2,3,4,5,6,7,8))<br />
res10: Int = 8<br />
</code></p>
<p>There are <a href="http://aperiodic.net/phil/scala/s-99/p04.scala">two functional answers</a> to this problem.</p>
<p>The first is a &#8220;simple recursive&#8221; solution:</p>
<p><code><br />
def length[T](l: List[T]): Int = l match {<br />
    case Nil       =&gt; 0<br />
    case _ :: tail =&gt; 1 + length(tail)<br />
  }<br />
</code></p>
<p>I see now that I didn&#8217;t need the middle case case <code>e::Nil =&gt; 1</code>, because the last case would have properly copmuted <code>length(Nil)</code> as 0.</p>
<p>The second answer, however, is more interesting and also somewhat disappointing. It says<br />
<blockquote> Unfortunately, the JVM doesn&#8217;t do tail-call elimination in the general case.  Scala *will* do it if the method is either final or is a local function.  </p></blockquote>
<p>Which means that all the work I&#8217;ve been doing to get out of the imperative (iterative) style, as per P01&#8242;s admonition about the &#8220;functional approach&#8221; which uses recursion, was a nice exercise, but frankly nearly futile in Scala because these functions, given a large enough List, will blow the stack. Luckily their answer to P04 links to a blog with <a href="http://blog.richdougherty.com/2009/04/tail-calls-tailrec-and-trampolines.html">a way to get tail recursion in Scala</a>, and frankly, its not pretty. It involves some special maneuvers which you can read about there.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/99problemsinscala.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/99problemsinscala.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/99problemsinscala.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/99problemsinscala.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/99problemsinscala.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/99problemsinscala.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/99problemsinscala.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/99problemsinscala.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/99problemsinscala.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/99problemsinscala.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/99problemsinscala.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/99problemsinscala.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/99problemsinscala.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/99problemsinscala.wordpress.com/76/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=76&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://99problemsinscala.wordpress.com/2009/04/16/p04-%e2%80%93-find-the-number-of-elements-of-a-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/81bdf71e01e9c210fef494b1e23b31f6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rbpasker</media:title>
		</media:content>
	</item>
		<item>
		<title>P03 – Find the Kth element of a list</title>
		<link>http://99problemsinscala.wordpress.com/2009/04/16/p03-%e2%80%93-find-the-kth-element-of-a-list/</link>
		<comments>http://99problemsinscala.wordpress.com/2009/04/16/p03-%e2%80%93-find-the-kth-element-of-a-list/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 08:38:09 +0000</pubDate>
		<dc:creator>rbpasker</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://99problemsinscala.wordpress.com/?p=50</guid>
		<description><![CDATA[This problem takes me back to the Peano Axioms, which I learned in high school. The 5th and 6th axiom state (recursively): 5) 0 is a natural number. 6) For every natural number n, S(n) is a natural number, where S() is a successor function. Which means that S(0) is 1, and S(S(0)) is 2, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=50&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This problem takes me back to the <a href="http://en.wikipedia.org/wiki/Peano_axioms">Peano Axioms</a>, which I learned in high school. The 5th and 6th axiom state (recursively):</p>
<blockquote><p>5) 0 is a natural number.<br />
6) For every natural number n, S(n) is a natural number, where S() is a successor function.
</p></blockquote>
<p>Which means that S(0) is 1, and S(S(0)) is 2, etc.</p>
<p>Therefore, the kth element must be that element which is found at k recursions of <code>_::tail</code>, or a NoSuchElementException, if there are too few elements.</p>
<p>So instead of matching and recursing on <code>l</code>, we&#8217;ll match and recurse on <code>k</code>:</p>
<p><PRE><br />
scala&gt; def nth[T](n:Int, l:List[T]) : T = n match {<br />
      case 0 =&gt; l::_<br />
      case &gt;0 =&gt; nth(n-1,l.tail)<br />
:3: error: &#8216;=&gt;&#8217; expected but integer literal found.<br />
       case &gt;0 =&gt; nth(n-1,l.tail)<br />
             ^<br />
</PRE></p>
<p>Ooops. I can&#8217;t just say <code>&gt;0</code> to match k&gt;0.</p>
<p>I would like to say I figure out how to put an inequality into a case on my own, but after a fair bit of research I couldn&#8217;t figure out how to do it, so I asked on the Scala IRC channel&#8221; (irc://irc.freenode.org/scala) channel how express trichotomy in a match and got this answer:</p>
<p><code>case 0 =&gt; e;case n if n &lt; f;case n if n &gt; 0 =&gt; g</code></p>
<p>Section 8.4 of the <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf">Scala reference manual</a> says that a case expression can have an optional &#8220;Guard&#8221;, which is an expression, after the pattern match.</p>
<p>OK, so I would have to go back to matching on the List, and add a guard:</p>
<p><code>scala&gt; def nth[T](n:Int, l:List[T]) : T = l match {<br />
      case e::t if n==0 =&gt; e<br />
      case e::t if n&gt;0 =&gt; nth(n-1,t)<br />
      case _ =&gt; throw new NoSuchElementException<br />
      }<br />
nth: [T](Int,List[T])T<br />
</code></p>
<p>The first case (n==0) means that we have reached the nth element, and so return the head of the list. Also, my match <code>e::t</code> should have used a wildcard <code>e::_</code> instead because the tail is thrown away in the action.</p>
<p>The second case means we have &#8216;n&#8217; more elements to go, so recurse with n=n-1 and remove the head element.  Again, I should have used a wildcard <code>_::t</code> because the head is not used.</p>
<p>The last case matches anything else, which means n&lt;0, so there must not have been enough elements in the list. </p>
<p>Lets see how it worked:<br />
<code><br />
scala&gt; nth(2, List(1, 1, 2, 3, 5, 8))<br />
res5: Int = 2</p>
<p>scala&gt; nth(4, List(1, 1, 2, 3, 5, 8))<br />
res6: Int = 5</p>
<p>scala&gt; nth(22, List(1, 1, 2, 3, 5, 8))<br />
java.util.NoSuchElementException</p>
<p>scala&gt; nth(0,List())<br />
java.util.NoSuchElementException<br />
</code></p>
<p>Pretty good!</p>
<p>Now lets look at <a href="http://aperiodic.net/phil/scala/s-99/p03.scala">the &#8220;functional&#8221; answer</a> to Problem 3:</p>
<p><code>  def nth[T](n: Int, l: List[T]): T = (n, l) match {<br />
    case (0, e :: _   ) =&gt; e<br />
    case (n, _ :: tail) =&gt; nth(n - 1, tail)<br />
    case (_, Nil      ) =&gt; throw new NoSuchElementException<br />
  }<br />
</code></p>
<p>Interesting! They matched a List consisting of (n, l), and instead of checking to see if k was &lt;0, they checked for an empty list (in the 3d case).</p>
<p>So, from my answer, I learned about guard expressions in pattern matches, and from their answer, I learned that you can cons up a List, and match against multiple values.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/99problemsinscala.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/99problemsinscala.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/99problemsinscala.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/99problemsinscala.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/99problemsinscala.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/99problemsinscala.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/99problemsinscala.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/99problemsinscala.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/99problemsinscala.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/99problemsinscala.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/99problemsinscala.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/99problemsinscala.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/99problemsinscala.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/99problemsinscala.wordpress.com/50/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=99problemsinscala.wordpress.com&amp;blog=7376161&amp;post=50&amp;subd=99problemsinscala&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://99problemsinscala.wordpress.com/2009/04/16/p03-%e2%80%93-find-the-kth-element-of-a-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/81bdf71e01e9c210fef494b1e23b31f6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rbpasker</media:title>
		</media:content>
	</item>
	</channel>
</rss>
