{"id":1050,"date":"2014-01-13T20:56:51","date_gmt":"2014-01-13T20:56:51","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1050"},"modified":"2014-01-13T20:56:51","modified_gmt":"2014-01-13T20:56:51","slug":"list-basics-in-f","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/list-basics-in-f\/","title":{"rendered":"List basics in F#"},"content":{"rendered":"<p>In a previous post I touched on F# lists, now let&#8217;s look a little further into the F# lists.<\/p>\n<p><em>Note: Also check out <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd233224.aspx\" title=\"Lists (F#)\" target=\"_blank\">Lists (F#)<\/a> for a far more comprehensive view of F# lists<\/em><\/p>\n<p>The MSDN documentation states that F# lists are implemented as singly linked lists.<\/p>\n<p><strong>Creating Lists<\/strong><\/p>\n<p>First off we can create a list using square brackets and semi colon delimiters, for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet list = &#x5B;&quot;One&quot;; &quot;Two&quot;; &quot;Three&quot;]\r\n<\/pre>\n<p>This creates a string list using type inference, i.e. <\/p>\n<p><em>val list : string list = [&#8220;One&#8221;; &#8220;Two&#8221;; &#8220;Three&#8221;]<\/em><\/p>\n<p>Had we wanted we could also create a list with a type hint mirroring this line (note the string list type hint)<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet list : string list = &#x5B;&quot;One&quot;; &quot;Two&quot;; &quot;Three&quot;]\r\n<\/pre>\n<p>We can also define this list without the semi-colon delimiter, instead using newlines as per<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet list = &#x5B;\r\n   &quot;One&quot;\r\n   &quot;Two&quot;\r\n   &quot;Three&quot;]\r\n<\/pre>\n<p>We can declare an empty list as follows<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet list = &#x5B;]\r\n<\/pre>\n<p>We can also declare lists using the range operator, for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet list = &#x5B;'a' .. 'z']\r\n<\/pre>\n<p>Whilst we can define a list of data as above, we can also generate the list in the following way<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet list = &#x5B;for i in 'A' .. 'Z' -&gt; i]\r\n<\/pre>\n<p><strong>Operators<\/strong><\/p>\n<p>The double colon :: operator takes a left hand argument as a single item and the right hand side as a list and creates a new list which is made up of the left hand argument and then right hand list appended to it. So, for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet original = &#x5B;1; 2; 3]\r\nlet list = 0 :: original\r\n<\/pre>\n<p>will result in list being [0; 1; 2; 3]<\/p>\n<p>We can also concatenate lists using the @ operator, for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet right = &#x5B;1; 2; 3]\r\nlet left = &#x5B;3; 2; 1]\r\n\r\nlet combined = left @ right\r\n<\/pre>\n<p>This results in a new list which looks like [3; 2; 1; 1; 2; 3]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post I touched on F# lists, now let&#8217;s look a little further into the F# lists. Note: Also check out Lists (F#) for a far more comprehensive view of F# lists The MSDN documentation states that F# lists are implemented as singly linked lists. Creating Lists First off we can create a [&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-1050","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\/1050","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=1050"}],"version-history":[{"count":13,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1050\/revisions"}],"predecessor-version":[{"id":1070,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1050\/revisions\/1070"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}