<?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: Collatz, Haskell and Memoization</title>
	<atom:link href="http://swizec.com/blog/collatz-haskell-and-memoization/swizec/3382/feed" rel="self" type="application/rss+xml" />
	<link>http://swizec.com/blog/collatz-haskell-and-memoization/swizec/3382</link>
	<description>Drinker of tea</description>
	<lastBuildDate>Fri, 14 Jun 2013 11:08:30 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>By: Anonymous</title>
		<link>http://swizec.com/blog/collatz-haskell-and-memoization/swizec/3382/comment-page-1#comment-4119</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Thu, 16 Feb 2012 02:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://swizec.com/blog/?p=3382#comment-4119</guid>
		<description> So, after playing around with this for a while, and trying various different ways to memoize the result, I&#039;ve come to the conclusion that memoization is a bad idea for this problem. You actually saw this yourself in your &quot;fixed&quot; version of the app, that Killed message was the OS informing you that it nuked the process from orbit for using too much memory. If you ran something like top (I like htop) you&#039;d see the process quickly gobble up every last shred of memory available, and then get killed off by the OS.

The final non-memoized version I came up with can be found here:
https://gist.github.com/1841085

I probably don&#039;t need to use a couple of those $!, and there&#039;s probably an existing way to do &quot;x `seq` x&quot;, but on the whole it seems fairly stable. When run it exhibits constant memory usage which shows it&#039;s not leaking thunks anywhere. If you really really wanted to use memoization your best bet would be to either find or create a memoization library that allows you to set an upper bound on memory usage for the result cache, otherwise it&#039;s just going to keep getting killed by the OS.</description>
		<content:encoded><![CDATA[<p> So, after playing around with this for a while, and trying various different ways to memoize the result, I&#8217;ve come to the conclusion that memoization is a bad idea for this problem. You actually saw this yourself in your &#8220;fixed&#8221; version of the app, that Killed message was the OS informing you that it nuked the process from orbit for using too much memory. If you ran something like top (I like htop) you&#8217;d see the process quickly gobble up every last shred of memory available, and then get killed off by the OS.</p>
<p>The final non-memoized version I came up with can be found here:<br />
<a href="https://gist.github.com/1841085" rel="nofollow">https://gist.github.com/1841085</a></p>
<p>I probably don&#8217;t need to use a couple of those $!, and there&#8217;s probably an existing way to do &#8220;x `seq` x&#8221;, but on the whole it seems fairly stable. When run it exhibits constant memory usage which shows it&#8217;s not leaking thunks anywhere. If you really really wanted to use memoization your best bet would be to either find or create a memoization library that allows you to set an upper bound on memory usage for the result cache, otherwise it&#8217;s just going to keep getting killed by the OS.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://swizec.com/blog/collatz-haskell-and-memoization/swizec/3382/comment-page-1#comment-4118</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 15 Feb 2012 22:05:00 +0000</pubDate>
		<guid isPermaLink="false">http://swizec.com/blog/?p=3382#comment-4118</guid>
		<description>You&#039;re still not calling the memoized function. Try this instead:

collatz :: Integer -&gt; [Integer]
collatz = memoFix col 
  where  
     col _ 1 = []
     col f n    
         &#124; odd n = 3*n+1:f $! 3*n+1
         &#124; even n = div n 2:f $! div n 2

I haven&#039;t actually run this code yet as I&#039;m at work, but when I get home I&#039;ll give it a run through and make sure I didn&#039;t screw anything up.
(Edit: Had to fix this post as the formatting got all screwed up)
(Edit2: Formatting is still screwed up, when I get home I&#039;ll make a GIST of this and link that, you should still be able to get the general idea from this though)</description>
		<content:encoded><![CDATA[<p>You&#8217;re still not calling the memoized function. Try this instead:</p>
<p>collatz :: Integer -&gt; [Integer]<br />
collatz = memoFix col<br />
  where  <br />
     col _ 1 = []<br />
     col f n    <br />
         | odd n = 3*n+1:f $! 3*n+1<br />
         | even n = div n 2:f $! div n 2</p>
<p>I haven&#8217;t actually run this code yet as I&#8217;m at work, but when I get home I&#8217;ll give it a run through and make sure I didn&#8217;t screw anything up.<br />
(Edit: Had to fix this post as the formatting got all screwed up)<br />
(Edit2: Formatting is still screwed up, when I get home I&#8217;ll make a GIST of this and link that, you should still be able to get the general idea from this though)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Swizec</title>
		<link>http://swizec.com/blog/collatz-haskell-and-memoization/swizec/3382/comment-page-1#comment-4117</link>
		<dc:creator>Swizec</dc:creator>
		<pubDate>Wed, 15 Feb 2012 21:03:00 +0000</pubDate>
		<guid isPermaLink="false">http://swizec.com/blog/?p=3382#comment-4117</guid>
		<description>Thanks for the suggestions! As mentioned I don&#039;t really understand performance in haskell yet.

Not sure why I didn&#039;t link it in the blog, but here&#039;s the full code: https://github.com/Swizec/random-coding/blob/master/project-euler/14.hs</description>
		<content:encoded><![CDATA[<p>Thanks for the suggestions! As mentioned I don&#8217;t really understand performance in haskell yet.</p>
<p>Not sure why I didn&#8217;t link it in the blog, but here&#8217;s the full code: https://github.com/Swizec/random-coding/blob/master/project-euler/14.hs</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://swizec.com/blog/collatz-haskell-and-memoization/swizec/3382/comment-page-1#comment-4116</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 15 Feb 2012 20:39:00 +0000</pubDate>
		<guid isPermaLink="false">http://swizec.com/blog/?p=3382#comment-4116</guid>
		<description>Sorry to necro this, but I just got around to looking through your blog after a few months of ignoring it and this jumped out at me. I&#039;d be willing to bet you&#039;re chewing through too much memory because of all the thunks you&#039;re building up.

You&#039;ve probably seen this, but it&#039;s worth pointing out: http://www.haskell.org/haskellwiki/Memoization

Being lazy is both the biggest strength, and biggest weakness of Haskell. By defaulting to lazy Haskell implicitly trades time complexity for space complexity (by deferring processing till absolutely necessary and instead building thunks in memory). This is often useful, but anytime you perform any sort of looping or recursion you run the danger of building monstrous thunk chains when you really don&#039;t need to. Judicious use of $!, BangPatterns, strict folds, and seq can go a long way towards performance improvements as well as in the best case lead to constant memory usage.

I must confess I don&#039;t fully understand the algorithm you&#039;ve provided in part because several chunks of it are missing, so I can&#039;t say for sure exactly where the problem is.</description>
		<content:encoded><![CDATA[<p>Sorry to necro this, but I just got around to looking through your blog after a few months of ignoring it and this jumped out at me. I&#8217;d be willing to bet you&#8217;re chewing through too much memory because of all the thunks you&#8217;re building up.</p>
<p>You&#8217;ve probably seen this, but it&#8217;s worth pointing out: <a href="http://www.haskell.org/haskellwiki/Memoization" rel="nofollow">http://www.haskell.org/haskellwiki/Memoization</a></p>
<p>Being lazy is both the biggest strength, and biggest weakness of Haskell. By defaulting to lazy Haskell implicitly trades time complexity for space complexity (by deferring processing till absolutely necessary and instead building thunks in memory). This is often useful, but anytime you perform any sort of looping or recursion you run the danger of building monstrous thunk chains when you really don&#8217;t need to. Judicious use of $!, BangPatterns, strict folds, and seq can go a long way towards performance improvements as well as in the best case lead to constant memory usage.</p>
<p>I must confess I don&#8217;t fully understand the algorithm you&#8217;ve provided in part because several chunks of it are missing, so I can&#8217;t say for sure exactly where the problem is.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: A geek with a hat &#187; This Haskell is wrong. Why?</title>
		<link>http://swizec.com/blog/collatz-haskell-and-memoization/swizec/3382/comment-page-1#comment-4027</link>
		<dc:creator>A geek with a hat &#187; This Haskell is wrong. Why?</dc:creator>
		<pubDate>Fri, 20 Jan 2012 17:47:45 +0000</pubDate>
		<guid isPermaLink="false">http://swizec.com/blog/?p=3382#comment-4027</guid>
		<description>[...] Collatz, Haskell and Memoization (swizec.com) [...]</description>
		<content:encoded><![CDATA[<p>[...] Collatz, Haskell and Memoization (swizec.com) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Swizec</title>
		<link>http://swizec.com/blog/collatz-haskell-and-memoization/swizec/3382/comment-page-1#comment-3993</link>
		<dc:creator>Swizec</dc:creator>
		<pubDate>Tue, 10 Jan 2012 11:22:00 +0000</pubDate>
		<guid isPermaLink="false">http://swizec.com/blog/?p=3382#comment-3993</guid>
		<description>Yes but you actually know how to use python whereas I have no idea how haskell works :P

Did you bruteforce it?</description>
		<content:encoded><![CDATA[<p>Yes but you actually know how to use python whereas I have no idea how haskell works <img src="http://swizec.com/blog/wp-includes/images/smilies/icon_razz.gif?3e5991" alt=':P' class='wp-smiley' /> </p>
<p>Did you bruteforce it?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Smotko</title>
		<link>http://swizec.com/blog/collatz-haskell-and-memoization/swizec/3382/comment-page-1#comment-3992</link>
		<dc:creator>Smotko</dc:creator>
		<pubDate>Tue, 10 Jan 2012 10:47:00 +0000</pubDate>
		<guid isPermaLink="false">http://swizec.com/blog/?p=3382#comment-3992</guid>
		<description>I guess there is still a lot more room for improvements as even my python solution seems to be faster than yours. But I must admit, the haskell solutions look way more cooler :D</description>
		<content:encoded><![CDATA[<p>I guess there is still a lot more room for improvements as even my python solution seems to be faster than yours. But I must admit, the haskell solutions look way more cooler <img src="http://swizec.com/blog/wp-includes/images/smilies/icon_biggrin.gif?3e5991" alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Swizec</title>
		<link>http://swizec.com/blog/collatz-haskell-and-memoization/swizec/3382/comment-page-1#comment-3990</link>
		<dc:creator>Swizec</dc:creator>
		<pubDate>Mon, 09 Jan 2012 18:33:00 +0000</pubDate>
		<guid isPermaLink="false">http://swizec.com/blog/?p=3382#comment-3990</guid>
		<description>14.</description>
		<content:encoded><![CDATA[<p>14.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Smotko</title>
		<link>http://swizec.com/blog/collatz-haskell-and-memoization/swizec/3382/comment-page-1#comment-3989</link>
		<dc:creator>Smotko</dc:creator>
		<pubDate>Mon, 09 Jan 2012 18:25:00 +0000</pubDate>
		<guid isPermaLink="false">http://swizec.com/blog/?p=3382#comment-3989</guid>
		<description>Which Euler problem is this?</description>
		<content:encoded><![CDATA[<p>Which Euler problem is this?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Served from: www.swizec.com @ 2013-06-19 05:07:06 by W3 Total Cache -->