<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/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>Matt Manela's Blog : Haskell</title><link>http://blogs.msdn.com/matt/archive/tags/Haskell/default.aspx</link><description>Tags: Haskell</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Prime Factorization using Unfold in Haskell</title><link>http://blogs.msdn.com/matt/archive/2009/03/17/prime-factorization-using-unfold-in-haskell.aspx</link><pubDate>Tue, 17 Mar 2009 16:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9483663</guid><dc:creator>MattManela</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/matt/comments/9483663.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=9483663</wfw:commentRss><description>&lt;P&gt;I randomly yesterday started thinking about the &lt;STRONG&gt;unfoldr &lt;/STRONG&gt;function in &lt;A href="http://en.wikipedia.org/wiki/Haskell_(programming_language)" target=_blank mce_href="http://en.wikipedia.org/wiki/Haskell_(programming_language)"&gt;Haskell&lt;/A&gt; while working out at the gym (how nerdy is that, I am lifting iron but thinking of functional programming). Unfoldr take a single and an unfolding function and turns it into a list (the opposite of fold).&amp;nbsp; At the gym I was thinking about an application where I can use this and I decided that when I got home I would use it to write a prime factorization function.&amp;nbsp; This is a method that when given a number returns the list of its prime factors. &lt;/P&gt;
&lt;P&gt;It was easy to write the only part I am not pleased about is the code I used to deal with tuples.&amp;nbsp; It seems clumsy and I am still looking for a way to clean that up. &lt;/P&gt;
&lt;P&gt;One note: The code below references a list of prime numbers called primes , which is not shown. &lt;/P&gt;
&lt;P&gt;Here is the code:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: gray 1px solid; BORDER-LEFT: gray 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; HEIGHT: 172px; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: gray 1px solid; CURSOR: text; BORDER-RIGHT: gray 1px solid; PADDING-TOP: 4px"&gt;
&lt;DIV style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"&gt;&lt;SPAN style="COLOR: #606060"&gt;   1:&lt;/SPAN&gt; primeFactors x = unfoldr findFactor x&lt;/PRE&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"&gt;&lt;SPAN style="COLOR: #606060"&gt;   2:&lt;/SPAN&gt;                  where&lt;/PRE&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"&gt;&lt;SPAN style="COLOR: #606060"&gt;   3:&lt;/SPAN&gt;                    first (a,b,c) = a&lt;/PRE&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"&gt;&lt;SPAN style="COLOR: #606060"&gt;   4:&lt;/SPAN&gt;                    findFactor 1 = Nothing&lt;/PRE&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"&gt;&lt;SPAN style="COLOR: #606060"&gt;   5:&lt;/SPAN&gt;                    findFactor b = (\(_,d,p)-&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; Just (p, d))&lt;/PRE&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"&gt;&lt;SPAN style="COLOR: #606060"&gt;   6:&lt;/SPAN&gt;                                   $ head $ filter ((==0).first) &lt;/PRE&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"&gt;&lt;SPAN style="COLOR: #606060"&gt;   7:&lt;/SPAN&gt;                                   $  map (\p -&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; (b `mod` p, b `div` p, p))  primes&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;This function will take any number which is greater than 1 and return a list of its prime factors.&amp;nbsp; But don’t take my word for it, I wrote a quickcheck property to ensure the prime factors multiply back to the original number:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: gray 1px solid; BORDER-LEFT: gray 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; BACKGROUND-COLOR: #f4f4f4; MARGIN: 20px 0px 10px; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; HEIGHT: 54px; MAX-HEIGHT: 200px; FONT-SIZE: 8pt; OVERFLOW: auto; BORDER-TOP: gray 1px solid; CURSOR: text; BORDER-RIGHT: gray 1px solid; PADDING-TOP: 4px"&gt;
&lt;DIV style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; HEIGHT: 2em; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"&gt;&lt;SPAN style="COLOR: #606060"&gt;   1:&lt;/SPAN&gt; prop_factors num =  num &lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; 1 ==&lt;SPAN style="COLOR: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; num == (foldr1 (*) $ primeFactors num)&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;When&amp;nbsp;running quickcheck on this property you see the following:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;quickCheck prop_factors &lt;BR&gt;OK, passed 100 tests.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9483663" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matt/archive/tags/Haskell/default.aspx">Haskell</category><category domain="http://blogs.msdn.com/matt/archive/tags/Programming/default.aspx">Programming</category></item><item><title>Parameterized State Transformer Monad in F#?</title><link>http://blogs.msdn.com/matt/archive/2008/11/04/parameterized-state-transformer-monad-in-f.aspx</link><pubDate>Wed, 05 Nov 2008 08:30:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9043440</guid><dc:creator>MattManela</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/matt/comments/9043440.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=9043440</wfw:commentRss><description>&lt;p&gt;I have have been playing around with F# and I decided to create a state monad.&amp;nbsp; This worked out really well since I was able to leverage the &lt;a href="http://blogs.msdn.com/dsyme/archive/2007/09/22/some-details-on-f-computation-expressions-aka-monadic-or-workflow-syntax.aspx" target="_blank"&gt;F# computation expressions&lt;/a&gt;.&amp;nbsp; I then decided to try to extend this and make it more general by creating a parameterized state transformer monad.&amp;nbsp; This is a state monad which encapsulates another monad.&amp;nbsp; What this allow you to do is turn any computation into a statefull one.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;This concept exists in Haskell which you can see &lt;a href="http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#3" target="_blank"&gt;here&lt;/a&gt;. However, my attempts at replicating this in F# failed.&amp;nbsp; Unlike in Haskell, the computation expressions in F# don't share a common interface (or type class).&amp;nbsp; This prevents a computation from being able to generically take another computation as a parameter.&amp;nbsp; The reason is that an operation on the parameterized state transformer monad such as bind will result in a bind called on its encapsulated monad.&amp;nbsp; But since there is no interface for computations there is no bind method to call.&amp;nbsp; &lt;/p&gt; &lt;p&gt;I attempted to fix this by creating my own monad interface but this didn't work:&lt;/p&gt; &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt; &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; type Monad =&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt;     abstract Bind : 'm * ('a -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; 'm) -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; 'm&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;     abstract Delay : (unit -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;'m) -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; 'm&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;     abstract Let : 'a * ('a -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; 'm) -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; 'm&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;     abstract Return : 'a -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; 'm&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;     abstract Zero : unit -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; 'm&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt;     abstract Combine : 'm -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; 'm2 -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; 'm3&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt;     abstract Run : (unit -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;'m) -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; 'm&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;After playing with that and failing I ended up with a less than ideal solution.&amp;nbsp; Given a Maybe monad like below:&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; // Maybe Monad    &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; type MaybeBuilder() =&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;     member b.Return(x) = Some x&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;     member b.Run(f) = f()&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;     member b.Delay(f) = f&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;     member b.Let(p,rest) = rest p&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt;     member b.Zero () = None&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt;     member b.Combine(m1,dm2) = match m1 with&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   9:&lt;/span&gt;                                  | None -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; dm2()&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  10:&lt;/span&gt;                                  | x -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; x&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  11:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  12:&lt;/span&gt;     member b.Bind(p,rest) = match p with &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  13:&lt;/span&gt;                             | None -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; None&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  14:&lt;/span&gt;                             | Some r -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; rest r&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I created a state transformer monad which take an argument of type m:MaybeBuilder&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; type StateMBuilder(m:MaybeBuilder) = &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt;     member b.Return(x) = fun s -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; m.Return (x,s)&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;     member b.Run(f) = fun inp -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; (f inp)()&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;     member b.Delay(f) = fun inp -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; fun () -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; f() inp&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;     member b.Let(p,rest) = rest p&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;     member b.Zero () = fun s -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; m.Zero()&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt;     member b.Bind(p,rest) = fun s -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; m.Bind(p s,fun (v,s2) -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; rest v s2)&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt;     member b.Combine(p1,dp2) = fun s -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; m.Combine(p1 s, dp2 s)&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   9:&lt;/span&gt;&amp;nbsp; &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  10:&lt;/span&gt;     // State specific functions&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  11:&lt;/span&gt;     member b.Update f = fun s -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; try m.Return (s, f s) with e -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; m.Zero()&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  12:&lt;/span&gt;     member b.Read () = b.Update id&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  13:&lt;/span&gt;     member b.Set t = b.Update (fun _ -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; t)&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now although this type is technically parameterized ;) it isn't really the idea since its not generic, it has to be a MaybeBuilder.&amp;nbsp; To use this with a different monad I would need to change m:MaybeBuild to m:SomeOtherMonad.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I am still going to play with this but this is as far as I have gotten.&lt;/p&gt;
&lt;p&gt;After all of that here is how you create a state transformer monad parameterized over the maybe monad:&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; let maybe = MaybeBuilder()&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; let state = StateMBuilder(maybe)&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If anyone has an idea how I can make this work I would love to hear it.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9043440" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matt/archive/tags/Haskell/default.aspx">Haskell</category><category domain="http://blogs.msdn.com/matt/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/matt/archive/tags/F_2300_/default.aspx">F#</category></item><item><title>Writing a Regular Expression parser in Haskell: Part 4</title><link>http://blogs.msdn.com/matt/archive/2008/06/21/writing-a-regular-expression-parser-in-haskell-part-4.aspx</link><pubDate>Sun, 22 Jun 2008 02:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8634775</guid><dc:creator>MattManela</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/matt/comments/8634775.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=8634775</wfw:commentRss><description>&lt;P&gt;With the previous two modules in place we are now set up to use a DFA to match against a string.  In my implementation I support either a greedy match or an short match.  In a full featured regular expression engine this ability to choose greedy or not would be per operator but for simplicity I have it for the overall match.  &lt;/P&gt; &lt;P&gt; &lt;/P&gt; &lt;P&gt;To do the matching I have a general function which will create a list of all matches.  Then the difference between short and greedy matching is which of the candidate solutions does it choose.&lt;/P&gt; &lt;P&gt;This is the method:&lt;/P&gt; &lt;DIV style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt; &lt;DIV style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   1:&lt;/SPAN&gt; doMatch func machine st [] = doAccept  machine st []&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   2:&lt;/SPAN&gt; doMatch func machine st string =  func $ map (\f -&lt;SPAN style="color: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; doMatch' st f []) (tails string)&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   3:&lt;/SPAN&gt;     where&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   4:&lt;/SPAN&gt;       doMatch' state [] soFar = doAccept machine st soFar&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   5:&lt;/SPAN&gt;       doMatch' state (s:str) soFar = &lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   6:&lt;/SPAN&gt;           case findTransition machine s state of&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   7:&lt;/SPAN&gt;             Nothing -&lt;SPAN style="color: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; doAccept machine state soFar&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   8:&lt;/SPAN&gt;             Just (from, to, val) -&lt;SPAN style="color: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; case doMatch' to str (soFar ++ [s]) of&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   9:&lt;/SPAN&gt;                                       (False,_) -&lt;SPAN style="color: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; case canAccept machine to of&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;  10:&lt;/SPAN&gt;                                                     True -&lt;SPAN style="color: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; (True, soFar ++ [s])&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;  11:&lt;/SPAN&gt;                                                     False -&lt;SPAN style="color: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; doMatch' to str (soFar ++ [s])&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;  12:&lt;/SPAN&gt;                                       (True,res) -&lt;SPAN style="color: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; (True,res)&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt; &lt;/P&gt;
&lt;P&gt;This creates the list of matches and uses the passed in function to determine how to filter to either the shortest or longest match.&lt;/P&gt;
&lt;P&gt;For short or long matches I pass in one of these two functions:&lt;/P&gt;
&lt;DIV style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;
&lt;DIV style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   1:&lt;/SPAN&gt; -- Get the shortest match&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   2:&lt;/SPAN&gt; shortest matches = case  filter (\s-&lt;SPAN style="color: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt;fst s) (sort matches) of&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   3:&lt;/SPAN&gt;                      [] -&lt;SPAN style="color: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; (False,"")&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   4:&lt;/SPAN&gt;                      ms -&lt;SPAN style="color: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; head ms&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   5:&lt;/SPAN&gt;  &lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   6:&lt;/SPAN&gt; -- Get the longest match&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   7:&lt;/SPAN&gt; longest matches = last.sort $ matches&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt; &lt;/P&gt;
&lt;P&gt;I created aliases for the functions to make it more handy:&lt;/P&gt;
&lt;DIV style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; height: 59px; background-color: #f4f4f4"&gt;
&lt;DIV style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   1:&lt;/SPAN&gt; (=~) = greedyMatch&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   2:&lt;/SPAN&gt; (=~?) = shortMatch&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt; &lt;/P&gt;
&lt;P&gt; And then the final result:&lt;/P&gt;
&lt;DIV style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;
&lt;DIV style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   1:&lt;/SPAN&gt; *SimpleRegex&lt;SPAN style="color: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; "hiphiphiphorray" =~? "hip(hip)*"&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   2:&lt;/SPAN&gt; (True,"hip")&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   3:&lt;/SPAN&gt;  &lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   4:&lt;/SPAN&gt; *SimpleRegex&lt;SPAN style="color: #0000ff"&gt;&amp;gt;&lt;/SPAN&gt; "hiphiphiphorray" =~ "hip(hip)*"&lt;/PRE&gt;&lt;PRE style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;SPAN style="color: #606060"&gt;   5:&lt;/SPAN&gt; (True,"hiphiphip")&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt; &lt;/P&gt;
&lt;P&gt; &lt;/P&gt;
&lt;P&gt;I attached a zip of all the files for this project.&lt;/P&gt;
&lt;P&gt;Enjoy!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8634775" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/matt/attachment/8634775.ashx" length="5977" type="application/zip" /><category domain="http://blogs.msdn.com/matt/archive/tags/Haskell/default.aspx">Haskell</category><category domain="http://blogs.msdn.com/matt/archive/tags/Programming/default.aspx">Programming</category></item><item><title>Writing a Regular Expression parser in Haskell: Part 3</title><link>http://blogs.msdn.com/matt/archive/2008/06/09/writing-a-regular-expression-parser-in-haskell-part-3.aspx</link><pubDate>Tue, 10 Jun 2008 09:08:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8588925</guid><dc:creator>MattManela</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/matt/comments/8588925.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=8588925</wfw:commentRss><description>&lt;P&gt;The third module in the simple regular expression parser is called: NFAtoDFA.  Which as you might have guessed, takes the NFA that resulted from the first module and converts it into a DFA.  The structure that the DFA uses is the same that the NFA uses since they are both finite state machines.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;Converting an NFA to a DFA requires mapping sets of nodes in the NFA to a single node in the DFA.  Many nodes in a NFA will correspond to one node in the DFA.  Making this change requires updating transitions to point to and from sets of nodes.  To manage this transformation I create a state monad using the following context:&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;DIV style='border-right-color: #808080; border-right-width: 1px; border-right-style: solid; padding-right: 4px; border-top-color: #808080; border-top-width: 1px; border-top-style: solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; overflow: auto; border-left-color: #808080; border-left-width: 1px; border-left-style: solid; width: 97.50%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom-color: #808080; border-bottom-width: 1px; border-bottom-style: solid; font-family: Consolas, "Courier New", Courier, monospace; background-color: #f4f4f4'&gt;&lt;DIV style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   1:&lt;/SPAN&gt; -- The state which we pass to build the DFA&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   2:&lt;/SPAN&gt; data ConvertContext = ConvertContext { nfa :: FiniteMachine,&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   3:&lt;/SPAN&gt;                                        trans :: [Transition],&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   4:&lt;/SPAN&gt;                                        setMap :: Map.Map (Set Node) Integer,&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   5:&lt;/SPAN&gt;                                        setStack :: [Set Node],&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   6:&lt;/SPAN&gt;                                        begin :: Node,&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   7:&lt;/SPAN&gt;                                        accept :: Set Node,&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   8:&lt;/SPAN&gt;                                        nextNode :: Node&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   9:&lt;/SPAN&gt;                                      } deriving (Show, Eq)&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;  10:&lt;/SPAN&gt; type ConvertState a = State ConvertContext a&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;Most of the code in this module is just managing this context and updating it according to two operation:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Epsilon Closure&lt;/LI&gt;&lt;LI&gt;Set Move&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;These are explained in more detail in &lt;A href="http://www.codeproject.com/KB/recipes/OwnRegExpressionsParser.aspx" mce_href="http://www.codeproject.com/KB/recipes/OwnRegExpressionsParser.aspx" target="_blank"&gt;this article&lt;/A&gt;. &lt;/P&gt;&lt;P&gt;Basically, epsilon closure is the process of taking a set of initial nodes and returning a new set of all nodes you can traverse to purely on epsilon transitions.  To help with this I created some smaller methods to build up to an epsilon closure.&lt;/P&gt;&lt;P&gt;First are a couple methods (findToNodes and closure):&lt;/P&gt;&lt;DIV style='border-right-color: #808080; border-right-width: 1px; border-right-style: solid; padding-right: 4px; border-top-color: #808080; border-top-width: 1px; border-top-style: solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; overflow: auto; border-left-color: #808080; border-left-width: 1px; border-left-style: solid; width: 97.50%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom-color: #808080; border-bottom-width: 1px; border-bottom-style: solid; font-family: Consolas, "Courier New", Courier, monospace; background-color: #f4f4f4'&gt;&lt;DIV style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   1:&lt;/SPAN&gt; closure trans value nodes oldSet = Set.union (findToNodes trans value nodes) oldSet&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   2:&lt;/SPAN&gt;  &lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   3:&lt;/SPAN&gt; -- Search the table of transitions to find all nodes you can reach given an initial set of nodes&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   4:&lt;/SPAN&gt; findToNodes trans value fromNodes = foldr match Set.empty trans&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   5:&lt;/SPAN&gt;     where &lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   6:&lt;/SPAN&gt;       match (from, to, val) nodes&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   7:&lt;/SPAN&gt;           | (from == fromNodes) &amp;amp;&amp;amp; (val == value) = Set.insert to nodes&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   8:&lt;/SPAN&gt;           | otherwise = nodes&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;STRONG&gt;findToNodes&lt;/STRONG&gt; searches a transition table for all nodes which go from any node in &lt;STRONG&gt;fromNodes&lt;/STRONG&gt; on &lt;STRONG&gt;value&lt;/STRONG&gt;.  It will builds up a set with all the &lt;STRONG&gt;to&lt;/STRONG&gt;  nodes that match.  &lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;closure &lt;/STRONG&gt;wraps findToNodes to let us easily union together an initial set and the nodes we can reach from that set.&lt;/P&gt;&lt;P&gt;With this in hand we can write clearly a epsilon closure function:&lt;/P&gt;&lt;DIV style='border-right-color: #808080; border-right-width: 1px; border-right-style: solid; padding-right: 4px; border-top-color: #808080; border-top-width: 1px; border-top-style: solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; overflow: auto; border-left-color: #808080; border-left-width: 1px; border-left-style: solid; width: 97.50%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom-color: #808080; border-bottom-width: 1px; border-bottom-style: solid; font-family: Consolas, "Courier New", Courier, monospace; background-color: #f4f4f4'&gt;&lt;DIV style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   1:&lt;/SPAN&gt; -- Given an initial set of nodes, find the set of all nodes you can reach by taking &lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   2:&lt;/SPAN&gt; -- transitions on epsilon only&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   3:&lt;/SPAN&gt; epsilonClosure trans nodes = foldUntilRepeat Set.union Set.empty $&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   4:&lt;/SPAN&gt;                              iterate (Set.fold (closure trans epsilon) Set.empty) nodes&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   5:&lt;/SPAN&gt;  &lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   6:&lt;/SPAN&gt;  &lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;This function takes full advantage of the lazy nature of Haskell.  It repeats the closure on epsilon over and over and streams its results into our function &lt;STRONG&gt;foldUntilRepeat.  &lt;/STRONG&gt;This method does what it says, it will fold the values that are streamed in until it sees the same value twice.  &lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;The set move is just combination of what you have already seen:&lt;/P&gt;&lt;DIV style='border-right-color: #808080; border-right-width: 1px; border-right-style: solid; padding-right: 4px; border-top-color: #808080; border-top-width: 1px; border-top-style: solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; overflow: auto; border-left-color: #808080; border-left-width: 1px; border-left-style: solid; width: 97.50%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom-color: #808080; border-bottom-width: 1px; border-bottom-style: solid; font-family: Consolas, "Courier New", Courier, monospace; background-color: #f4f4f4'&gt;&lt;DIV style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   1:&lt;/SPAN&gt; -- Given a starting set of nodes the set of all nodes that you can reach on a given value&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   2:&lt;/SPAN&gt; -- This includes epislonClosure on the desitination nodes&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   3:&lt;/SPAN&gt; moveClosure trans value nodes = epsilonClosure trans $ &lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   4:&lt;/SPAN&gt;                                 Set.fold (closure trans value) Set.empty nodes&lt;/PRE&gt;&lt;PRE style='padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin-top: 0em; margin-right: 0em; margin-bottom: 0em; margin-left: 0em; overflow: visible; width: 100%; color: #000000; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: Consolas, "Courier New", Courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none'&gt;&lt;SPAN style="color: #606060"&gt;   5:&lt;/SPAN&gt;  &lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;With these functions in hand, this module just becomes calling them and updating the context until we have no more nodes in the NFA to process.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;In the &lt;A href="http://blogs.msdn.com/matt/archive/2008/06/21/writing-a-regular-expression-parser-in-haskell-part-4.aspx" mce_href="http://blogs.msdn.com/matt/archive/2008/06/21/writing-a-regular-expression-parser-in-haskell-part-4.aspx"&gt;next installment&lt;/A&gt; I will discuss using the output of this modules to match a regex against a string.&lt;/P&gt;&lt;P&gt;Also, once again the code is attached.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8588925" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/matt/attachment/8588925.ashx" length="6161" type="application/octet-stream" /><category domain="http://blogs.msdn.com/matt/archive/tags/Haskell/default.aspx">Haskell</category><category domain="http://blogs.msdn.com/matt/archive/tags/Programming/default.aspx">Programming</category></item><item><title>Writing a Regular Expression parser in Haskell: Part 1</title><link>http://blogs.msdn.com/matt/archive/2008/06/01/writing-a-regular-expression-parser-in-haskell-part-1.aspx</link><pubDate>Sun, 01 Jun 2008 23:28:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8568217</guid><dc:creator>MattManela</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/matt/comments/8568217.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=8568217</wfw:commentRss><description>&lt;p&gt;A few weeks ago I read &lt;a href="http://www.codeproject.com/KB/recipes/OwnRegExpressionsParser.aspx" target="_blank"&gt;this article&lt;/a&gt; about writing a simple regular expression parser.&amp;nbsp; That article does a really good job of explaining the theory behind regular expression.&amp;nbsp; It then goes step by step into how to write a program (he uses C++) to parse a regular expression, convert it into a NFA, convert that into a DFA and then use that DFA to match strings.&lt;/p&gt; &lt;p&gt;After reading that I decided to write my own simple regular expression parser using Haskell.&amp;nbsp; I saw it as a challenge to try to see how you deal with a more complex program in a pure functional language.&amp;nbsp; After a couple weeks ( Grand Theft Auto 4 kind of ruined my progress for a while ) I have some results.&lt;/p&gt; &lt;p&gt;I split the project into 3 modules. &lt;/p&gt; &lt;ol&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/matt/archive/2008/06/02/writing-a-regular-expression-parser-in-haskell-part-2.aspx" target="_blank"&gt;RegexToNFA&lt;/a&gt; - Provides functionality to parse a simple regular expression and return a NFA.  &lt;ol&gt; &lt;li&gt;This modules also define the FiniteMachine type which is a general structure for finite state automata.&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;NFAtoDFA - Providers functionality to convert a NFA into a DFA.  &lt;ol&gt; &lt;li&gt;This module uses the same FiniteMachine type from RegexToNFA&lt;/li&gt;&lt;/ol&gt; &lt;li&gt;SimpleRegex - Provides the functionality to give take a regular expression and a string and return what it matches (if it matches anything).  &lt;ol&gt; &lt;li&gt;This modules uses RegexToNFA and sends its results to NFAtoDFA and then uses the resulting DFA to match against a string.&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;This is a very simple and limited regular expression parser.&amp;nbsp; It supports only union(|), concatenation, closure(*) and parenthesis.&amp;nbsp; In addition, I don't preserve information after the NFA is created about the location of the parenthesis.&amp;nbsp; This means you can't pull out sub-matches when a entire expression matches.&lt;/p&gt; &lt;p&gt;In my next three I will talk about each module and point out interesting parts of them.&amp;nbsp; There is nothing too complex but shows how to approach it in Haskell (making heavy use of the State monad).&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;If you want to see a much more complex and full featured regular expression parser written in Haskell take a look at &lt;a href="http://www.dcs.gla.ac.uk/~meurig/regexp/" target="_blank"&gt;this&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;a href="http://blogs.msdn.com/matt/archive/2008/06/02/writing-a-regular-expression-parser-in-haskell-part-2.aspx" target="_blank"&gt;Click here&lt;/a&gt; to continue to Part 2.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8568217" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matt/archive/tags/Haskell/default.aspx">Haskell</category><category domain="http://blogs.msdn.com/matt/archive/tags/pro/default.aspx">pro</category></item><item><title>Breadth First Tree Traversal in Haskell</title><link>http://blogs.msdn.com/matt/archive/2008/05/11/breadth-first-tree-traversal-in-haskell.aspx</link><pubDate>Mon, 12 May 2008 05:30:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8491028</guid><dc:creator>MattManela</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/matt/comments/8491028.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=8491028</wfw:commentRss><description>&lt;p&gt;As my interest in functional languages has grown, I have become increasingly interested in using them to implement algorithms which I can already write with imperative languages.&lt;/p&gt; &lt;p&gt;For example, I was taught to implement (and I assume most other people as well)&amp;nbsp; a breadth first traversal of a tree using a queue and a loop.&amp;nbsp; An example using this method can be found at the &lt;a href="http://en.wikipedia.org/wiki/Breadth-first_search" target="_blank"&gt;wikipedia page for a breadth first search&lt;/a&gt;.&amp;nbsp; When I wanted to try implement a breadth first search in Haskell I quickly realized that algorithm wouldn't port over very well.&amp;nbsp; I thought a bit and was able to come up with this algorithm:&lt;/p&gt; &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt; &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; -- My Implementation&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; breadth :: Tree a -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; [a]&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt; breadth nd =  map rootLabel $ nd : (breadth' [nd])&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;     where             &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;       breadth' [] = []&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;       breadth' nds = let cs = foldr ((++).subForest) [] nds in&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt;                      cs ++ breadth' cs&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The idea was that each call to breadth' takes a list of nodes (which represents of level of the tree) and will concatenate the children of each of those nodes together and recursively call itself again with that list.&amp;nbsp; This works but its not pretty Haskell.&amp;nbsp; When choosing Haskell (from what I have learned) it is best if you can avoid explicit recursion and use built in combinators.&amp;nbsp; After I coded my breadth first traversal function I decided to look into the Haskell standard libraries to see how it is done there.&amp;nbsp; What I found was a function called levels which returns a list of lists, where each sub-list is a level of the tree.&amp;nbsp; I slightly modified this to have the same functionality as my breadth function which creates one list of all the nodes in the breadth first order.&lt;/p&gt;
&lt;p&gt;This is the resulting code:&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;
&lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; -- Haskell Standard Libraray Implementation&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; br :: Tree a -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; [a]&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt; br t = map rootLabel $&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;        concat $&lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;        takeWhile (not . null) $                &lt;/pre&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;        iterate (concatMap subForest) [t]&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This is really slick implementation of what I did above.&amp;nbsp; The algorithm is the same but the way they went about writing it is so much prettier. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8491028" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matt/archive/tags/Haskell/default.aspx">Haskell</category><category domain="http://blogs.msdn.com/matt/archive/tags/Programming/default.aspx">Programming</category></item><item><title>When I am finally starting to get a handle on Monads...</title><link>http://blogs.msdn.com/matt/archive/2008/04/05/when-i-am-finally-starting-to-get-a-handle-on-monads.aspx</link><pubDate>Sun, 06 Apr 2008 09:59:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8362432</guid><dc:creator>MattManela</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/matt/comments/8362432.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=8362432</wfw:commentRss><description>&lt;p&gt;When I am finally starting to get a handle on Monads I discovered Arrows and I am thrown back into confusion.&amp;nbsp; At least that is how it feels.&amp;nbsp; In my exploration of Haskell I have spent a good deal of time trying to completely grasp and understand Monads.&amp;nbsp; I have read much about it and feel pretty good about my understanding of them now.&amp;nbsp; I am no expert but I at least am no longer afraid when I see or read about them.&lt;/p&gt; &lt;p&gt;But then I came across Arrows.&amp;nbsp; Arrows (from what I understand) are a further abstraction on top of Monads which are themselves abstractions.&amp;nbsp; What does this mean? I don't know.&lt;/p&gt; &lt;p&gt;I encountered arrows when I was looking into using the &lt;a href="http://www.fh-wedel.de/~si/HXmlToolbox/index.html"&gt;Haskell XML Toolkit&lt;/a&gt;.&amp;nbsp; I was able to get a sample working (which I am planning to write about soon) using the toolkit and Arrows but it felt like shooting a pistol in the dark.&amp;nbsp; &lt;/p&gt; &lt;p&gt;When I post the sample I created I will try my best to describe what I understand about it but I feel like this is going to be a slow road before I get a grasp on Arrows.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8362432" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matt/archive/tags/Haskell/default.aspx">Haskell</category><category domain="http://blogs.msdn.com/matt/archive/tags/Personal/default.aspx">Personal</category></item><item><title>Lazy Prime Number Sieve in C#</title><link>http://blogs.msdn.com/matt/archive/2008/03/16/lazy-prime-number-sieve-in-c.aspx</link><pubDate>Mon, 17 Mar 2008 09:28:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8281800</guid><dc:creator>MattManela</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/matt/comments/8281800.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=8281800</wfw:commentRss><description>&lt;p&gt;In my &lt;a href="http://blogs.msdn.com/matt/archive/2008/03/14/digging-deeper-into-lazy-and-functional-c.aspx" target="_blank"&gt;last post&lt;/a&gt; I talked about a Stream class for creating efficient lazy lists in C#.&amp;nbsp; In addition, I showed several classic functional methods I ported to C# to be used on the lazy lists.&amp;nbsp; As I mentioned in that post, I will now talk about an example I included in the &lt;a href="https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=matt&amp;amp;ReleaseId=669" target="_blank"&gt;source code&lt;/a&gt; for the lazy list class.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;The example is an implementation of a naive sieve for generating prime numbers. This prime generator is a classic example of lazy list evaluation in Haskell.&amp;nbsp; This is the sieve in Haskell:&lt;/p&gt; &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;primes = sieve [2..]
sieve (p : xs) = p : sieve [x | x &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;− xs, x ‘mod‘ p &lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; 0]&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This sieve is often mistakenly called the Sieve of Eratosthenes.&amp;nbsp; This sieve behaves differently and is extremely less efficient.&amp;nbsp; An explanation on how the sieve above is different from the Sieve of Eratosthenes can be found in &lt;a href="http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf" target="_blank"&gt;this paper by Melissa E. O’Neill&lt;/a&gt;.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Irregardless" target="_blank"&gt;Irregardless&lt;/a&gt; of the fact that this is not the Sieve of Eratosthenes it still exhibits the beauty of lazy lists.&amp;nbsp; Each invocation of the sieve method gets passed a new lazy list with a filter applied to it.&amp;nbsp; The filter excuted on a per element basis when it is needed on an element.&amp;nbsp; This code is really pretty and exhibits the idea of filtering an infinite stream and passing this filtered stream into another method to be filtered again.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;With the stream class from my last post and some of the method I defined we can create this method in C# (although with out the same elegant syntax since we don't have list comprehension).&amp;nbsp; My definition in C# is show below:&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;Stream&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; naturals = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
naturals = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Stream&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt;(0, () =&amp;gt; ones.ZipWith(naturals, (x, y) =&amp;gt; x + y));
Stream&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; naturalsFromTwo = naturals.Tail.Tail;

Stream&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; primes = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
primes = Primes(naturalsFromTwo);

&lt;span style="color: #008000"&gt;// ... &lt;/span&gt;

&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Stream&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; Primes(Stream&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; xs)
{
    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Stream&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt;(xs.Head, () =&amp;gt; Primes(xs.Tail.Filter(x =&amp;gt; x % xs.Head != 0)));
}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;While this code looks much uglier it is doing the same thing.&amp;nbsp; First of all, &lt;strong&gt;naturals&lt;/strong&gt;, is a stream which is all the natural numbers.&amp;nbsp; I then create &lt;strong&gt;naturalsFromTwo&lt;/strong&gt; which is the same as [2..] which you saw in the Haskell code.&amp;nbsp; The Primes function is the same as the sieve function in the Haskell code.&amp;nbsp; It takes a stream and creates a new stream with &lt;strong&gt;all&lt;/strong&gt; of the next primes multiples filtered out of it.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Beautiful!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8281800" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matt/archive/tags/Haskell/default.aspx">Haskell</category><category domain="http://blogs.msdn.com/matt/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/matt/archive/tags/Programming/default.aspx">Programming</category></item><item><title>Digging deeper into C# Lazy Lists</title><link>http://blogs.msdn.com/matt/archive/2008/03/14/digging-deeper-into-lazy-and-functional-c.aspx</link><pubDate>Sat, 15 Mar 2008 09:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8221643</guid><dc:creator>MattManela</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/matt/comments/8221643.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=8221643</wfw:commentRss><description>&lt;p&gt;One of the most interesting aspects of the Haskell language is the fact that features lazy evaluation.&amp;nbsp; My interest in lazy evaluation led me to a post on &lt;a href="http://blogs.msdn.com/wesdyer/default.aspx" target="_blank"&gt;Wes Dyers&lt;/a&gt; blog about &lt;a href="http://blogs.msdn.com/wesdyer/archive/2007/02/13/the-virtues-of-laziness.aspx" target="_blank"&gt;lazy lists in C#.&lt;/a&gt;&amp;nbsp; In his blog post he talks describes how to create a lazy list class in C# and demonstrates some classic lazy and functional methods of manipulating them.&amp;nbsp; After reading this I decided to use his code as a basis to some further experimentation.&lt;/p&gt; &lt;p&gt;I started playing with his code and modifying it to make it easier to use.&amp;nbsp; The end result was a class and a set of methods which allow you to easily use and manipulate lazy lists in C#. I will list some changes/ additions I made below:&lt;/p&gt; &lt;h3&gt;Stream Class&lt;/h3&gt; &lt;p&gt;I first changed the names &lt;strong&gt;next&lt;/strong&gt;&amp;nbsp; and &lt;strong&gt;value&lt;/strong&gt; from Wes's example to head and tail since I feel that makes more sense.&lt;/p&gt; &lt;p&gt;I then modified the stream class to implement IEnumerable&amp;lt;T&amp;gt;.&amp;nbsp; This will allow the stream to be used more seamlessly in code and act just as any other collection does.&amp;nbsp; The implementation for GetEnumerator was pretty straight forward:&lt;/p&gt; &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IEnumerator&amp;lt;T&amp;gt; GetEnumerator()
{
    &lt;span style="color: #0000ff"&gt;yield&lt;/span&gt; &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; head;

    var t = tail();
    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (t != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
    {
        &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var x &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; t)
            &lt;span style="color: #0000ff"&gt;yield&lt;/span&gt; &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; x;
    }

}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The third change I made to the Stream class was to not use his &lt;a href="http://blogs.msdn.com/wesdyer/archive/2007/01/26/function-memoization.aspx" target="_blank"&gt;Memoize&lt;/a&gt; method but to add another field in the stream class called &lt;strong&gt;realized &lt;/strong&gt;which will store the value of tail after its first use.&amp;nbsp; I felt this was easier to read.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
&lt;h2&gt;
&lt;h3&gt;&lt;strong&gt;Lazy List Functions&lt;/strong&gt;&lt;/h3&gt;&lt;/h2&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;In Wes's post he shows how to implement Zip (well he actually showed ZipWith but called it Zip) and Map on the stream class.&amp;nbsp; I added to this by first converting them to extension methods just for convenience.&amp;nbsp; Then I added many other methods over lists that any functional programmer is used to having like:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fold&lt;/strong&gt;&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; T FoldR&amp;lt;U, T&amp;gt;(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; Stream&amp;lt;U&amp;gt; st1, Func&amp;lt;U, T, T&amp;gt; folder, T init)
{
    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (st1 == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;) &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; init;
    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (st1 != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; st1.Tail == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;) &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; folder(st1.Head, init);
    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; folder(st1.Head, st1.Tail.FoldR(folder, init));
}

&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; T FoldL&amp;lt;U, T&amp;gt;(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; Stream&amp;lt;U&amp;gt; st1, Func&amp;lt;T, U, T&amp;gt; folder, T init)
{
    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (st1 == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;) &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; init;
    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; st1.Tail.FoldL(folder, folder(init, st1.Head));
}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;and &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Filter&lt;/strong&gt;&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Stream&amp;lt;T&amp;gt; Filter&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; Stream&amp;lt;T&amp;gt; st1, Func&amp;lt;T, &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt;&amp;gt; filter)
{
    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (st1 == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;) &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (filter(st1.Head))
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Stream&amp;lt;T&amp;gt;(st1.Head, () =&amp;gt; st1.Tail.Filter(filter));
    &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; st1.Tail.Filter(filter);
}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Then to top it all off I added an extension method AsStream to allow you as convert any IEnumerable into a Stream:&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Stream&amp;lt;T&amp;gt; AsStream&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; coll)
{
    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; coll.GetEnumerator().AsStream();
}

&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Stream&amp;lt;T&amp;gt; AsStream&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; IEnumerator&amp;lt;T&amp;gt; enumer)
{
    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (enumer.MoveNext())
    {
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Stream&amp;lt;T&amp;gt;(enumer.Current, () =&amp;gt; enumer.AsStream());
    }
    &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;
    {
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
    }
}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;With all of these extension methods you will now be able to take a running start at Lazy Lists in C#.&lt;/p&gt;
&lt;p&gt;I posted the code I worked on on &lt;a href="http://code.msdn.microsoft.com/" target="_blank"&gt;Code Gallery&lt;/a&gt; so &lt;a href="https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=matt&amp;amp;ReleaseId=669" target="_blank"&gt;click here&lt;/a&gt; if you want to download it an give it a spin.&lt;/p&gt;
&lt;p&gt;The code contains several examples of using the stream class and also a lazy prime generator which I will discuss in my next post.&lt;/p&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8221643" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matt/archive/tags/Haskell/default.aspx">Haskell</category><category domain="http://blogs.msdn.com/matt/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/matt/archive/tags/Programming/default.aspx">Programming</category></item><item><title>Who would have thunk it?</title><link>http://blogs.msdn.com/matt/archive/2007/12/18/who-would-have-thunk-it.aspx</link><pubDate>Wed, 19 Dec 2007 09:40:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6803127</guid><dc:creator>MattManela</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/matt/comments/6803127.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=6803127</wfw:commentRss><description>&lt;p&gt; I recently read this article about &lt;a href="http://msdn2.microsoft.com/vcsharp/bb870976.aspx"&gt;Lazy Computation in C#&lt;/a&gt;. What the article discusses is creating lazy evaluation in C#.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Lazy evaluation is a key feature of functional languages like Haskell but is not common in imperative languages.&amp;nbsp; It is used in Haskell because it implements parameter passing on a &lt;a href="http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_need" target="_blank"&gt;call by need&lt;/a&gt; basis.&amp;nbsp; What this means is that a functions arguments are not executed until they are used but once used the argument retains its calculated value.&amp;nbsp; This is good because it prevents wasted computation since if a argument isn't used it isn't executed.&lt;/p&gt; &lt;p&gt;The article goes into details about how to create lazily evaluated arguments and why they are useful.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Now, I don't know if anyone else found this posts title funny but I did because a lazily evaluated argument used in Haskell and that the article shows how to create is commonly referred to as a &lt;a href="http://en.wikipedia.org/wiki/Thunk" target="_blank"&gt;Thunk&lt;/a&gt;.&amp;nbsp; :)&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6803127" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matt/archive/tags/Haskell/default.aspx">Haskell</category><category domain="http://blogs.msdn.com/matt/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/matt/archive/tags/Programming/default.aspx">Programming</category></item><item><title>Lambda Expressions are more fun in Visual Basic .NET</title><link>http://blogs.msdn.com/matt/archive/2007/12/06/lambda-expressions-are-more-fun-in-visual-basic-net.aspx</link><pubDate>Fri, 07 Dec 2007 09:33:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6689633</guid><dc:creator>MattManela</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/matt/comments/6689633.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=6689633</wfw:commentRss><description>&lt;p&gt;I love C# and I would never want to do anything to make it seem any less amazing but I have to give credit where credit is due ... to Visual Basic .NET.&amp;nbsp; Yes, I said it.&amp;nbsp; Both languages have been adding features inspired by the functional and dynamic programing world.&amp;nbsp; However, VB .NET has went a few steps further to truly merge imperative, object oriented, dynamic and functional programing into one convenient package.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Much of this functionality comes from the fact that VB .NET has late binding built in to many of its operations.&amp;nbsp; This allows the programmer to be more expressive and "get away" with more.&amp;nbsp; I was aware that VB .NET had some unique features but as a C# programmer I never got around to playing around with it to see how they feel.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;This changed recently when I was fooling around with lambda expressions in C#.&amp;nbsp; I have been casually playing and experimenting with &lt;a href="http://en.wikipedia.org/wiki/Haskell_(programming_language)" target="_blank"&gt;Haskell&lt;/a&gt; (a pure functional programing language) for about a year now and I have become very accustomed with just writing a lambda expression and not worrying about defining its type.&lt;/p&gt; &lt;p&gt;With C# 3.0 I assumed I would be able to do the same thing.&amp;nbsp; I wanted to be able to write C# code that is similar to the following Haskell code:&lt;/p&gt; &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;f = \x -&amp;gt; x + x&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I figured this would be done easily in C# with the lambda expressions and implicitly typed variables. So I wrote:&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt; f = x =&amp;gt; x + x;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I was very pleased with this code, it looked very close to the Haskell.&amp;nbsp; It is concise, clean and quick however it doesn't compile.&amp;nbsp; You get this error:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font color="#ff0000"&gt;Cannot assign lambda expression to an implicitly-typed local variable&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The problem is that C# is strongly typed and is unable to determine what type f should be since it cannot determine what type x is.&amp;nbsp; So, you would have to write something like this:&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;Func&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;,&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; f = x =&amp;gt; x + x;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now, that isn't hard to do but I don't want to have to write out that type every time I want to create a lambda expression.&lt;/p&gt;
&lt;p&gt;I tried the same lambda expression in Visual Basic .NET and ...:&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; f = &lt;span style="color: #0000ff"&gt;Function&lt;/span&gt;(x) x + x&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Tada!, that worked like a charm.&amp;nbsp; And when I call f(2) it returns 4, and when I call f("matt") it would return "mattmatt".&lt;/p&gt;
&lt;p&gt;This was the behavior I was looking for.&amp;nbsp; This peaked my interest in VB .NET and since then I have been exploring features in it and seeing how it performs late binding to make this all happen.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;After I experiment some more I will write in detail about what is really going on behind the scenes in the VB .NET Compiler.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6689633" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matt/archive/tags/Haskell/default.aspx">Haskell</category><category domain="http://blogs.msdn.com/matt/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/matt/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/matt/archive/tags/Visual+Basic/default.aspx">Visual Basic</category></item></channel></rss>