{"id":338,"date":"2013-12-22T11:05:38","date_gmt":"2013-12-22T11:05:38","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=338"},"modified":"2013-12-29T21:11:35","modified_gmt":"2013-12-29T21:11:35","slug":"f-basics","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/f-basics\/","title":{"rendered":"F# basics"},"content":{"rendered":"<p>Some basics of F#<\/p>\n<p><strong>#light<\/strong><\/p>\n<p>This directive (hash light) is used to simplify the F# syntax. Meaning the syntax is a little less verbose and removes aspects of the OCaml compatibility. <\/p>\n<p><strong>Immutable by default<\/strong><\/p>\n<p>By default &#8220;variables&#8221; are really just &#8220;values&#8221; as they are immutable by default so <\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet n = 100\r\n<\/pre>\n<p>initializes n to the value 100 and you cannot now alter n. <\/p>\n<p>However we can mark a value as mutable and thus is can be altered, for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet mutable n = 100\r\nn &lt;- n + 1\r\n<\/pre>\n<p>n is set to be a mutable type and <em>n <- n + 1<\/em> is the assignment line of code, assigning n the new value of n + 1.<\/p>\n<p><strong>Assignment operator<\/strong><\/p>\n<p>As mentioned above, F# values are immutable, by marking them as mutable we can assign values to them. The assignment operator is not = but is <-, for example\n\n[code language=\"fsharp\"]\nlet mutable n = 10  \/\/ initializes n the value 10\nn &lt;- 100            \/\/ assignment of the value 100\n[\/code]\n\n<strong>ignore<\/strong><\/p>\n<p>Some functions return values which we might not actually wish to use. For example the following uses List.map to simply output the items in the array urls<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet urls = &#x5B;&quot;http:\/\/www.bbc.co.uk&quot;;\r\n             &quot;http:\/\/www.google.co.uk&quot;;\r\n             &quot;http:\/\/www.amazon.co.uk&quot;]\r\n\r\nlet f x = printfn &quot;%s&quot; x\r\nList.map f urls\r\n<\/pre>\n<p>But the compiler will show a warning <em>This expression should have type &#8216;unit&#8217;, but has type &#8216;unit list&#8217;. Use &#8216;ignore&#8217; to discard the result of the expression, or &#8216;let&#8217; to bind the result to a name<\/em> for the line List.map f urls. To fix this either change this line to<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet o = List.map f urls\r\n<\/pre>\n<p>or pipe the result to ignore as per<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nList.map f urls |&gt; ignore\r\n<\/pre>\n<p>We can also use the following syntax<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nignore(List.map f urls)\r\n<\/pre>\n<p><strong>Semi-colon and commas in lists and arrays<\/strong><\/p>\n<p>To declare a list value in F# we use a semi-colon to denote each value of a list for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet evens = &#x5B;2;4;6;8;10;12]\r\n<\/pre>\n<p>This will create a val evens : int list = [2; 4; 6; 8; 10; 12]<\/p>\n<p>Alternatively using a comma as per<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet evens = &#x5B;2,4,6,8,10,12]\r\n<\/pre>\n<p>This will create a val evens : (int * int * int * int) list = [(2, 4, 6, 8)]. The difference here is that with the comma we&#8217;re creating a tuple containing integers, not a list of integers.<\/p>\n<p><strong>Collection Types<\/strong><\/p>\n<p><em>List<\/em><\/p>\n<p>A list is declared as <\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet list = &#x5B;2;4;6;8;10]\r\n<\/pre>\n<p>Lists are ordered, immutable data of the same type.<\/p>\n<p><em>Array<\/em><\/p>\n<p>An array is declared as<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet array = &#x5B;|2;4;6;8;10|]\r\n<\/pre>\n<p>Array&#8217;s are fixed-size, zero based values of the same type.<\/p>\n<p><em>Sequence<\/em><\/p>\n<p>A sequence is declared as<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet s = seq { 0 .. 10 .. 100 }\r\n<\/pre>\n<p>A sequence is a series of values of the same type. <\/p>\n<p><strong>Lambda expressions and anonymous functions<\/strong><\/p>\n<p>Anonymous functions are declared as follows<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nfun parameter-list -&gt; expression\r\n<\/pre>\n<p>For example we might use an anonymous function in a List.map as follows<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet urls = &#x5B;&quot;http:\/\/www.bbc.co.uk&quot;;\r\n             &quot;http:\/\/www.google.co.uk&quot;;\r\n             &quot;http:\/\/www.amazon.co.uk&quot;]\r\n\r\nList.map (fun x -&gt; printfn &quot;%s&quot; x) urls\r\n<\/pre>\n<p><strong>Comments<\/strong><\/p>\n<p>Single line comments are denoted in the same way as C# i.e.<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\n\/\/ This is a comment\r\n<\/pre>\n<p>Multi-line comments are denoted using (*..*), i.e.<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\n(*\r\nThis is a comment block\r\n*)\r\n<\/pre>\n<p><strong>Loops<\/strong><\/p>\n<p>The F# equivalent of a foreach<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet a = &#x5B;0 .. 6]\r\nfor i in a do\r\n   printfn &quot;%d&quot; i\r\n<\/pre>\n<p>The <em>for<\/em> loop<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nfor i = 1 to 10 do\r\n   printfn &quot;%d&quot; i\r\n\r\nfor j = 10 downto 1 do\r\n   printfn &quot;%d&quot; j\r\n<\/pre>\n<p>The <em>while<\/em> loop<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet aa = &#x5B;|0 .. 6|]\r\nlet mutable i = 0\r\n  while i &lt; (Array.length aa) do\r\n     printfn &quot;%d&quot; (Array.get aa i)\r\n     i &lt;- i + 1\r\n<\/pre>\n<p><strong>The unit type<\/strong><\/p>\n<p>Functions which take no arguments and\/or return no values are said to take and return the type <em>unit<\/em>. This can be seen as being analogous to void in C#.<\/p>\n<p><strong>The double backtick &#8220; in identifiers<\/strong><\/p>\n<p>As per <a href=\"http:\/\/research.microsoft.com\/en-us\/um\/cambridge\/projects\/fsharp\/manual\/spec.html#_Toc270597387\" title=\"Identifiers and Keywords\" target=\"_blank\">Identifiers and Keywords<\/a>. We can use the double backtick &#8220; to enclose identifier that might be a language keyword, for example <\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet ``private`` = 1\r\n<\/pre>\n<p>is valid even though private is a keyword. We can also have spaces in identifiers such as<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet ``add two numbers`` () =\r\n   \/\/ function definition\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Some basics of F# #light This directive (hash light) is used to simplify the F# syntax. Meaning the syntax is a little less verbose and removes aspects of the OCaml compatibility. Immutable by default By default &#8220;variables&#8221; are really just &#8220;values&#8221; as they are immutable by default so let n = 100 initializes n to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-338","post","type-post","status-publish","format-standard","hentry","category-f"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=338"}],"version-history":[{"count":23,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/338\/revisions"}],"predecessor-version":[{"id":973,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/338\/revisions\/973"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}