<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Playing With F#</title>
	<link>http://www.netcrucible.com/blog/2007/12/30/playing-with-f/</link>
	<description>The software industry from a rational perspective</description>
	<pubDate>Thu, 20 Nov 2008 11:12:49 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>

	<item>
		<title>By: allenjs</title>
		<link>http://www.netcrucible.com/blog/2007/12/30/playing-with-f/#comment-51656</link>
		<author>allenjs</author>
		<pubDate>Mon, 31 Dec 2007 04:39:10 +0000</pubDate>
		<guid>http://www.netcrucible.com/blog/2007/12/30/playing-with-f/#comment-51656</guid>
		<description>Janne: The range function isn't tail recursive, either, but I thought it didn't matter anymore.  I actually tested before posting, and F# returns 10,000 deep almost instantly -- so I assumed the compiler was doing some magic to implement tail recursion under the covers (it's smart enough to infer all my types, so I thought "why not"), and 10,000 deep is an ungodly level of recursion.

Anyway, I just tested with bigger numbers and was able to blow the F# stack at 100,000 deep on my machine.  So you're right.

I also just tested the same routine in C#, and found that it actually does 10,000 deep on my machine (sum only, not range) as well.  I am amazed at this; I would expect it to blow up after a few hundred.  C# is a lot slower than F# at this level of recursion, though; and I can blow up C# at just 15,000 -- where F# functions fine and near-instant at 15,000.  It seems that F# perf doesn't degrade as quickly as C# before blowing up.

For completeness sake, the range function is better implemented as:
let rec range2 a b = [for i in a to b -&gt; i]</description>
		<content:encoded><![CDATA[<p>Janne: The range function isn&#8217;t tail recursive, either, but I thought it didn&#8217;t matter anymore.  I actually tested before posting, and F# returns 10,000 deep almost instantly &#8212; so I assumed the compiler was doing some magic to implement tail recursion under the covers (it&#8217;s smart enough to infer all my types, so I thought &#8220;why not&#8221;), and 10,000 deep is an ungodly level of recursion.</p>
<p>Anyway, I just tested with bigger numbers and was able to blow the F# stack at 100,000 deep on my machine.  So you&#8217;re right.</p>
<p>I also just tested the same routine in C#, and found that it actually does 10,000 deep on my machine (sum only, not range) as well.  I am amazed at this; I would expect it to blow up after a few hundred.  C# is a lot slower than F# at this level of recursion, though; and I can blow up C# at just 15,000 &#8212; where F# functions fine and near-instant at 15,000.  It seems that F# perf doesn&#8217;t degrade as quickly as C# before blowing up.</p>
<p>For completeness sake, the range function is better implemented as:<br />
let rec range2 a b = [for i in a to b -> i]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Janne</title>
		<link>http://www.netcrucible.com/blog/2007/12/30/playing-with-f/#comment-51620</link>
		<author>Janne</author>
		<pubDate>Sun, 30 Dec 2007 20:25:13 +0000</pubDate>
		<guid>http://www.netcrucible.com/blog/2007/12/30/playing-with-f/#comment-51620</guid>
		<description>whoops, the last function should read

let sum = 
let rec loop acc ...</description>
		<content:encoded><![CDATA[<p>whoops, the last function should read</p>
<p>let sum =<br />
let rec loop acc &#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Janne</title>
		<link>http://www.netcrucible.com/blog/2007/12/30/playing-with-f/#comment-51619</link>
		<author>Janne</author>
		<pubDate>Sun, 30 Dec 2007 20:18:24 +0000</pubDate>
		<guid>http://www.netcrucible.com/blog/2007/12/30/playing-with-f/#comment-51619</guid>
		<description>Are you sure that your sum function will not blow up the stack?  It's not tail recursive:

let rec sum list =
    match list with
    &#124; h::tail -&#62; (sum tail) + h (*  0

something like 

List.fold_left (fun acc e -&#62; acc+e) 0 list

or

let rec sum =
  let loop acc = function
    x::xs -&#62; loop (x+acc) xs
  &#124; [] -&#62; acc in
  loop 0 

would be.</description>
		<content:encoded><![CDATA[<p>Are you sure that your sum function will not blow up the stack?  It&#8217;s not tail recursive:</p>
<p>let rec sum list =<br />
    match list with<br />
    | h::tail -&gt; (sum tail) + h (*  0</p>
<p>something like </p>
<p>List.fold_left (fun acc e -&gt; acc+e) 0 list</p>
<p>or</p>
<p>let rec sum =<br />
  let loop acc = function<br />
    x::xs -&gt; loop (x+acc) xs<br />
  | [] -&gt; acc in<br />
  loop 0 </p>
<p>would be.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
