{"id":821,"date":"2009-06-02T10:34:10","date_gmt":"2009-06-02T08:34:10","guid":{"rendered":"http:\/\/www.navision-blog.de\/2009\/06\/02\/f-bootcamp-questions-and-answers-part-i-introduction\/"},"modified":"2009-06-02T11:10:05","modified_gmt":"2009-06-02T09:10:05","slug":"f-sharp-bootcamp-questions-and-answers-part-i-introduction","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2009\/06\/02\/f-sharp-bootcamp-questions-and-answers-part-i-introduction\/","title":{"rendered":"F# BootCamp &ndash; Questions and Answers &ndash; part I &ndash; Introduction"},"content":{"rendered":"<p>Last Friday we had a fantastic <a href=\"http:\/\/en.wikipedia.org\/wiki\/Learning_by_teaching\">LearningByTeaching<\/a> F# BootCamp in Leipzig. Each attendee got homework and had to solve one theoretical question and one programming task. For this two questions they had to present their results to the rest of us and after this I gave my solution in addition.<\/p>\n<p>It was very interesting to see the different strategies and solutions. In this post series I will discuss the questions and some of the possible solutions.<\/p>\n<h5>Question 1 &#8211; What is \u201cFunctional Programming\u201d in contrast to \u201cImperative Programming\u201d?<\/h5>\n<p>This seems to be an easy question but in fact, the attendees had some problems to give a short definition of both functional and imperative Programming. <\/p>\n<p>I didn\u2019t find a formal definition of the terms so my intention was to clarify things with an informal description like the one from Wikipedia:<\/p>\n<blockquote>\n<p>\u201cIn computer science, <b>functional programming<\/b> is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. Functional programming has its roots in the lambda calculus, a formal system developed in the 1930s to investigate function definition, function application, and recursion.\u201d<\/p>\n<p align=\"right\"><a href=\"http:\/\/en.wikipedia.org\/wiki\/Functional_programming\">Wikipedia<\/a><\/p>\n<\/blockquote>\n<p>I think the main aspect here is: avoiding state and mutable data. Maybe the words \u201cside-effect\u201d, recursion and \u201chigher-order functions\u201d could also be used, but they will be discussed in later questions.<\/p>\n<p>On my slides I covered the following aspects:<\/p>\n<ul>\n<li>Functional programming is a paradigm <\/li>\n<li>FP tries to avoid shared state <\/li>\n<li>Functions are first class citizens, enabling higher-order functions <\/li>\n<li>Pure functions<\/li>\n<ul>\n<li>no side-effects<\/li>\n<li>Results calculated only on the basis of input values <\/li>\n<li>No information storage <\/li>\n<li>Deterministic <\/li>\n<li>==&gt; Debugging and testing benefits <\/li>\n<li>==&gt; Thread-safe without locking of data <\/li>\n<\/ul>\n<\/ul>\n<p>For further reading I recommend &quot;<a href=\"http:\/\/www.cs.berkeley.edu\/%7Ejcondit\/pl-prelim\/hudak89functional.pdf\">Conception, evolution, and application of functional programming languages<\/a>&quot; (Paul Hudak) or \u201c<a href=\"http:\/\/www.defmacro.org\/ramblings\/fp.html\">Functional Programming For The Rest of Us<\/a>\u201d (Slava Akhmechet).<\/p>\n<h5>Question 2 \u2013 Explain the keyword \u201clet\u201d. In F# we are talking about \u201clet-bindings\u201d and not \u201cvariables\u201d. Why?<\/h5>\n<p>Basically you use the <strong>let<\/strong> keyword to bind a name to a value or function. It won\u2019t change any more, so a binding is immutable at default and not \u201cvariable\u201d. <\/p>\n<p>I was glad to see the presenter showing the problem with an imperative assignment like   <br \/> x = x + 1, which from a mathematical view is paradoxical. There is no x which equals x plus one. I think choice of the F# assignment operator is better than equality sign. The statement x &lt;- x + 1 shows the real intention. I want to put the old value of x plus one into the memory cell where x was before.    <br \/>So we discussed some basic terms like scope and mutability here and I showed how we can explicitly tell the compiler to use mutable data using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd233186(VS.100).aspx\">reference cells<\/a> or <a href=\"http:\/\/weblogs.asp.net\/podwysocki\/archive\/2008\/04\/15\/adventures-in-f-f-101-part-8-mutables-and-reference-cells.aspx\">mutable variables<\/a>. <\/p>\n<p>Maybe it wasn\u2019t that good idea to discuss \u201cImperative F#\u201d at such an early point (without knowing any functional concepts), but it showed the contrast to immutable let-Bindings.<\/p>\n<h5>Question 3 \u2013 What is a recursion? Try to explain why we often want recursions to be tail-recursive. Hint: Look at the following C# program. What is the problem and how could you solve it?<\/h5>\n<pre class=\"code\"><span style=\"color: blue\">public static <\/span><span style=\"color: #2b91af\">Int64 <\/span>Factorial(<span style=\"color: #2b91af\">Int64 <\/span>x)\n{\n    <span style=\"color: blue\">if <\/span>(x == 0) <span style=\"color: blue\">return <\/span>1;\n    <span style=\"color: blue\">return <\/span>x*Factorial(x - 1);\n}\n\u2026\nFactorial(10000);<\/pre>\n<p>It was interesting to see that nearly nobody expected a real problem in such a short code snippet. Some attendees thought this program might have an integer overflow \u2013 but only the presenters (they tested the program) gave the right answer (stack overflow). In fact they gave a very good and deep explanation about recursion and the problem on the stack.<\/p>\n<p>As the question hinted, a possible solution was adding a accumulator variable and using tail-recursion:<\/p>\n<pre class=\"code\"><span style=\"color: blue\">public static <\/span><span style=\"color: #2b91af\">BigInt <\/span>FactorialTailRecursive(<span style=\"color: #2b91af\">BigInt <\/span>x, <span style=\"color: #2b91af\">BigInt <\/span>acc)\n{\n    <span style=\"color: blue\">if <\/span>(x == <span style=\"color: #2b91af\">BigInt<\/span>.Zero) <span style=\"color: blue\">return <\/span>acc;\n    <span style=\"color: blue\">return <\/span>FactorialTailRecursive(x - <span style=\"color: #2b91af\">BigInt<\/span>.One, x*acc);\n}<\/pre>\n<p>Unfortunately this &quot;trick&quot; doesn&#8217;t work in C# (the compiler doesn\u2019t use tail calls), but it leads to the correct idea \u2013 converting it to a while-loop. Of course I would prefer the tail-recursive F# solution:<\/p>\n<pre class=\"code\"><span style=\"color: green\">\/\/\/ Tail recursive version\n<\/span><span style=\"color: blue\">let <\/span>factorial x = \n  <span style=\"color: blue\">let rec <\/span>tailRecursiveFactorial x acc =\n    <span style=\"color: blue\">match <\/span>x <span style=\"color: blue\">with\n      <\/span>| y <span style=\"color: blue\">when <\/span>y = 0I <span style=\"color: blue\">-&gt; <\/span>acc\n      | _ <span style=\"color: blue\">-&gt; <\/span>tailRecursiveFactorial (x-1I) (acc*x)           \n\n  tailRecursiveFactorial x 1I<\/pre>\n<p>We didn\u2019t cover <a href=\"http:\/\/en.wikipedia.org\/wiki\/Continuation-passing_style\">continuation passing<\/a> here. I think this could be something for an advanced session. <\/p>\n<p>Next time I will discuss the rest of the introduction and show some of the first programming tasks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last Friday we had a fantastic LearningByTeaching F# BootCamp in Leipzig. Each attendee got homework and had to solve one theoretical question and one programming task. For this two questions they had to present their results to the rest of us and after this I gave my solution in addition. It was very interesting to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,448,8,34,26,14,29],"tags":[664,557,555,556],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/821"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=821"}],"version-history":[{"count":4,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/821\/revisions"}],"predecessor-version":[{"id":825,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/821\/revisions\/825"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=821"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}