<?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: Haskell is possibly too lazy for me</title>
	<atom:link href="http://www.nobugs.org/blog/archives/2008/03/20/haskell-is-possibly-too-lazy-for-me/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nobugs.org/blog/archives/2008/03/20/haskell-is-possibly-too-lazy-for-me/</link>
	<description>Thoughts of a software engineer</description>
	<lastBuildDate>Fri, 12 Mar 2010 14:56:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: apfelmus</title>
		<link>http://www.nobugs.org/blog/archives/2008/03/20/haskell-is-possibly-too-lazy-for-me/comment-page-1/#comment-30514</link>
		<dc:creator>apfelmus</dc:creator>
		<pubDate>Fri, 21 Mar 2008 15:54:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.nobugs.org/blog/archives/2008/03/20/haskell-is-possibly-too-lazy-for-me/#comment-30514</guid>
		<description>Be sure to explore both sides of the laziness coin. As your post shows, some programs you write in O&#039;Caml aren&#039;t written the same way in Haskell (the corrections are really minor, though). But there are also Haskell programs you can&#039;t write that way in O&#039;Caml, these are where laziness shines.</description>
		<content:encoded><![CDATA[<p>Be sure to explore both sides of the laziness coin. As your post shows, some programs you write in O&#8217;Caml aren&#8217;t written the same way in Haskell (the corrections are really minor, though). But there are also Haskell programs you can&#8217;t write that way in O&#8217;Caml, these are where laziness shines.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cale Gibbard</title>
		<link>http://www.nobugs.org/blog/archives/2008/03/20/haskell-is-possibly-too-lazy-for-me/comment-page-1/#comment-30489</link>
		<dc:creator>Cale Gibbard</dc:creator>
		<pubDate>Fri, 21 Mar 2008 06:42:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.nobugs.org/blog/archives/2008/03/20/haskell-is-possibly-too-lazy-for-me/#comment-30489</guid>
		<description>foldl&#039; (+) 0</description>
		<content:encoded><![CDATA[<p>foldl&#8217; (+) 0</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christophe Poucet (vincenz)</title>
		<link>http://www.nobugs.org/blog/archives/2008/03/20/haskell-is-possibly-too-lazy-for-me/comment-page-1/#comment-30478</link>
		<dc:creator>Christophe Poucet (vincenz)</dc:creator>
		<pubDate>Fri, 21 Mar 2008 03:05:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.nobugs.org/blog/archives/2008/03/20/haskell-is-possibly-too-lazy-for-me/#comment-30478</guid>
		<description>One additional note.  It is unfair to claim this is an issue with Haskell.  I see that you are on the O&#039;Caml planet-list.  I am 100% sure that you would write this as a tail-recursive loop in O&#039;Caml as well. So the first sample you show is not even a fair objection.</description>
		<content:encoded><![CDATA[<p>One additional note.  It is unfair to claim this is an issue with Haskell.  I see that you are on the O&#8217;Caml planet-list.  I am 100% sure that you would write this as a tail-recursive loop in O&#8217;Caml as well. So the first sample you show is not even a fair objection.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christophe Poucet (vincenz)</title>
		<link>http://www.nobugs.org/blog/archives/2008/03/20/haskell-is-possibly-too-lazy-for-me/comment-page-1/#comment-30477</link>
		<dc:creator>Christophe Poucet (vincenz)</dc:creator>
		<pubDate>Fri, 21 Mar 2008 03:02:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.nobugs.org/blog/archives/2008/03/20/haskell-is-possibly-too-lazy-for-me/#comment-30477</guid>
		<description>I would simply use:

module Main where

mysumas !acc []     = acc
mysumas !acc (x:xs) = mysumas (acc+x) xs
main = putStrLn $ show $ mysumas 0 $ take 100000 $ [1..]

And compile wih &#039;ghc -fbang-patterns --make Main.hs&#039;.

Some additional notes that led to this solution:
1) It is clear that this requires a tail-recursive loop as it produces non-lazy data. (There is some better term for &#039;non-lazy&#039; but I&#039;m sure an expert can help you there)
2) When doing tail-recursion, you want to make your accumulator strict.  This is easily done with the ! bit
3) Normally I would not even define a function that traverses lists in such a trivial manner by using pattern-matching.  I would simply use fold.  And in this case the correct fold (namely for non-lazy data) is foldl&#039;.  This is a reasonably quick-learned fact if one spends time in the Haskell community (be it the mailing-list or the IRC channel).  In fact, it would probably be the first answer to anyone asking how to make this code better, by most any Haskeller.</description>
		<content:encoded><![CDATA[<p>I would simply use:</p>
<p>module Main where</p>
<p>mysumas !acc []     = acc<br />
mysumas !acc (x:xs) = mysumas (acc+x) xs<br />
main = putStrLn $ show $ mysumas 0 $ take 100000 $ [1..]</p>
<p>And compile wih &#8216;ghc -fbang-patterns &#8211;make Main.hs&#8217;.</p>
<p>Some additional notes that led to this solution:<br />
1) It is clear that this requires a tail-recursive loop as it produces non-lazy data. (There is some better term for &#8216;non-lazy&#8217; but I&#8217;m sure an expert can help you there)<br />
2) When doing tail-recursion, you want to make your accumulator strict.  This is easily done with the ! bit<br />
3) Normally I would not even define a function that traverses lists in such a trivial manner by using pattern-matching.  I would simply use fold.  And in this case the correct fold (namely for non-lazy data) is foldl&#8217;.  This is a reasonably quick-learned fact if one spends time in the Haskell community (be it the mailing-list or the IRC channel).  In fact, it would probably be the first answer to anyone asking how to make this code better, by most any Haskeller.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Langley</title>
		<link>http://www.nobugs.org/blog/archives/2008/03/20/haskell-is-possibly-too-lazy-for-me/comment-page-1/#comment-30474</link>
		<dc:creator>Adam Langley</dc:creator>
		<pubDate>Fri, 21 Mar 2008 02:23:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.nobugs.org/blog/archives/2008/03/20/haskell-is-possibly-too-lazy-for-me/#comment-30474</guid>
		<description>You&#039;re perfectly correct that it can be tough for even for the experts to evaluate the time and space usage of a given bit of code. It&#039;s an issue. However, you can learn a few combinators which solve the problem 90% of the time.

For example, when you see &quot;I want to compute a value of a list, strictly&quot;, you should think &quot;foldl&#039;&quot;:

Prelude Data.List&gt; foldl&#039; (+) 0 $ take 1000000 [1..]
500000500000


AGL</description>
		<content:encoded><![CDATA[<p>You&#8217;re perfectly correct that it can be tough for even for the experts to evaluate the time and space usage of a given bit of code. It&#8217;s an issue. However, you can learn a few combinators which solve the problem 90% of the time.</p>
<p>For example, when you see &#8220;I want to compute a value of a list, strictly&#8221;, you should think &#8220;foldl&#8217;&#8221;:</p>
<p>Prelude Data.List&gt; foldl&#8217; (+) 0 $ take 1000000 [1..]<br />
500000500000</p>
<p>AGL</p>
]]></content:encoded>
	</item>
</channel>
</rss>
