<?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/"
	>

<channel>
	<title>Alson Kemp &#187; Lazy Evaluation</title>
	<atom:link href="http://www.alsonkemp.com/tag/lazy-evaluation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alsonkemp.com</link>
	<description>Hackfoofery</description>
	<lastBuildDate>Fri, 16 Jul 2010 18:18:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Thinking About Haskell*: You Know Lazy Evaluation; You Just Don&#8217;t Know It</title>
		<link>http://www.alsonkemp.com/haskell/thinking-about-haskell-you-know-lazy-evaluation-you-just-dont-know-it/</link>
		<comments>http://www.alsonkemp.com/haskell/thinking-about-haskell-you-know-lazy-evaluation-you-just-dont-know-it/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 05:33:06 +0000</pubDate>
		<dc:creator>alson</dc:creator>
				<category><![CDATA[Geekery]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Lazy Evaluation]]></category>

		<guid isPermaLink="false">http://www.alsonkemp.com/?p=110</guid>
		<description><![CDATA[[Post updated to reflect comments.  ...too much late night typing...]
Lazy evaluation is a very novel aspect of Haskell.  Turns out that it&#8217;s not that difficult to think about.

A very common example of lazy-ish evaluation is &#8216;&#38;&#38;&#8217; operators used in lots of languages (using C#):


if &#40; &#40;obj != null&#41; &#38;&#38; &#40;obj.someMethod&#40;&#41; == somethingElse&#41; &#41; [...]]]></description>
			<content:encoded><![CDATA[<p>[Post updated to reflect comments.  ...too much late night typing...]
Lazy evaluation is a very novel aspect of Haskell.  Turns out that it&#8217;s not that difficult to think about.</p>

<p>A very common example of lazy-ish evaluation is &#8216;&amp;&amp;&#8217; operators used in lots of languages (using C#):</p>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span>obj <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span>obj.<span style="color: #0000FF;">someMethod</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> somethingElse<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// do something</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<p><span id="more-110"></span></p>

<p><em>obj.someMethod</em> isn&#8217;t evaluated unless <em>obj != null</em>.  So <em>&amp;&amp;</em>, a C# function/operator everyone knows and loves, is strict in its first parameter and lazy in its second.  The previous code might act a little like the following:</p>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>obj <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>obj.<span style="color: #0000FF;">someMethod</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> somethingElse<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// do something</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<p>If <em>&amp;&amp;</em> were strict in both parameters, then it&#8217;d throw errors when the second parameter was unconditionally evaluated (e.g. <em>null.someMethod()).  If _&amp;&amp;</em> weren&#8217;t lazy, it might act something like:</p>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">condition1 <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>obj <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
condition2 <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>obj.<span style="color: #0000FF;">someMethod</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> somethingElse<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>condition1 <span style="color: #008000;">&amp;&amp;</span> condition2<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// do something</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<p>Clearly, <em>condition2</em> would throw an Exception when <em>obj</em> was null, so the <em>if</em> statement must not be evaluating the second parameter.  It&#8217;s as if it were lazy&#8230;</p>

<p>Here&#8217;s the (slightly revised for clarity) definition of <em>&amp;&amp;</em> from Haskell:</p>


<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&amp;&amp;</span><span style="color: green;">&#41;</span>                    <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Bool</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span>
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&amp;&amp;</span><span style="color: green;">&#41;</span> True x              <span style="color: #339933; font-weight: bold;">=</span>  x
<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&amp;&amp;</span><span style="color: green;">&#41;</span> False <span style="color: #339933; font-weight: bold;">_</span>             <span style="color: #339933; font-weight: bold;">=</span>  False</pre></div></div>


<p>Just as with C#, the first parameter (<em>x</em>) must be evaluated, but the second parameter is only evaluated if the first parameter is True.  <em>&amp;&amp;</em> is <em>strict</em> in the first parameter and <em>lazy</em> in the second.</p>

<h3>And That&#8217;s All There Is to Laziness?</h3>

<p>Nope.  It&#8217;s &#8220;turtles all the way down&#8221;.</p>

<p>Laziness is a defining feature of Haskell and a feature that separates Haskell from the vast majority of languages.  Lazy evaluation isn&#8217;t confined to a few operators in Haskell; lazy evaluation starts at the first function/expression in your program and continues all the way down.</p>

<p>For more reading, check out <a href="http://www.haskell.org/haskellwiki/Haskell/Lazy_evaluation">here</a>, <a href="http://en.wikipedia.org/wiki/Lazy_evaluation">here</a>, <a href="http://neilmitchell.blogspot.com/2008/03/lazy-evaluation-strict-vs-speculative.html">here</a> and <a href="http://www.google.com/search?q=lazy+evaluation+haskell&#038;ie=utf-8&#038;oe=utf-8&#038;aq=t&#038;rls=org.mozilla:en-US:official&#038;client=firefox-a">here</a>.</p>

<h3>But I Don&#8217;t Want To Be Lazy</h3>

<p>[Example fixed.]
Then force evaluation of your parameters.  How?  One way is to use the <em>$!</em> function to force the evaluation of parameters.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">f a b c <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>someFunction <span style="color: #339933; font-weight: bold;">$!</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$!</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$!</span> c</pre></div></div>


<p>See <a href="http://www.haskell.org/haskellwiki/Performance/Strictness">here</a> for more.</p>

<hr />

<p>&#42; These little bits have helped me think about Haskell.  Maybe they&#8217;ll be useful for you, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alsonkemp.com/haskell/thinking-about-haskell-you-know-lazy-evaluation-you-just-dont-know-it/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
