<?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>ImmutableStack in F#</title><link>http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx</link><description>When learning a new language I find it very instructive to re-code certain structures from my well used libraries into the new language.&amp;#160; It gives a great basis for comparison in terms of ease of implementation, expressiveness of the language and</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>send flowers &amp;raquo; ImmutableStack in F#</title><link>http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx#8869497</link><pubDate>Fri, 15 Aug 2008 15:08:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8869497</guid><dc:creator>send flowers &amp;raquo; ImmutableStack in F#</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://hubsfunnywallpaper.cn/?p=829"&gt;http://hubsfunnywallpaper.cn/?p=829&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: ImmutableStack in F#</title><link>http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx#8869971</link><pubDate>Fri, 15 Aug 2008 18:43:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8869971</guid><dc:creator>ChrSmith</dc:creator><description>&lt;p&gt;Nice post. In response to your questions:&lt;/p&gt;
&lt;p&gt;1. You can hide the union case is through an explicit accessibility annotation (type private Node) or through the use of F# Signature files (.fsi). (The prefered way is through .fsi files.)&lt;/p&gt;
&lt;p&gt;2. I like the use of optional parameters in the constructor, although it allows for invalid combinations it doesn't require you to write several explicit constructors.&lt;/p&gt;
&lt;p&gt;3. You can make 'Empty' a property by just dropping the parens, e.g. 'static member Empty = new ImmutableStack()'.&lt;/p&gt;
&lt;p&gt;4. Perhaps you could try a Sequence Expression, I'm not sure if it is any better or worse than the code you had written, but it certainly looks cleaner IMHO.&lt;/p&gt;
&lt;p&gt;member x.All() =&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;match data with &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| Empty -&amp;gt; Seq.empty&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| Value (v,n) -&amp;gt; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;seq {&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;yield v&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;yield! n.All()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
</description></item><item><title>re: ImmutableStack in F#</title><link>http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx#8870110</link><pubDate>Fri, 15 Aug 2008 19:41:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8870110</guid><dc:creator>Jason M</dc:creator><description>&lt;p&gt;Two questions, since I'm interested in F# as well:&lt;/p&gt;
&lt;p&gt;1) This is an implementation of a stack over ints. Do we have generics, or some other (better?) concept?&lt;/p&gt;
&lt;p&gt;2) what does &amp;quot;failwith&amp;quot; do? I suspect it throws a CLR exception, but what details are there?&lt;/p&gt;
</description></item><item><title>re: ImmutableStack in F#</title><link>http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx#8870183</link><pubDate>Fri, 15 Aug 2008 20:15:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8870183</guid><dc:creator>Jared Parsons</dc:creator><description>&lt;p&gt;@ChrSmith&lt;/p&gt;
&lt;p&gt;Thanks for the feedback. &amp;nbsp;For #3 I think that is the better solution. &amp;nbsp;From my understanding, my All() method is eager and will process all nodes immediately and build a full sequence. &amp;nbsp;yield should produce a delay evaluated sequence which is safe since this is an immutable collection.&lt;/p&gt;
</description></item><item><title>re: ImmutableStack in F#</title><link>http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx#8870185</link><pubDate>Fri, 15 Aug 2008 20:16:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8870185</guid><dc:creator>Jared Parsons</dc:creator><description>&lt;p&gt;@Jason&lt;/p&gt;
&lt;p&gt;1) I forgot to put that in the list of items I want to fix. &amp;nbsp;I decided to learn without generics and then complicate my life later on :)&lt;/p&gt;
&lt;p&gt;2) failwith throws a F# exception. &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: ImmutableStack in F#</title><link>http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx#8871042</link><pubDate>Sat, 16 Aug 2008 03:08:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8871042</guid><dc:creator>MichaelGG</dc:creator><description>&lt;p&gt;One approach is to embrace the discriminated union as the actual used type. This has the added benefit of automatically allowing pattern matching. This benefit is shown in my printStack function.&lt;/p&gt;
&lt;p&gt;#light&lt;/p&gt;
&lt;p&gt;open System&lt;/p&gt;
&lt;p&gt;type 'a ImmutableStack = &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;| Empty&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;| Stack of 'a * 'a ImmutableStack&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;member x.Peek() = match x with | Stack (v, _) -&amp;gt; Some v | _ -&amp;gt; None&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;member x.Pop() = match x with | Stack (_, s) -&amp;gt; s | _ -&amp;gt; failwith &amp;quot;empty stack&amp;quot;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;member x.Push a = Stack(a, x)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;member x.IsEmpty = x = Empty &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;member x.All = &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;let until f = Seq.generate (fun () -&amp;gt; ref x) f (fun _ -&amp;gt; ())&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;until (fun cur -&amp;gt; match (!cur) with Stack(v, s) -&amp;gt; cur := s; Some v | _ -&amp;gt; None)&lt;/p&gt;
&lt;p&gt;let rec printStack = function&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;| Empty -&amp;gt; printfn &amp;quot;Empty&amp;quot;&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;| Stack(v, Empty) -&amp;gt; printfn &amp;quot;%A&amp;quot; v&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;| Stack(v, s) -&amp;gt; printf &amp;quot;%A; &amp;quot; v; &lt;/p&gt;
</description></item><item><title>re: ImmutableStack in F#</title><link>http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx#8871079</link><pubDate>Sat, 16 Aug 2008 03:35:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8871079</guid><dc:creator>MichaelGG</dc:creator><description>&lt;p&gt;Lists also can relfect a stack, so you can just wrap the F# lists:&lt;/p&gt;
&lt;p&gt;type 'a stack = &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;| Stack of 'a list&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;member x.IsEmpty = x = Stack []&lt;/p&gt;
&lt;p&gt;module Stack =&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let empty = Stack []&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let pop = function Stack l -&amp;gt; Stack (List.tl l)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let push x = function Stack l -&amp;gt; Stack (x :: l)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let peek = function Stack l -&amp;gt; Stack (List.hd l)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let all s = function Stack l -&amp;gt; seq l&lt;/p&gt;
&lt;p&gt;let s1 = Stack.empty&lt;/p&gt;
&lt;p&gt;let s2 = s1 |&amp;gt; Stack.push 12 |&amp;gt; Stack.push 11 |&amp;gt; Stack.push 19&lt;/p&gt;
</description></item><item><title>re: ImmutableStack in F#</title><link>http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx#8871376</link><pubDate>Sat, 16 Aug 2008 08:43:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8871376</guid><dc:creator>MichaelGG</dc:creator><description>&lt;p&gt;Oh, I should add, the All implementation I tried only creates a single seq instead of having to create new enumerators for each item. I tried a few ways here: &lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.atrevido.net/blog/2008/08/16/Reference+Cells+In+F+Sequences.aspx"&gt;http://www.atrevido.net/blog/2008/08/16/Reference+Cells+In+F+Sequences.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;But I'm not really sold. Chris's use of sequence expressions are probably the best way. &lt;/p&gt;
</description></item><item><title>re: ImmutableStack in F#</title><link>http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx#8871608</link><pubDate>Sat, 16 Aug 2008 17:48:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8871608</guid><dc:creator>MichaelGG</dc:creator><description>&lt;p&gt;Sorry for posting so much; I swear I'm not spamming. To address your original question (4) about All(), yes, it is eagerly evaluated. But the fix is really simple; just create a delayed sequence on the recursive call. That way it won't be called until enumerated:&lt;/p&gt;
&lt;p&gt;| Value (v,n) -&amp;gt; Seq.append (Seq.singleton v) (Seq.delay(fun () -&amp;gt; n.All()))&lt;/p&gt;
</description></item><item><title>re: ImmutableStack in F#</title><link>http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx#8876759</link><pubDate>Mon, 18 Aug 2008 19:24:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8876759</guid><dc:creator>Jared Parsons</dc:creator><description>&lt;p&gt;@MichaelGG&lt;/p&gt;
&lt;p&gt;Thanks for the multi-tude of comments. &amp;nbsp;It's great to see so many different ways of approaching this problem. &amp;nbsp;&lt;/p&gt;
</description></item></channel></rss>