<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" media="screen" href="/App_Themes/default/rss.xslt"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:evnet="http://www.mscommunities.com/rssmodule/"><channel><title>Comment Feed for Brian Beckman: The Zen of Stateless State - The State Monad - Part 1 (Going Deep on Channel 9)</title><atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/shows/going+deep/brian-beckman-the-zen-of-expressing-state-the-state-monad/rss/default.aspx" /><image><url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url><title>Comment Feed for Brian Beckman: The Zen of Stateless State - The State Monad - Part 1 (Going Deep on Channel 9)</title><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/</link></image><description>Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</description><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/</link><language>en-us</language><pubDate>Wed, 26 Aug 2009 19:12:57 GMT</pubDate><lastBuildDate>Wed, 26 Aug 2009 19:12:57 GMT</lastBuildDate><generator>EvNet (EvNet, Version=1.0.3608.3122, Culture=neutral, PublicKeyToken=null)</generator><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>&lt;p&gt;Thanks for including the visio diagrams. Will print them and look at the code...&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=486999</link><pubDate>Wed, 26 Aug 2009 19:12:58 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=486999</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/486999/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Thanks for including the visio diagrams. Will print them and look at the code...</evnet:previewtext><dc:creator>Bent Rasmussen</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/486999/Trackback.aspx</trackback:ping></item><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>&lt;p&gt;This looks very nice. I don't see any ways right off the top of my head to make it shorter (shorter is almost always better :)&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=486500</link><pubDate>Mon, 24 Aug 2009 02:05:05 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=486500</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/486500/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>This looks very nice. I don't see any ways right off the top of my head to make it shorter (shorter is almost always better :)</evnet:previewtext><dc:creator>brianbec</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/486500/Trackback.aspx</trackback:ping></item><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>&lt;p&gt;Here's my solution to exercise 2 using F#:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The type defs:&lt;/p&gt;
&lt;p&gt;
&lt;div&gt;type Rect =&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{ Height: float;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;Width: float;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;Top: float;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;Left: float }&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;with override r.ToString() = String.Format("Height: {0}, Width: {1}, Top: {2}, Left: {3}", r.Height, r.Width, r.Top, r.Left)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp;(* StateBuilder Bounder *)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp;let boundTree tree seed leftUpdater rightUpdater =&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;let rec labelTree t updater =&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;match t with&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;| Leaf(c) &amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt; state { let! s = getState&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;let (next, curr) = updater s&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;do! setState next&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return Leaf(curr, c) }&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;| Branch(l, r) -&amp;gt; state { let! l = labelTree l leftUpdater&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;let! r = labelTree r rightUpdater&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return Branch(l, r) }&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;exec (labelTree tree leftUpdater) seed&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;type Rect =&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{ Height: float;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;Width: float;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;Top: float;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;Left: float }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;with override r.ToString() = String.Format("Height: {0}, Width: {1}, Top: {2}, Left: {3}", r.Height, r.Width, r.Top, r.Left)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;type Tree&amp;lt;'a&amp;gt; =&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;| Leaf of 'a&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;| Branch of Tree&amp;lt;'a&amp;gt; * Tree&amp;lt;'a&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;// The show method already takes into account the tuples,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;// unlike the show method that is overloaded in the C# version.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;let show tree =&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;let rec printTree tree level =&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;let spacing = new string(' ', level * indentation)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;printf "%A" spacing&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;match tree with&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;| Leaf(label, contents) -&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;let labelString = label.ToString()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;printfn "Leaf: %s, Contents: %A" labelString contents&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;| Branch(left, right) -&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;printfn "Branch: "&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;printTree left (level + 1)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;printTree right (level + 1)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;printTree tree 0&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
The state monad builder:
&lt;p&gt;&amp;nbsp;&amp;nbsp;(* StateMonad *)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;type State&amp;lt;'S, 'a&amp;gt; = State of ('S -&amp;gt; 'S * 'a)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;(* StateBuilder Bounder *)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;let boundTree tree seed leftUpdater rightUpdater =&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;let rec labelTree t updater =&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;match t with&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;| Leaf(c) &amp;nbsp; &amp;nbsp; &amp;nbsp;-&amp;gt; state { let! s = getState&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;let (next, curr) = updater s&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;do! setState next&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return Leaf(curr, c) }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;| Branch(l, r) -&amp;gt; state { let! l = labelTree l leftUpdater&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;let! r = labelTree r rightUpdater&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return Branch(l, r) }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;exec (labelTree tree leftUpdater) seed&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;let eval sm s =&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;match sm with&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;| State f -&amp;gt; f s |&amp;gt; fst&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;let exec sm s =&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;match sm with&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;| State f -&amp;gt; f s |&amp;gt; snd&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The demo tree:&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;let demoTree = Branch(&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Leaf("A"),&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Branch(&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Branch(&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Leaf("B"),&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Leaf("C")),&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Leaf("D")))&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The updaters and execution code:&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;let leftBounder =&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;fun (depth, rect) -&amp;gt; let newDepth = depth + 1.0&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; let multiplier = 2.0 * newDepth&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ( (newDepth,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{ Height = rect.Height&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Width = rect.Width / multiplier&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Top = rect.Top&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Left = rect.Left + rect.Width / multiplier }),&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; { Height = rect.Height&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Width = rect.Width / multiplier&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Top = rect.Top&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Left = rect.Left } )&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;let rightBounder =&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;fun (depth, rect) -&amp;gt; let newDepth = depth - 1.0&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ( (newDepth,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{ Height = rect.Height&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Width = rect.Width * 2.0&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Top = rect.Top&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Left = rect.Left + rect.Width }),&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; rect )&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;let initialDepth = 0.0&lt;/p&gt;
&lt;p&gt;let initialRect = { Height = 100.0&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Width = 100.0&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Top = 0.0&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Left = 0.0 } &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt;let ex2Seed = (initialDepth, initialRect)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;printfn "Bound tree to rects"&lt;/p&gt;
&lt;p&gt;let bTree = boundTree demoTree ex2Seed leftBounder rightBounder&lt;/p&gt;
&lt;p&gt;show bTree&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This works, but I imagine there must be a better way. I'm open to suggestions.&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=486490</link><pubDate>Sun, 23 Aug 2009 22:49:13 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=486490</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/486490/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Here's my solution to exercise 2 using F#:
&amp;nbsp;
The type defs:

type Rect =
&amp;nbsp;&amp;nbsp; &amp;nbsp;{ Height: float;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;Width: float;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;Top: float;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;Left: float }
&amp;nbsp;&amp;nbsp; &amp;nbsp;with override r.ToString() =&amp;#8230;</evnet:previewtext><dc:creator>Ryan Riley</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/486490/Trackback.aspx</trackback:ping></item><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>&lt;p&gt;"So... any solutions available for the exercises above?" &amp;lt;| Geez! Maybe I should look to see if pages of comments exist before displaying my stupidity to the world. Great work holoed!&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=482016</link><pubDate>Fri, 31 Jul 2009 22:57:34 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=482016</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/482016/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>"So... any solutions available for the exercises above?" &amp;lt;| Geez! Maybe I should look to see if pages of comments exist before displaying my stupidity to the world. Great work holoed!</evnet:previewtext><dc:creator>Ryan Riley</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/482016/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>Indeed. I bet Brian's excited to dig into the Continuation monad... :)&lt;BR&gt;C</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=448609</link><pubDate>Mon, 15 Dec 2008 23:53:55 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=448609</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/448609/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Indeed. I bet Brian's excited to dig into the Continuation monad... :)C</evnet:previewtext><dc:creator>Charles</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/448609/Trackback.aspx</trackback:ping></item><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>woah holoed you Are the man :S i havent had time to&amp;nbsp;write out&amp;nbsp;the excercises yet(but ive tried to solve them in my head :) ), 8 people (out of 35) are beeling let go from where i work and i have to take over like 2 positions plus my own :S nut no more excuses :) i really really enjoyed this interview+excercises :)&lt;BR&gt;&lt;BR&gt;please pretty please charles and brian,&amp;nbsp; do intervies like this about the other monads as well :)</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=448600</link><pubDate>Mon, 15 Dec 2008 22:48:10 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=448600</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/448600/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>woah holoed you Are the man :S i havent had time to&amp;nbsp;write out&amp;nbsp;the excercises yet(but ive tried to solve them in my head :) ), 8 people (out of 35) are beeling let go from where i work and i have to take over like 2 positions plus my own :S nut no more excuses :) i really really enjoyed&amp;#8230;</evnet:previewtext><dc:creator>Allan Lindqvist</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/448600/Trackback.aspx</trackback:ping></item><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>&lt;pre&gt;
// The List Monad in F#&lt;/pre&gt;&lt;pre&gt;#light

type List&amp;lt;'a&gt; = List of ('a -&gt; 'a list)

type ListMonad() =
    member o.Bind(  (m:'a list), (f: 'a -&gt; 'b list) ) = List.concat( List.map (fun x -&gt; f x) m )
    member o.Return(x) = [x]

let list = ListMonad()

let cartesian = list { let! x = [1..3]
                       let! y = [4..6]
                       return (x,y) }

printf "%A" cartesian
&lt;/pre&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=448367</link><pubDate>Sat, 13 Dec 2008 19:58:36 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=448367</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/448367/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>// The List Monad in F##light

type List&amp;lt;'a&gt; = List of ('a -&gt; 'a list)

type ListMonad() =
    member o.Bind(  (m:'a list), (f: 'a -&gt; 'b list) ) = List.concat( List.map (fun x -&gt; f x) m )
    member o.Return(x) = [x]

let list = ListMonad()

let cartesian = list { let! x = [1..3]
                &amp;#8230;</evnet:previewtext><dc:creator>Edmondo Pentangelo</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/448367/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>Thank you Charles :)&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;I've updated my profile:&lt;/div&gt;&lt;div&gt;&lt;a href="http://channel9.msdn.com/Niners/holoed/"&gt;http://channel9.msdn.com/Niners/holoed/&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Edmondo Pentangelo&lt;/div&gt;&lt;div&gt;Deutsche Bank &lt;/div&gt;&lt;div&gt;London, UK&lt;/div&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=447375</link><pubDate>Sat, 06 Dec 2008 19:47:04 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=447375</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/447375/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Thank you Charles :)I've updated my profile:http://channel9.msdn.com/Niners/holoed/Edmondo PentangeloDeutsche Bank London, UK</evnet:previewtext><dc:creator>Edmondo Pentangelo</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/447375/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>&lt;p&gt;&lt;p&gt;Thank you Brian :)&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;

&lt;p&gt;I love this stuff. I've been an OO developer for a long time
and it is only thank to F# and videos like yours that I started appreciating
the beauty of functional ideas. I think my coding at work is improving as a result
of this exposure.&lt;br&gt;&lt;br&gt;I
plan to have exercise 10 ready before the end of the year ;)))&lt;/p&gt;&lt;p&gt;HoloEd&lt;/p&gt;&lt;/p&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=447374</link><pubDate>Sat, 06 Dec 2008 19:43:06 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=447374</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/447374/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Thank you Brian :)

I love this stuff. I've been an OO developer for a long time
and it is only thank to F# and videos like yours that I started appreciating
the beauty of functional ideas. I think my coding at work is improving as a result
of this exposure.I
plan to have exercise 10 ready before the end of the year ;)))HoloEd</evnet:previewtext><dc:creator>Edmondo Pentangelo</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/447374/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>Indeed! Hey, holoed. Who are you, man? :)&lt;BR&gt;C</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=447122</link><pubDate>Fri, 05 Dec 2008 05:31:22 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=447122</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/447122/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Indeed! Hey, holoed. Who are you, man? :)C</evnet:previewtext><dc:creator>Charles</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/447122/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>Absolutely heroic!</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=447105</link><pubDate>Fri, 05 Dec 2008 01:34:13 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=447105</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/447105/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Absolutely heroic!</evnet:previewtext><dc:creator>brianbec</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/447105/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>This is true, Ben, but suppose you could think of the entire state of the CLR &amp;amp; heap memory as being the State that gets passed around inside monads of type s -&amp;gt; (s, v)? Could you build up your whole program as "@bind" compositions of things that take "the state of the entire CLR" as argument and return pairs of ("state of the entire CLR", "some values I care about")? This isn't a rhetorical question, I'm not sure I know how to do it :)&amp;nbsp; But it is the question I'm posing to niners in Exercise 10. Of course, that "state of the entire CLR" is just a fictional state object, so not heavyweight to "pass around." It's the ambient monad, I think. But not sure how to guarantee that nothing but the function active at a given moment can modify that state, even if it is possible.&lt;BR&gt;&lt;BR&gt;(btw, in my Caltech days, exams and homework problem sets routinely contained questions the professors didn't know how to answer: open research questions i.o.w.)</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=447104</link><pubDate>Fri, 05 Dec 2008 01:33:44 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=447104</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/447104/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>This is true, Ben, but suppose you could think of the entire state of the CLR &amp;amp; heap memory as being the State that gets passed around inside monads of type s -&amp;gt; (s, v)? Could you build up your whole program as "@bind" compositions of things that take "the state of the entire CLR" as argument&amp;#8230;</evnet:previewtext><dc:creator>brianbec</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/447104/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>Nice work (all around)! Thanks for sharing.&lt;BR&gt;C</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446536</link><pubDate>Wed, 03 Dec 2008 04:33:47 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446536</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/446536/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Nice work (all around)! Thanks for sharing.C</evnet:previewtext><dc:creator>Charles</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/446536/Trackback.aspx</trackback:ping></item><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>&lt;pre&gt;
//F# Monadic Parser - Calculator Example :))) 

// Grammar
//expr ::= expr addop term j term
//term ::= term mulop factor j factor
//factor ::= digit j ( expr )
//digit ::= 0 j 1 j : : : j 9
//addop ::= + j -
//mulop ::= * j /

let addOp = parser { let! _ = symb "+" 
                     return (+) } +++ parser { let! _ = symb "-" 
                                               return (-) } 
let mulOp = parser { let! _ = symb "*" 
                     return (*) } +++ parser { let! _ = symb "/" 
                                               return (/) }  
let digit = parser { let! x = token (sat (fun ch -&gt; Char.IsDigit(ch)))
                     return Char.GetNumericValue(x) - Char.GetNumericValue('0') }

let rec expr   = chainl1 term addOp
and     term   = chainl1 factor mulOp
and     factor = digit +++ parser { let! _ = symb "("
                                    let! n = expr
                                    let! _ = symb ")"
                                    return n }
                                    
let parse s = match apply expr s  with
              | [] -&gt; failwith "failed to parse"
              | (ret, _)::xs -&gt; ret 
                                    &lt;/pre&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446530</link><pubDate>Wed, 03 Dec 2008 02:54:02 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446530</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/446530/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>//F# Monadic Parser - Calculator Example :))) 

// Grammar
//expr ::= expr addop term j term
//term ::= term mulop factor j factor
//factor ::= digit j ( expr )
//digit ::= 0 j 1 j : : : j 9
//addop ::= + j -
//mulop ::= * j /

let addOp = parser { let! _ = symb "+" 
                     return (+)&amp;#8230;</evnet:previewtext><dc:creator>Edmondo Pentangelo</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/446530/Trackback.aspx</trackback:ping></item><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>&lt;pre&gt;
// F# Parsers combinators :))

//A parser which successfully consumes the first character
//if the argument string is non-empty, and fails otherwise.
let item = Parser (fun cs -&gt; match cs with
                             | "" -&gt; []
                             | cs -&gt; [cs.[0], cs.Substring(1)])
                            
//A combinator sat that takes a predicate, and yields a parser that
//consumes a single character if it satisfies the predicate, and fails otherwise.
let sat p = parser { let! c = item
                     if p c then
                      return c }
                      
//A parser for specific characters
let char c = sat (fun s -&gt; c = s)
                                            
//Parse a specific string
let rec stringp s = match s with
                    | "" -&gt; parser { return [] }
                    | xs -&gt; parser { let! c = char xs.[0]
                                     let! cs = stringp (xs.Substring(1))
                                     return c::cs }                        
                                                          
//The many combinator permits zero
//or more applications of p, while many1 permits one or more.
let rec many1 p = parser { let! x = p
                           let! xs = many p
                           return (x::xs) }
and many p = (many1 p) +++ parser { return [] }

                            
//Parse repeated applications of a parser p, separated by applications of a parser
//op whose result value is an operator that is assumed to associate to the left,
//and which is used to combine the results from the p parsers.
and chainl1 p op = 
    let rec rest a = parser { let! f = op
                              let! b = p
                              return! rest (f a b) } +++ parser { return a }
    parser { let! a = p
             return! rest a } 
           
//Parse a string of spaces.
let space = many (sat (fun s -&gt; Char.IsWhiteSpace s))

//Parse a token using a parser p, throwing away any trailing space.
let token p = parser { let! a = p
                       let! _ = space
                       return a }

//Parse a symbolic token:
let symb cs = token (stringp cs)

//Apply a parser p, throwing away any leading space:
let apply p = extract (parser { let! _ = space
                                let! r = p
                                return r })&lt;/pre&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446529</link><pubDate>Wed, 03 Dec 2008 02:51:17 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446529</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/446529/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>// F# Parsers combinators :))

//A parser which successfully consumes the first character
//if the argument string is non-empty, and fails otherwise.
let item = Parser (fun cs -&gt; match cs with
                             | "" -&gt; []
                             | cs -&gt; [cs.[0], cs.Substring(1)])
   &amp;#8230;</evnet:previewtext><dc:creator>Edmondo Pentangelo</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/446529/Trackback.aspx</trackback:ping></item><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>&lt;pre&gt;
// F# Parser Monad (http://www.cs.nott.ac.uk/~gmh/pearl.pdf)

type Parser&amp;lt;'a&gt; = Parser of (string -&gt;('a * string) list)

let extract(Parser(f)) = f

type ParserMonad() = 
        member b.Bind(p, f) = Parser (fun cs -&gt; 
                                                let r = extract(p) cs
                                                let r' = List.map (fun (a,cs') -&gt; extract(f a) cs') r
                                                List.concat r')
        member b.Return(x) = Parser (fun cs -&gt; [x,cs])
        member b.Zero() = Parser (fun cs -&gt; [])


let (++) p q = Parser(fun cs -&gt; List.append (extract p cs) (extract q cs))
let (+++) p q = Parser(fun cs -&gt; match (extract(p ++ q) cs) with
                                 | [] -&gt; []
                                 | x::xs -&gt; [x])

let parser = ParserMonad()
&lt;/pre&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446528</link><pubDate>Wed, 03 Dec 2008 02:48:34 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446528</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/446528/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>// F# Parser Monad (http://www.cs.nott.ac.uk/~gmh/pearl.pdf)

type Parser&amp;lt;'a&gt; = Parser of (string -&gt;('a * string) list)

let extract(Parser(f)) = f

type ParserMonad() = 
        member b.Bind(p, f) = Parser (fun cs -&gt; 
                                                let r = extract(p) cs
       &amp;#8230;</evnet:previewtext><dc:creator>Edmondo Pentangelo</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/446528/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>Brian, I think I get it now.&amp;nbsp; I was thrown off by the example.&amp;nbsp; It makes sense that you need to copy the state around to avoid side affects. &amp;nbsp; I guess the my practical (and procedural) side is too worried that the size of the state and the overhead of copying it over and over is too large for the scenarios I deal with.&amp;nbsp; &lt;br&gt;&lt;br&gt;I also agree with what you said in the video about when the project is intricate enough you end up having to deal with all these nasty problems with state dependency, locking, etc.&lt;br&gt;&lt;br&gt;Ben&lt;br&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446512</link><pubDate>Wed, 03 Dec 2008 00:48:48 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446512</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/446512/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Brian, I think I get it now.&amp;nbsp; I was thrown off by the example.&amp;nbsp; It makes sense that you need to copy the state around to avoid side affects. &amp;nbsp; I guess the my practical (and procedural) side is too worried that the size of the state and the overhead of copying it over and over is too&amp;#8230;</evnet:previewtext><dc:creator>benyaboy</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/446512/Trackback.aspx</trackback:ping></item><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>holoed, this is fantastic! +1000 pts :)</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446223</link><pubDate>Mon, 01 Dec 2008 04:27:44 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446223</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/446223/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>holoed, this is fantastic! +1000 pts :)</evnet:previewtext><dc:creator>brianbec</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/446223/Trackback.aspx</trackback:ping></item><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>&lt;pre&gt;//LINQ State Monad implementation&lt;/pre&gt;&lt;pre&gt;
public delegate StateContentPair&amp;lt;T&gt; StateMonad&amp;lt;T&gt;(int state);&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;
public static class LinqStateMonad
{
    public static StateMonad&amp;lt;U&gt; Select&amp;lt;T, U&gt;(this StateMonad&amp;lt;T&gt; p, Func&amp;lt;T, U&gt; selector)
    {
        return state =&gt; new StateContentPair&amp;lt;U&gt;(selector(p(state).Content), state);
    }
    public static StateMonad&amp;lt;V&gt; SelectMany&amp;lt;T, U, V&gt;(this StateMonad&amp;lt;T&gt; p, Func&amp;lt;T, StateMonad&amp;lt;U&gt;&gt; selector, Func&amp;lt;T, U, V&gt; projector)
    {
        return state =&gt;
        {
            var first = p(state);
            var second = selector(first.Content)(first.State);
            var content = projector(first.Content, second.Content);
            return new StateContentPair&amp;lt;V&gt;(content, second.State);
        };
    }

    public static StateMonad&amp;lt;int&gt; GetState()
    {
        return s =&gt; new StateContentPair&amp;lt;int&gt;(s, s);
    }

    public static StateMonad&amp;lt;T&gt; SetState&amp;lt;T&gt;(int s1)
    {
        return s =&gt; new StateContentPair&amp;lt;T&gt;(default(T), s1);
    }
}&lt;/pre&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446208</link><pubDate>Mon, 01 Dec 2008 01:16:13 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446208</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/446208/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>//LINQ State Monad implementation
public delegate StateContentPair&amp;lt;T&gt; StateMonad&amp;lt;T&gt;(int state);
public static class LinqStateMonad
{
    public static StateMonad&amp;lt;U&gt; Select&amp;lt;T, U&gt;(this StateMonad&amp;lt;T&gt; p, Func&amp;lt;T, U&gt; selector)
    {
        return state =&gt; new&amp;#8230;</evnet:previewtext><dc:creator>Edmondo Pentangelo</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/446208/Trackback.aspx</trackback:ping></item><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>&lt;pre&gt;//LINQ Monadic Labelling of a Binary Tree&lt;/pre&gt;&lt;pre&gt;
public static StateMonad&amp;lt;BTree&gt; MLabel(BTree tree)
{
    var branch = tree as BBranch;
    var leaf = tree as BLeaf;

    StateMonad&amp;lt;BTree&gt; ret = null;

    if (leaf != null)
    {
        var q = from x in LinqStateMonad.GetState()
                from f in LinqStateMonad.SetState&amp;lt;int&gt;(x + 1)
                select new BLeaf { Content = leaf.Content, State = x };
        ret = s =&gt; { var r = q(s); return new StateContentPair&amp;lt;BTree&gt;(r.Content, r.State); };
    }

    if (branch != null)
    {
        var q = from l in MLabel(branch.Left)
                from r in MLabel(branch.Right)
                select new BBranch() { Left = l, Right = r };
        ret = s =&gt; { var r = q(s); return new StateContentPair&amp;lt;BTree&gt;(r.Content, r.State); };
    }

    return ret;
}&lt;/pre&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446206</link><pubDate>Mon, 01 Dec 2008 01:05:36 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=446206</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/446206/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>//LINQ Monadic Labelling of a Binary Tree
public static StateMonad&amp;lt;BTree&gt; MLabel(BTree tree)
{
    var branch = tree as BBranch;
    var leaf = tree as BLeaf;

    StateMonad&amp;lt;BTree&gt; ret = null;

    if (leaf != null)
    {
        var q = from x in LinqStateMonad.GetState()
               &amp;#8230;</evnet:previewtext><dc:creator>Edmondo Pentangelo</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/446206/Trackback.aspx</trackback:ping></item><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>&lt;pre&gt;// F# Monadically label n-ary tree&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;
let MMap f xs = 
            let rec MMap' (f, xs', out) = 
                state {
                        match xs' with
                        | h :: t -&gt; let! h' = f(h)
                                    return! MMap'(f, t, List.append out [h'])
                        | [] -&gt; return out
                      }
            MMap' (f, xs, [])


let rec private MNNode (x) =
    match x with
    | NLeaf(c) -&gt; state { let! x = GetState
                          do! SetState (x + 1)
                          return NLeaf(c,x) }
                         
    | NNode(xs) -&gt; state { let! xs' = MMap MNNode xs
                           return NNode(xs')  } 
                    
let MNLabel x = Execute(MNNode(x))&lt;/pre&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=445665</link><pubDate>Fri, 28 Nov 2008 00:47:59 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=445665</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/445665/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>// F# Monadically label n-ary tree
let MMap f xs = 
            let rec MMap' (f, xs', out) = 
                state {
                        match xs' with
                        | h :: t -&gt; let! h' = f(h)
                                    return! MMap'(f, t, List.append out [h'])
             &amp;#8230;</evnet:previewtext><dc:creator>Edmondo Pentangelo</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/445665/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>&lt;P&gt;I see - so Haskell says "trust me, this won't last" and Haskell is undoubtedly correct!&lt;/P&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=445635</link><pubDate>Thu, 27 Nov 2008 18:35:48 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=445635</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/445635/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>I see - so Haskell says "trust me, this won't last" and Haskell is undoubtedly correct!</evnet:previewtext><dc:creator>Daniel Earwicker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/445635/Trackback.aspx</trackback:ping></item><item><title>Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>HINT for Ex 4: look up "mapM" in the Haskell libraries (&lt;A href="http://www.haskell.org"&gt;www.haskell.org&lt;/A&gt;)</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=445619</link><pubDate>Thu, 27 Nov 2008 15:55:56 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=445619</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/445619/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>HINT for Ex 4: look up "mapM" in the Haskell libraries (www.haskell.org)</evnet:previewtext><dc:creator>brianbec</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/445619/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>Ben, the State is "anything you like," and that's the point of some of the exercises: to package, say, layout information in the State. The mechanism doing the threading is the particular pattern of capturing values in closed-over variables (free variables inside functions from state to state-value pairs) and then doing the state threading by composition of "functions from state to state-value pairs." This pattern is so common as to deserve its own library, and, it just so happens that it satisfies the monad laws (associativity and right-left unit). So, as they used to say, don't you love it when a plan comes together? &lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;The result is conceptually small code that happens to be completely safe in a parallel, concurrent environment, but can do all kinds of stateful things like laying out visualizables. The code is syntactically a bit big in C#, but I'm really liking the way the F# rendition of holoed is looking.&lt;/div&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=445563</link><pubDate>Thu, 27 Nov 2008 00:57:28 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=445563</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/445563/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Ben, the State is "anything you like," and that's the point of some of the exercises: to package, say, layout information in the State. The mechanism doing the threading is the particular pattern of capturing values in closed-over variables (free variables inside functions from state to state-value&amp;#8230;</evnet:previewtext><dc:creator>brianbec</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/445563/Trackback.aspx</trackback:ping></item><item><title>Re: Re: Brian Beckman: The Zen of Stateless State - The State Monad - Part 1</title><description>Yes, this is a big brain workout, but just think nothing will ever scare you again! The underlying concepts are clean, it just takes a lot of syntax to write them out.&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;I think you're right about abstracting out by M: no higher-kinded types in C# makes it hard to mimic them. Perhaps it's easier to do this in F#? Not sure, but there is a parallel thread on doing the exercises in F#.&lt;/div&gt;</description><comments></comments><link>http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=445562</link><pubDate>Thu, 27 Nov 2008 00:52:18 GMT</pubDate><guid isPermaLink="false">http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/?CommentID=445562</guid><evnet:views>0</evnet:views><evnet:viewtrackingurl>http://channel9.msdn.com/445562/WebViewBug.aspx?EVT=0</evnet:viewtrackingurl><evnet:previewtext>Yes, this is a big brain workout, but just think nothing will ever scare you again! The underlying concepts are clean, it just takes a lot of syntax to write them out.I think you're right about abstracting out by M: no higher-kinded types in C# makes it hard to mimic them. Perhaps it's easier to do&amp;#8230;</evnet:previewtext><dc:creator>brianbec</dc:creator><slash:comments>0</slash:comments><wfw:commentRss></wfw:commentRss><trackback:ping>http://channel9.msdn.com/445562/Trackback.aspx</trackback:ping></item></channel></rss>