<div dir="ltr">Hi Joe,<div><br></div><div>There's a semicolon missing after the declaration of fib(), which for some reason, causes the stack overflow. After adding it the error went away.</div><div><br></div><div>HTH,</div><div><br></div><div>J</div><div><br>On Monday, June 16, 2014 11:22:23 AM UTC-3, Joe Armstrong wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir="ltr"><div>I'm trying to compare node.js with erlang. I'm a total node.js novice BTW - but I've written some JS.</div><div><br></div><div>Program 1 is just fibonacci - I want a web server to compute fib(N)</div>
<div><br></div><div>So requesting <a href="http://127.0.0.1:8124/fib?n=2" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2F127.0.0.1%3A8124%2Ffib%3Fn%3D2\46sa\75D\46sntz\0751\46usg\75AFQjCNFTB5Tb8Itkd4qmRKTXBWc01JMgyw';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2F127.0.0.1%3A8124%2Ffib%3Fn%3D2\46sa\75D\46sntz\0751\46usg\75AFQjCNFTB5Tb8Itkd4qmRKTXBWc01JMgyw';return true;">http://127.0.0.1:8124/fib?n=2</a> should compute and return fib(2)</div><div><br></div><div>My node.js attempt crashes</div><div>Here's the code in fib.js</div>
<div><br></div><div>--- fib.js</div><div><br></div><div><div>var http = require('http');</div><div>var url = require('url');</div><div><br></div><div>function fib(n) {</div><div> if (n < 2) {</div><div>
return 1;</div><div> } else {</div><div> return fib(n - 2) + fib(n - 1);</div><div> }</div><div>}</div><div><br></div><div>http.createServer(function (req, res) {</div><div> var q = url.parse(req.url, true).query;</div>
<div> var n = q.n;</div><div> var result = fib(n);</div><div> console.log('fib('+ n + ')= '+result);</div><div> res.writeHead(200, {'Content-Type': 'text/plain'});</div><div> res.end(result.toString());</div>
<div>}).listen(8124, "127.0.0.1");</div><div><br></div><div>console.log('Server running at <a href="http://127.0.0.1:8124/'" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2F127.0.0.1%3A8124%2F\47\46sa\75D\46sntz\0751\46usg\75AFQjCNHOwcUTIR1AFWL8iEKANe1xVh0jkQ';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2F127.0.0.1%3A8124%2F\47\46sa\75D\46sntz\0751\46usg\75AFQjCNHOwcUTIR1AFWL8iEKANe1xVh0jkQ';return true;">http://127.0.0.1:8124/'</a>);</div></div><div><br></div><div>-- end</div><div>
<br></div><div>-- now we run it</div><div><div><br></div><div>$ node fib.js</div><div>Server running at <a href="http://127.0.0.1:8124/" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2F127.0.0.1%3A8124%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHR8X9YZeegbKccKLXLa1fxu0cE9w';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2F127.0.0.1%3A8124%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNHR8X9YZeegbKccKLXLa1fxu0cE9w';return true;">http://127.0.0.1:8124/</a></div><div>fib(2)= 2</div><div><br></div><div>/home/ejoearm/Dropbox/<wbr>experiments/hello_world/fib.<wbr>js:5</div>
<div>function fib(n) {</div><div> ^</div><div>RangeError: Maximum call stack size exceeded</div></div><div><br></div><div>fib(2) has run out of stack space????</div><div><div><br></div><div>$ node --version</div>
<div>v0.8.21</div></div><div><br></div><div>Any ideas what I'm doing wrong</div><div><br></div><div>Cheers</div><div><br></div><div>/Joe</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div>
<div><br></div></div>
</blockquote></div></div>