<?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: Dynamic Programming in Haskell and why DP is useful</title>
	<atom:link href="http://www.akrish.net/2009/06/28/dynamic-programming-in-haskell-and-why-dp-is-useful/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.akrish.net/2009/06/28/dynamic-programming-in-haskell-and-why-dp-is-useful/</link>
	<description>Thoughts, ideas and more</description>
	<pubDate>Thu, 11 Mar 2010 14:26:29 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Jedaï</title>
		<link>http://www.akrish.net/2009/06/28/dynamic-programming-in-haskell-and-why-dp-is-useful/comment-page-1/#comment-233</link>
		<dc:creator>Jedaï</dc:creator>
		<pubDate>Tue, 30 Jun 2009 13:48:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.akrish.net/?p=216#comment-233</guid>
		<description>&gt; yes I do need to explore the prelude. It’s one of the things that 
&gt; I’m lazy to do whenever i learn a new language but it’s quite important.

In the case of Haskell, exploring the Prelude is both important and interesting because most of this Prelude is written in Haskell (in fact all of it but for some primitives for additions, equalities and so on). It isn't as dry and boring than reading a list of functions with their type and description, you also have some quite nice code. Reading the Prelude is as much a didactic experience that it is a necessity to write code without reinventing the wheel.

http://www.haskell.org/onlinereport/standard-prelude.html

(not all bits are as interesting, the numeric instances part bore me to tears myself)
The current Prelude code in GHC contains some subtle difference and Data.List and others bring some other functions but the base of Haskell is still the Prelude.</description>
		<content:encoded><![CDATA[<p>&gt; yes I do need to explore the prelude. It’s one of the things that<br />
&gt; I’m lazy to do whenever i learn a new language but it’s quite important.</p>
<p>In the case of Haskell, exploring the Prelude is both important and interesting because most of this Prelude is written in Haskell (in fact all of it but for some primitives for additions, equalities and so on). It isn&#8217;t as dry and boring than reading a list of functions with their type and description, you also have some quite nice code. Reading the Prelude is as much a didactic experience that it is a necessity to write code without reinventing the wheel.</p>
<p><a href="http://www.haskell.org/onlinereport/standard-prelude.html" rel="nofollow">http://www.haskell.org/onlinereport/standard-prelude.html</a></p>
<p>(not all bits are as interesting, the numeric instances part bore me to tears myself)<br />
The current Prelude code in GHC contains some subtle difference and Data.List and others bring some other functions but the base of Haskell is still the Prelude.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: akshayk</title>
		<link>http://www.akrish.net/2009/06/28/dynamic-programming-in-haskell-and-why-dp-is-useful/comment-page-1/#comment-232</link>
		<dc:creator>akshayk</dc:creator>
		<pubDate>Tue, 30 Jun 2009 13:02:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.akrish.net/?p=216#comment-232</guid>
		<description>@adrian - I'm not entirely sure what your program is doing but it looks like a 2-d dynamic programming. However, I think it quite inefficient (i.e. exponential) because of all the calls to leMatrix
@phil - it's a different problem but great. i liked your post and the haskell solution looked good to me.
@internetguy - yeah I know about pattern matching, just haven't gotten used to using it. I didn't know about guards but thanks for the tip. I will try to use both of them in the future. Yeah I knew about the empty list problem but the way that I'm calling it will never trigger that so it's a bad practice but ok in this situation. You're right about using Maybe though.
@Jedai - yes I do need to explore the prelude. It's one of the things that I'm lazy to do whenever i learn a new language but it's quite important. Yeah I will try and incorporate pattern matching and guards into my programming.

thanks for the comments everyone</description>
		<content:encoded><![CDATA[<p>@adrian - I&#8217;m not entirely sure what your program is doing but it looks like a 2-d dynamic programming. However, I think it quite inefficient (i.e. exponential) because of all the calls to leMatrix<br />
@phil - it&#8217;s a different problem but great. i liked your post and the haskell solution looked good to me.<br />
@internetguy - yeah I know about pattern matching, just haven&#8217;t gotten used to using it. I didn&#8217;t know about guards but thanks for the tip. I will try to use both of them in the future. Yeah I knew about the empty list problem but the way that I&#8217;m calling it will never trigger that so it&#8217;s a bad practice but ok in this situation. You&#8217;re right about using Maybe though.<br />
@Jedai - yes I do need to explore the prelude. It&#8217;s one of the things that I&#8217;m lazy to do whenever i learn a new language but it&#8217;s quite important. Yeah I will try and incorporate pattern matching and guards into my programming.</p>
<p>thanks for the comments everyone</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jedaï</title>
		<link>http://www.akrish.net/2009/06/28/dynamic-programming-in-haskell-and-why-dp-is-useful/comment-page-1/#comment-231</link>
		<dc:creator>Jedaï</dc:creator>
		<pubDate>Tue, 30 Jun 2009 11:09:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.akrish.net/?p=216#comment-231</guid>
		<description>lis :: (Ord a) =&gt; [a] -&gt; [a]
lis = snd . maximum . foldr go []
&#160;&#160;where
&#160;&#160;&#160;&#160;go x xxs =  foldl' (best x) (1,[x]) xxs : xxs
&#160;&#160;&#160;&#160;best x cur@(len,_) (len', yys@(y:ys)) = if x &lt; y &amp;&amp; len &lt;= len' then (len'+1, x:yys) else cur

This seems to do the trick.

A remark on your style : you should probably take some time to explore the prelude and Data.List (listGet = (!!), getIndex = elemIndex, listMax = maximum). You also use length too much : length is O(n) and you shouldn't need it very much (for instance using length in your listMax was completely useless). Instead you should try to use pattern matching more, and guards are good too, though an if...then...else here and there isn't too bad (as long you don't start to nest them).</description>
		<content:encoded><![CDATA[<p>lis :: (Ord a) =&gt; [a] -&gt; [a]<br />
lis = snd . maximum . foldr go []<br />
&nbsp;&nbsp;where<br />
&nbsp;&nbsp;&nbsp;&nbsp;go x xxs =  foldl&#8217; (best x) (1,[x]) xxs : xxs<br />
&nbsp;&nbsp;&nbsp;&nbsp;best x cur@(len,_) (len&#8217;, yys@(y:ys)) = if x &lt; y &amp;&amp; len &lt;= len&#8217; then (len&#8217;+1, x:yys) else cur</p>
<p>This seems to do the trick.</p>
<p>A remark on your style : you should probably take some time to explore the prelude and Data.List (listGet = (!!), getIndex = elemIndex, listMax = maximum). You also use length too much : length is O(n) and you shouldn&#8217;t need it very much (for instance using length in your listMax was completely useless). Instead you should try to use pattern matching more, and guards are good too, though an if&#8230;then&#8230;else here and there isn&#8217;t too bad (as long you don&#8217;t start to nest them).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Some Internet Guy</title>
		<link>http://www.akrish.net/2009/06/28/dynamic-programming-in-haskell-and-why-dp-is-useful/comment-page-1/#comment-227</link>
		<dc:creator>Some Internet Guy</dc:creator>
		<pubDate>Mon, 29 Jun 2009 20:41:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.akrish.net/?p=216#comment-227</guid>
		<description>I neglected to provide an empty list case for the alternative getIndex I sketched.  Your original version contains the same flaw as mine.

getIndex [] i = error "probably this function should return Maybe Int instead of crashing"

If an empty list were passed to your getIndex, then the call to head would crash.  If your version doesn't find the desired item, then it will recurse until the empty list case happens, so you still get a crash.

Functions which are not defined for all inputs are called &lt;i&gt;partial functions&lt;/i&gt; and they can ruin your day.  head is a partial function because it just crashes (prints an error and aborts the program) if you pass it an empty list.  You can make a partial function a total function by returning a Maybe type, and using Maybe's Nothing value to signal failure.

For example, Data.list.elemIndex returns Maybe Int, such that you get Just 5 if your item is found at index 5, or Nothing if it is not found at all.</description>
		<content:encoded><![CDATA[<p>I neglected to provide an empty list case for the alternative getIndex I sketched.  Your original version contains the same flaw as mine.</p>
<p>getIndex [] i = error &#8220;probably this function should return Maybe Int instead of crashing&#8221;</p>
<p>If an empty list were passed to your getIndex, then the call to head would crash.  If your version doesn&#8217;t find the desired item, then it will recurse until the empty list case happens, so you still get a crash.</p>
<p>Functions which are not defined for all inputs are called <i>partial functions</i> and they can ruin your day.  head is a partial function because it just crashes (prints an error and aborts the program) if you pass it an empty list.  You can make a partial function a total function by returning a Maybe type, and using Maybe&#8217;s Nothing value to signal failure.</p>
<p>For example, Data.list.elemIndex returns Maybe Int, such that you get Just 5 if your item is found at index 5, or Nothing if it is not found at all.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Some Internet Guy</title>
		<link>http://www.akrish.net/2009/06/28/dynamic-programming-in-haskell-and-why-dp-is-useful/comment-page-1/#comment-226</link>
		<dc:creator>Some Internet Guy</dc:creator>
		<pubDate>Mon, 29 Jun 2009 20:19:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.akrish.net/?p=216#comment-226</guid>
		<description>You can make your code more concise by using pattern matching and guards.  Consider this alternative version of your getIndex function (formatting might be eaten by the blog):

getIndex (x:xs) i
    &#124; x == i        = 0
    &#124; otherwise   = 1 + getIndex xs i

You can avoid calling head and tail by simply breaking your list apart in the pattern.  The '&#124;' things are guards, which simply evaluate some boolean expression to decide which function body will be used.

For even more concision, you can call &lt;a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v%3AelemIndex" rel="nofollow"&gt;Data.List.elemIndex&lt;/a&gt; instead of using your getIndex function.</description>
		<content:encoded><![CDATA[<p>You can make your code more concise by using pattern matching and guards.  Consider this alternative version of your getIndex function (formatting might be eaten by the blog):</p>
<p>getIndex (x:xs) i<br />
    | x == i        = 0<br />
    | otherwise   = 1 + getIndex xs i</p>
<p>You can avoid calling head and tail by simply breaking your list apart in the pattern.  The &#8216;|&#8217; things are guards, which simply evaluate some boolean expression to decide which function body will be used.</p>
<p>For even more concision, you can call <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v%3AelemIndex" rel="nofollow">Data.List.elemIndex</a> instead of using your getIndex function.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: akrish » Blog Archive » Dynamic Programming in Haskell and why DP &#8230;</title>
		<link>http://www.akrish.net/2009/06/28/dynamic-programming-in-haskell-and-why-dp-is-useful/comment-page-1/#comment-225</link>
		<dc:creator>akrish » Blog Archive » Dynamic Programming in Haskell and why DP &#8230;</dc:creator>
		<pubDate>Mon, 29 Jun 2009 16:48:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.akrish.net/?p=216#comment-225</guid>
		<description>[...] original here:  akrish » Blog Archive » Dynamic Programming in Haskell and why DP &#8230;   SHARETHIS.addEntry({ title: "akrish » Blog Archive » Dynamic Programming in Haskell and why DP [...]</description>
		<content:encoded><![CDATA[<p>[...] original here:  akrish » Blog Archive » Dynamic Programming in Haskell and why DP &#8230;   SHARETHIS.addEntry({ title: &#8220;akrish » Blog Archive » Dynamic Programming in Haskell and why DP [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phil</title>
		<link>http://www.akrish.net/2009/06/28/dynamic-programming-in-haskell-and-why-dp-is-useful/comment-page-1/#comment-224</link>
		<dc:creator>Phil</dc:creator>
		<pubDate>Mon, 29 Jun 2009 15:42:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.akrish.net/?p=216#comment-224</guid>
		<description>I recently solved the longest common subsequence problem at http://programmingpraxis.com/2009/06/09/longest-common-subsequence/.  One of the comments provided a Haskell solution.</description>
		<content:encoded><![CDATA[<p>I recently solved the longest common subsequence problem at <a href="http://programmingpraxis.com/2009/06/09/longest-common-subsequence/" rel="nofollow">http://programmingpraxis.com/2009/06/09/longest-common-subsequence/</a>.  One of the comments provided a Haskell solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: adrian</title>
		<link>http://www.akrish.net/2009/06/28/dynamic-programming-in-haskell-and-why-dp-is-useful/comment-page-1/#comment-222</link>
		<dc:creator>adrian</dc:creator>
		<pubDate>Mon, 29 Jun 2009 06:56:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.akrish.net/?p=216#comment-222</guid>
		<description>import Data.Array

type Matrix = Array (Int,Int) Int

leMatrix :: String -&gt; String -&gt; Matrix
leMatrix s1 s2= let (ls1,ls2)=(length s1, length s2) in array ((0,0), (ls1, ls2)) $
    [((x,0),x)&#124;x&lt;-[0.. ls1]] ++ [((0,x),x)&#124;x&lt;-[1.. ls2]]  ++ -- Initialisierung
    [((i,j), minimum [(leMatrix s1 s2)!(i-1,j-1)+ d i j s1 s2, -- Rekursion
                      (leMatrix s1 s2)!(i-1,j)+1,
                      (leMatrix s1 s2)!(i,j-1)+1]) &#124; i&lt;-[1.. ls1], j Int -&gt; String -&gt; String -&gt; Int
d i j s1 s2
    &#124; s1!!(i-1) == s2!!(j-1) = 0
    &#124; otherwise = 1</description>
		<content:encoded><![CDATA[<p>import Data.Array</p>
<p>type Matrix = Array (Int,Int) Int</p>
<p>leMatrix :: String -&gt; String -&gt; Matrix<br />
leMatrix s1 s2= let (ls1,ls2)=(length s1, length s2) in array ((0,0), (ls1, ls2)) $<br />
    [((x,0),x)|x&lt;-[0.. ls1]] ++ [((0,x),x)|x&lt;-[1.. ls2]]  ++ &#8212; Initialisierung<br />
    [((i,j), minimum [(leMatrix s1 s2)!(i-1,j-1)+ d i j s1 s2, -- Rekursion<br />
                      (leMatrix s1 s2)!(i-1,j)+1,<br />
                      (leMatrix s1 s2)!(i,j-1)+1]) | i&lt;-[1.. ls1], j Int -&gt; String -&gt; String -&gt; Int<br />
d i j s1 s2<br />
    | s1!!(i-1) == s2!!(j-1) = 0<br />
    | otherwise = 1</p>
]]></content:encoded>
	</item>
</channel>
</rss>
