<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: The Ted Neward F# Folding Challenge</title>
	<atom:link href="http://richardminerich.com/2010/04/the-ted-neward-f-folding-challenge/feed/" rel="self" type="application/rss+xml" />
	<link>http://richardminerich.com/2010/04/the-ted-neward-f-folding-challenge/</link>
	<description>A programmer&#039;s chronicle of insights and discoveries</description>
	<lastBuildDate>Sun, 05 Feb 2012 02:20:05 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Richard Minerich</title>
		<link>http://richardminerich.com/2010/04/the-ted-neward-f-folding-challenge/comment-page-1/#comment-471</link>
		<dc:creator>Richard Minerich</dc:creator>
		<pubDate>Tue, 27 Sep 2011 23:46:09 +0000</pubDate>
		<guid isPermaLink="false">http://richardminerich.com/?p=76#comment-471</guid>
		<description>Very nice!  Even more valuable would be a step-by-step explanation of your logical reasoning when writing that.</description>
		<content:encoded><![CDATA[<p>Very nice!  Even more valuable would be a step-by-step explanation of your logical reasoning when writing that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Klein</title>
		<link>http://richardminerich.com/2010/04/the-ted-neward-f-folding-challenge/comment-page-1/#comment-468</link>
		<dc:creator>David Klein</dc:creator>
		<pubDate>Mon, 26 Sep 2011 09:39:38 +0000</pubDate>
		<guid isPermaLink="false">http://richardminerich.com/?p=76#comment-468</guid>
		<description>@Blake, your solution is the best listed here, concise and clear, thanks!</description>
		<content:encoded><![CDATA[<p>@Blake, your solution is the best listed here, concise and clear, thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Blake Hegerle</title>
		<link>http://richardminerich.com/2010/04/the-ted-neward-f-folding-challenge/comment-page-1/#comment-442</link>
		<dc:creator>Blake Hegerle</dc:creator>
		<pubDate>Wed, 22 Jun 2011 18:23:33 +0000</pubDate>
		<guid isPermaLink="false">http://richardminerich.com/?p=76#comment-442</guid>
		<description>Here&#039;s a solution that I think is more idiomatic of functional programming, with an added benefit of being slightly shorter and possibly easier to understand.

let rle list = 
    let push n list =
        match list with
        &#124; c :: m :: t when m=n -&gt; (c+1)::m::t
        &#124; t -&gt; 1::n::t
    List.foldBack push list []</description>
		<content:encoded><![CDATA[<p>Here&#8217;s a solution that I think is more idiomatic of functional programming, with an added benefit of being slightly shorter and possibly easier to understand.</p>
<p>let rle list =<br />
    let push n list =<br />
        match list with<br />
        | c :: m :: t when m=n -&gt; (c+1)::m::t<br />
        | t -&gt; 1::n::t<br />
    List.foldBack push list []</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: The Road to Functional Programming in F# &#8211; From Imperative to Computation Expressions &#171; Inviting Epiphany</title>
		<link>http://richardminerich.com/2010/04/the-ted-neward-f-folding-challenge/comment-page-1/#comment-360</link>
		<dc:creator>The Road to Functional Programming in F# &#8211; From Imperative to Computation Expressions &#171; Inviting Epiphany</dc:creator>
		<pubDate>Tue, 08 Feb 2011 21:56:20 +0000</pubDate>
		<guid isPermaLink="false">http://richardminerich.com/?p=76#comment-360</guid>
		<description>[...] route when generating sequence values by using the Seq.init function.  As I discussed in my Ted Neward&#8217;s Folding Challenge post, the contents of the folding function can be directly mapped to the imperative and recursive [...]</description>
		<content:encoded><![CDATA[<p>[...] route when generating sequence values by using the Seq.init function.  As I discussed in my Ted Neward&#8217;s Folding Challenge post, the contents of the folding function can be directly mapped to the imperative and recursive [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon-Anders Teigen</title>
		<link>http://richardminerich.com/2010/04/the-ted-neward-f-folding-challenge/comment-page-1/#comment-259</link>
		<dc:creator>Jon-Anders Teigen</dc:creator>
		<pubDate>Mon, 08 Nov 2010 07:39:59 +0000</pubDate>
		<guid isPermaLink="false">http://richardminerich.com/?p=76#comment-259</guid>
		<description>// scala

def fold(list:List[Int]) = list.foldRight(List[Int]()){ (e, a) =&gt; a match {
    case num :: `e` :: rest =&gt; num + 1 :: e :: rest
    case _ =&gt; 1 :: e :: a
  }
}

def reconstruct(list:List[Int]):List[Int] = list match {
  case Nil =&gt; Nil
  case num :: e :: rest =&gt; Nil.padTo(num, e) ::: reconstruct(rest)
}


val challenge1 = List(4, 5, 5, 5, 3, 3)
val result1 = List(1, 4, 3, 5, 2, 3)
val challenge2 = List(4, 5 , 4)
val result2 = List(1, 4, 1, 5, 1, 4)

fold(challenge1) == result1
fold(challenge2) == result2

reconstruct(result1) == challenge1
reconstruct(result2) == challenge2</description>
		<content:encoded><![CDATA[<p>// scala</p>
<p>def fold(list:List[Int]) = list.foldRight(List[Int]()){ (e, a) =&gt; a match {<br />
    case num :: `e` :: rest =&gt; num + 1 :: e :: rest<br />
    case _ =&gt; 1 :: e :: a<br />
  }<br />
}</p>
<p>def reconstruct(list:List[Int]):List[Int] = list match {<br />
  case Nil =&gt; Nil<br />
  case num :: e :: rest =&gt; Nil.padTo(num, e) ::: reconstruct(rest)<br />
}</p>
<p>val challenge1 = List(4, 5, 5, 5, 3, 3)<br />
val result1 = List(1, 4, 3, 5, 2, 3)<br />
val challenge2 = List(4, 5 , 4)<br />
val result2 = List(1, 4, 1, 5, 1, 4)</p>
<p>fold(challenge1) == result1<br />
fold(challenge2) == result2</p>
<p>reconstruct(result1) == challenge1<br />
reconstruct(result2) == challenge2</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Boisvert</title>
		<link>http://richardminerich.com/2010/04/the-ted-neward-f-folding-challenge/comment-page-1/#comment-258</link>
		<dc:creator>Alex Boisvert</dc:creator>
		<pubDate>Mon, 08 Nov 2010 06:20:38 +0000</pubDate>
		<guid isPermaLink="false">http://richardminerich.com/?p=76#comment-258</guid>
		<description>Hi Eric, your one-liner is unfortunately incorrect since &#039;groupBy&#039; produces groups irrespective of element sequencing.

Here&#039;s another Scala variation, similar to Tim Dalton&#039;s above,

def compress[T](l: List[T]) = l.foldLeft(List[(T, Int)]()) {  
  case (Nil, x) =&gt; (x, 1) :: Nil
  case ((y, c) :: r, x) if (x == y) =&gt; (x, c+1) :: r
  case (l, x) =&gt; (x, 1) :: l
}.reverse

scala&gt; compress(List(4,5,5,5,3,3))
res0: List[(Int, Int)] = List((4,1), (5,3), (3,2))</description>
		<content:encoded><![CDATA[<p>Hi Eric, your one-liner is unfortunately incorrect since &#8216;groupBy&#8217; produces groups irrespective of element sequencing.</p>
<p>Here&#8217;s another Scala variation, similar to Tim Dalton&#8217;s above,</p>
<p>def compress[T](l: List[T]) = l.foldLeft(List[(T, Int)]()) {<br />
  case (Nil, x) =&gt; (x, 1) :: Nil<br />
  case ((y, c) :: r, x) if (x == y) =&gt; (x, c+1) :: r<br />
  case (l, x) =&gt; (x, 1) :: l<br />
}.reverse</p>
<p>scala&gt; compress(List(4,5,5,5,3,3))<br />
res0: List[(Int, Int)] = List((4,1), (5,3), (3,2))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric</title>
		<link>http://richardminerich.com/2010/04/the-ted-neward-f-folding-challenge/comment-page-1/#comment-257</link>
		<dc:creator>Eric</dc:creator>
		<pubDate>Mon, 08 Nov 2010 04:55:11 +0000</pubDate>
		<guid isPermaLink="false">http://richardminerich.com/?p=76#comment-257</guid>
		<description>Here is a one-liner in Scala:

Seq(4,5,5,5,3,3) groupBy identity flatMap { case (n, l) =&gt; Seq(l.size, n) }
=&gt; List(1, 4, 2, 3, 3, 5)</description>
		<content:encoded><![CDATA[<p>Here is a one-liner in Scala:</p>
<p>Seq(4,5,5,5,3,3) groupBy identity flatMap { case (n, l) =&gt; Seq(l.size, n) }<br />
=&gt; List(1, 4, 2, 3, 3, 5)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JadeNB</title>
		<link>http://richardminerich.com/2010/04/the-ted-neward-f-folding-challenge/comment-page-1/#comment-159</link>
		<dc:creator>JadeNB</dc:creator>
		<pubDate>Wed, 14 Jul 2010 22:58:35 +0000</pubDate>
		<guid isPermaLink="false">http://richardminerich.com/?p=76#comment-159</guid>
		<description>Here&#039;s another Haskell solution, this time without &lt;code&gt;group&lt;/code&gt;.  It&#039;s a bit naughty, because it does a lot of list con- and destruction.
&lt;code&gt;
foldr (
        \(a, b) abs -&gt; a:b:abs
    ) [] . foldr (
        \a ps -&gt;
            case ps of
                (n, a&#039;):rest &#124; a == a&#039; -&gt; (n + 1, a):rest
                otherwise              -&gt; (1, a):ps
    ) []
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Here&#8217;s another Haskell solution, this time without <code>group</code>.  It&#8217;s a bit naughty, because it does a lot of list con- and destruction.<br />
<code><br />
foldr (<br />
        \(a, b) abs -&gt; a:b:abs<br />
    ) [] . foldr (<br />
        \a ps -&gt;<br />
            case ps of<br />
                (n, a'):rest | a == a' -&gt; (n + 1, a):rest<br />
                otherwise              -&gt; (1, a):ps<br />
    ) []<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: prakash</title>
		<link>http://richardminerich.com/2010/04/the-ted-neward-f-folding-challenge/comment-page-1/#comment-102</link>
		<dc:creator>prakash</dc:creator>
		<pubDate>Fri, 14 May 2010 14:25:24 +0000</pubDate>
		<guid isPermaLink="false">http://richardminerich.com/?p=76#comment-102</guid>
		<description>It&#039;s been pointed to me that using stateful variable like $count isn&#039;t functional. Here&#039;s another approach (hopefully functional enough) in Perl 6:

(
    reduce {
        $^a.elems
            ?? $^b != $^a[*-1]
                ?? [ $^a[*], 1, $^b ]
                !! [ $^a[0..$^a.elems-3], $^a[*-2]+1, $^b ]
            !! [ 1, $^b ]
        }, [], 4, 5, 5, 5, 3, 3
).perl.say;</description>
		<content:encoded><![CDATA[<p>It&#8217;s been pointed to me that using stateful variable like $count isn&#8217;t functional. Here&#8217;s another approach (hopefully functional enough) in Perl 6:</p>
<p>(<br />
    reduce {<br />
        $^a.elems<br />
            ?? $^b != $^a[*-1]<br />
                ?? [ $^a[*], 1, $^b ]<br />
                !! [ $^a[0..$^a.elems-3], $^a[*-2]+1, $^b ]<br />
            !! [ 1, $^b ]<br />
        }, [], 4, 5, 5, 5, 3, 3<br />
).perl.say;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard Minerich</title>
		<link>http://richardminerich.com/2010/04/the-ted-neward-f-folding-challenge/comment-page-1/#comment-101</link>
		<dc:creator>Richard Minerich</dc:creator>
		<pubDate>Wed, 12 May 2010 18:50:35 +0000</pubDate>
		<guid isPermaLink="false">http://richardminerich.com/?p=76#comment-101</guid>
		<description>Mission successful.</description>
		<content:encoded><![CDATA[<p>Mission successful.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

