<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Side Effects and Functional Programming</title><link>http://weblogs.asp.net/podwysocki/archive/2008/09/12/side-effects-and-functional-programming.aspx</link><description>One of my first posts at CodeBetter was in regards to side effects and how, when unmanaged, can be truly evil. Today, I want to revisit that topic briefly in regards to functional programming and managing side effects. When I was out in Redmond a couple</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>DenkZEIT : Stolpersteine auf dem Weg zur Faulheit</title><link>http://weblogs.asp.net/podwysocki/archive/2008/09/12/side-effects-and-functional-programming.aspx#7143674</link><pubDate>Wed, 08 Jul 2009 17:51:53 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7143674</guid><dc:creator>DenkZEIT : Stolpersteine auf dem Weg zur Faulheit</dc:creator><author>DenkZEIT : Stolpersteine auf dem Weg zur Faulheit</author><description>&lt;p&gt;Pingback from &amp;nbsp;DenkZEIT : Stolpersteine auf dem Weg zur Faulheit&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7143674" width="1" height="1"&gt;</description></item><item><title>re: Side Effects and Functional Programming</title><link>http://weblogs.asp.net/podwysocki/archive/2008/09/12/side-effects-and-functional-programming.aspx#6628098</link><pubDate>Wed, 17 Sep 2008 00:30:15 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6628098</guid><dc:creator>podwysocki</dc:creator><author>podwysocki</author><description>&lt;p&gt;@Logicalmind&lt;/p&gt;
&lt;p&gt;Yes, to move the scope of the reader within the function itself would definitely be an answer to the problem. &amp;nbsp;To contain the IO action is probably the best bet, or avoid laziness altogether.&lt;/p&gt;
&lt;p&gt;Matt&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6628098" width="1" height="1"&gt;</description></item><item><title>re: Side Effects and Functional Programming</title><link>http://weblogs.asp.net/podwysocki/archive/2008/09/12/side-effects-and-functional-programming.aspx#6627727</link><pubDate>Tue, 16 Sep 2008 19:44:27 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6627727</guid><dc:creator>Logicalmind</dc:creator><author>Logicalmind</author><description>&lt;p&gt;C# Version of Resource Management:&lt;/p&gt;
&lt;p&gt;Func&amp;lt;string&amp;gt; ReadLines = () =&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;using (var fs = File.OpenText(&amp;quot;abc.txt&amp;quot;))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return fs.ReadToEnd();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;};&lt;/p&gt;
&lt;p&gt;Console.WriteLine(ReadLines());&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6627727" width="1" height="1"&gt;</description></item><item><title>re: Side Effects and Functional Programming</title><link>http://weblogs.asp.net/podwysocki/archive/2008/09/12/side-effects-and-functional-programming.aspx#6624806</link><pubDate>Sun, 14 Sep 2008 17:36:27 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6624806</guid><dc:creator>podwysocki</dc:creator><author>podwysocki</author><description>&lt;p&gt;@Laurent&lt;/p&gt;
&lt;p&gt;I agree that there are places such as dealing with resources or exception handling sometimes doesn't make a good match. &amp;nbsp;Unfortunately, the languages here I used aren't pure in a way that I can declare what happens inside of them, and then be able to react accordingly. &amp;nbsp;Maybe in the future, we could have some static analysis or some other mechanism to help with that.&lt;/p&gt;
&lt;p&gt;Sure, a sequence computation expression could help and I think that's the ideal situation there. &amp;nbsp;But for exception management, there is no real good answer using lazy evaluation I think.&lt;/p&gt;
&lt;p&gt;Matt&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6624806" width="1" height="1"&gt;</description></item><item><title>Dew Drop - September 13, 2008 | Alvin Ashcraft's Morning Dew</title><link>http://weblogs.asp.net/podwysocki/archive/2008/09/12/side-effects-and-functional-programming.aspx#6622945</link><pubDate>Sat, 13 Sep 2008 13:52:50 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6622945</guid><dc:creator>Dew Drop - September 13, 2008 | Alvin Ashcraft's Morning Dew</dc:creator><author>Dew Drop - September 13, 2008 | Alvin Ashcraft's Morning Dew</author><description>&lt;p&gt;Pingback from &amp;nbsp;Dew Drop - September 13, 2008 | Alvin Ashcraft's Morning Dew&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6622945" width="1" height="1"&gt;</description></item><item><title>re: Side Effects and Functional Programming</title><link>http://weblogs.asp.net/podwysocki/archive/2008/09/12/side-effects-and-functional-programming.aspx#6622607</link><pubDate>Sat, 13 Sep 2008 10:43:23 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6622607</guid><dc:creator>Laurent</dc:creator><author>Laurent</author><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;There are some good examples here. Sometimes, we just don't want lazy evaluation. For example, in your Exception Management problem, it seems that it's a bad idea to use lazy evaluation (we want to know if all elements are valid before returning any value - thus, we want strict evaluation).&lt;/p&gt;
&lt;p&gt;If you want side effects to occur at most once, you should use Seq.cache.&lt;/p&gt;
&lt;p&gt;For Resource Management, I suggest:&lt;/p&gt;
&lt;p&gt;let readLines = seq {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;use reader = System.IO.File.OpenText(@&amp;quot;D:\Foo.txt&amp;quot;)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;yield reader.ReadToEnd() }&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6622607" width="1" height="1"&gt;</description></item></channel></rss>