<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: AllenZhu</title>
    <description>The latest articles on DEV Community by AllenZhu (@pythonzhu).</description>
    <link>https://dev.to/pythonzhu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1499805%2Fec15ff85-a313-42de-bc71-3be0a50cd75a.jpg</url>
      <title>DEV Community: AllenZhu</title>
      <link>https://dev.to/pythonzhu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pythonzhu"/>
    <language>en</language>
    <item>
      <title>F# For Dummys - Day 16 Collections Sequence</title>
      <dc:creator>AllenZhu</dc:creator>
      <pubDate>Tue, 28 May 2024 11:01:54 +0000</pubDate>
      <link>https://dev.to/pythonzhu/f-for-dummys-day-16-collections-sequence-can</link>
      <guid>https://dev.to/pythonzhu/f-for-dummys-day-16-collections-sequence-can</guid>
      <description>&lt;p&gt;Today we learn Sequence, represents an ordered, read-only series of elements&lt;/p&gt;

&lt;h4&gt;
  
  
  Why Sequence
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;  Sequences are particularly useful when you have a large, ordered collection of data but do not necessarily expect to use all of the elements.&lt;/li&gt;
&lt;li&gt;  Individual sequence elements are computed on-demand, so a sequence can provide better performance than a list in situations in which not all the elements are used&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What is computed on-demand&lt;/strong&gt;&lt;br&gt;
like buying a burger in KFC, the staff prepared 1000 burgers in the early morning, so you got your burger immediately, all the burgers are made before customer order, this is called &lt;em&gt;eager evaluation&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;then you ordered a fried chicken, the staff replies: sorry, you have to wait for a minute, as we need to fry the chicken first, they only do the work when someone ordered, this is called &lt;em&gt;lazy evaluation&lt;/em&gt;, also known as &lt;em&gt;On-demand computation&lt;/em&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Create Sequence
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Explicitly specifying elements
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let seq1 = seq [1; 2; 3; 4]
printfn "seq: %A" seq1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Using range expression
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let seq1 = seq { 1 .. 10 } // from 1 to 10
printfn "seq: %A" seq1 // seq: seq [1; 2; 3; 4; ...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;use step in range expression&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let seq1 = seq { 1 .. 2 .. 10 } // start from 1, add 2 each time
printfn "seq: %A" seq1 // seq: seq [1; 3; 5; 7; ...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Using for loop
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let seq1 = seq { for i in 1 .. 10 -&amp;gt; i }
printfn "seq: %A" seq1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;ofList
Views the given list as a sequence
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let inputs = [ 1; 2; 5 ]
let seq1 = inputs |&amp;gt; Seq.ofList
printfn "seq: %A" seq1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;ofArray
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let inputs = [| 1; 2; 5 |]
let seq1 = inputs |&amp;gt; Seq.ofArray
printfn "seq: %A" seq1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Seq.initInfinite
generate an infinite sequence
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let infiniteSeq = Seq.initInfinite (fun i -&amp;gt; i * 2)
printfn "infiniteSeq %A" infiniteSeq
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;define a start for infinite sequence&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let start = 5
let infiniteSeq = Seq.initInfinite (fun i -&amp;gt; i + start)
printfn "infiniteSeq %A" infiniteSeq // infiniteSeq seq [5; 6; 7; 8; ...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or define a start like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let infiniteSeq = Seq.initInfinite ((+) 5)
printfn "infiniteSeq %A" infiniteSeq // infiniteSeq seq [5; 6; 7; 8; ...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Loop Sequence
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;for loop
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = seq { 1 .. 10 }

for number in numbers do
    printfn "Number: %d" number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Seq.iter
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = seq { 1 .. 10 }

Seq.iter (fun x -&amp;gt; printfn "Number: %d" x) numbers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Access element
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Seq.item&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;syntax: Seq.item index source, thrown ArgumentException when the index is negative or the input sequence does not contain enough elements&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = seq { 1 .. 10 }
let thirdElement = Seq.item 2 numbers  // Indexing is zero-based

printfn "The third element is %d" thirdElement
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Seq.head &amp;amp;&amp;amp; Seq.tail&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Seq.head: Returns the first element of the sequence&lt;br&gt;
Seq.tail: Returns a sequence that skips 1 element of the underlying sequence and then yields the remaining elements of the sequence&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = seq { 1 .. 10 }
let firstElement = Seq.head numbers
let restElement = Seq.tail numbers

printfn "The first element is %d" firstElement // The first element is 1
printfn "The rest element is %A" restElement // The rest element is seq [2; 3; 4; 5; ...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Operate element
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Seq.updateAt&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return a new sequence with the item at a given index set to the new value&lt;br&gt;
syntax: Seq.updateAt index value source&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let newSeq = Seq.updateAt 1 9 seq { 0; 1; 2 }
// let newSeq = Seq.updateAt 1 9 (seq { 0; 1; 2 })
printfn "newSeq: %A" newSeq // newSeq: seq [0; 9; 2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Seq.filter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Returns a new collection containing only the elements of the collection for which the given predicate returns "true". This is a synonym for Seq.where&lt;br&gt;
syntax: Seq.filter predicateFunc sourceSeq&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = seq { 1 .. 10 }
let evenNumbers = Seq.filter (fun x -&amp;gt; x % 2 = 0) numbers

Seq.iter (fun x -&amp;gt; printfn "Even Number: %d" x) evenNumbers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Seq.map&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Builds a new collection whose elements are the results of applying the given function to each of the elements of the collection&lt;br&gt;
syntax: Seq.filter mappingFunc sourceSeq&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = seq { 1 .. 10 }
let doubleNumbers = Seq.map (fun x -&amp;gt; x * 2) numbers

Seq.iter (fun x -&amp;gt; printfn "doubleNumbers: %d" x) doubleNumbers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Seq.fold&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Applies a function to each element of the collection&lt;br&gt;
syntax: Seq.fold folderFunc state sourceSeq&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Charge =
     | In of int
     | Out of int
let inputs = [In 1; Out 2; In 3]
let balance = Seq.fold (fun acc charge -&amp;gt;
    match charge with
    | In i -&amp;gt; acc + i
    | Out o -&amp;gt; acc - o) 0 inputs

printfn "balance %A" balance // balance 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or use pipeline operator to pass state sourceSeq, here is ||&amp;gt; for passing more than one param&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Charge =
     | In of int
     | Out of int
let inputs = [In 1; Out 2; In 3]
let balance = (0, inputs) ||&amp;gt; Seq.fold (fun acc charge -&amp;gt;
    match charge with
    | In i -&amp;gt; acc + i
    | Out o -&amp;gt; acc - o)

printfn "balance %A" balance // balance 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>fsharp</category>
    </item>
    <item>
      <title>F# For Dummys - Day 15 Collections Set</title>
      <dc:creator>AllenZhu</dc:creator>
      <pubDate>Mon, 27 May 2024 08:39:41 +0000</pubDate>
      <link>https://dev.to/pythonzhu/f-for-dummys-day-15-collections-set-3884</link>
      <guid>https://dev.to/pythonzhu/f-for-dummys-day-15-collections-set-3884</guid>
      <description>&lt;p&gt;Today we learn Set, an immutable collection that automatically ensures all its elements are unique and sorted&lt;/p&gt;

&lt;h4&gt;
  
  
  Create Set
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Using set notation
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numberSet = set [1; 3; 5; 4; 2; 3]
printfn "numberSet: %A" numberSet // numberSet: set [1; 2; 3; 4; 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;from List
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let fruits = Set.ofList ["apple"; "banana"; "orange"; "apple"]
printfn "Fruits: %A" fruits
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Set.empty create an empty Set
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let emptySet = Set.empty
printfn "emptySet: %A" emptySet // emptySet: set []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Get element not support
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let fruits = Set.ofList ["apple"; "banana"; "orange"]
printfn "Fruits first element: %A" fruits.[0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;raise Error: The type 'Set&amp;lt;_&amp;gt;' does not define the field, constructor or member 'Item'&lt;/p&gt;

&lt;h4&gt;
  
  
  Add/Remove element
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Set.add
Returns a new set with an element added to the set. No exception is raised if the set already contains the given element
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let set = Set.empty.Add(1).Add(1).Add(2)
printfn $"The new set is: {set}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Set.remove
Returns a new set with the given element removed. No exception is raised if the set doesn't contain the given element
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let set = Set.empty.Add(1).Add(2).Add(3)
printfn $"The set without 1 is {Set.remove 1 set}" // The set without 1 is set [2; 3]
printfn $"The set without 1 is {Set.remove 4 set}" // The set without 1 is set [1; 2; 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can see set itself didn't change after remove 1&lt;/p&gt;

&lt;h4&gt;
  
  
  Loop Set
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;for ... in
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let fruits = Set.ofList ["apple"; "banana"; "orange"; "grape"]

for fruit in fruits do
    printfn "Fruit: %s" fruit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Set.iter
Applies the given function to each element of the set, in order according to the comparison function
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let set = Set.empty.Add(1).Add(2).Add(3)
Set.iter (fun x -&amp;gt; printfn $"The set contains {x}") set
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Element operation
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Contains
syntax: Set.Contains element set
Evaluates to "true" if the given element is in the given set
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let set = Set.empty.Add(2).Add(3)
printfn $"Does the set contain 1? {Set.contains 1 set}" // Does the set contain 1? false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;use &lt;em&gt;Instance Member Function&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let set = Set.empty.Add(2).Add(3)
printfn $"Does the set contain 1? {set.Contains(1)}"  // Does the set contain 1? false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;exists
syntax: Set.exists predicate set
Tests if any element of the collection satisfies the given predicate
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let set = Set.empty.Add(1).Add(2).Add(3)
printfn $"Does the set contain even number? {Set.exists (fun x -&amp;gt; x % 2 = 0) set}" // Does the set contain even number? true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;filter
syntax: Set.filter predicate set
Returns a new collection containing only the elements of the collection for which the given predicate returns True
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let set = Set.empty.Add(1).Add(2).Add(3).Add(4)
printfn $"The set with even numbers is {Set.filter (fun x -&amp;gt; x % 2 = 0) set}" // The set with even numbers is set [2; 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Set operation
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgdrin1ogfc1f5gslhm7s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgdrin1ogfc1f5gslhm7s.png" alt="Image description" width="632" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lucy and Lily having breakfast: &lt;br&gt;
Lucy have milk, boiled egg and sausages, Lily have milk, bacon and fried egg, &lt;br&gt;
we can define their food list by 2 Sets: set ["milk", "boiled egg", "sausages"], set ["milk", "bacon", "fried egg"]&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;union
union is all the elements in 2 Sets
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let LucyBreakfastSet = set ["milk"; "boiled egg"; "sausages"]
let LilyBreakfastSet = set ["milk"; "bacon"; "fried egg"]
printfn $"The union of {LucyBreakfastSet} and {LilyBreakfastSet} is {(Set.union LucyBreakfastSet LilyBreakfastSet)}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;the union is expected to be "milk", "boiled egg", "sausages", "bacon", "fried egg"&lt;br&gt;
results: The union of set [boiled egg; milk; sausages] and set [bacon; fried egg; milk] is set [bacon; boiled egg; fried egg; milk; sausages]&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;intersect
Computes the intersection of the two sets, the common elements they share
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let LucyBreakfastSet = set ["milk"; "boiled egg"; "sausages"]
let LilyBreakfastSet = set ["milk"; "bacon"; "fried egg"]
printfn $"The intersect of {LucyBreakfastSet} and {LilyBreakfastSet} is {(Set.intersect LucyBreakfastSet LilyBreakfastSet)}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;the intersect is expected to be "milk"&lt;br&gt;
results: The intersect of set [boiled egg; milk; sausages] and set [bacon; fried egg; milk] is set [milk]&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;difference
difference returns a new set with the elements of the second set removed from the first set, which is the left colored part in the picture (A left B right)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ypjsbvgujcc811bx31m.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ypjsbvgujcc811bx31m.jpg" alt="Image description" width="261" height="177"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;if we put Lucy's breakfast in the first place&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let LucyBreakfastSet = set ["milk"; "boiled egg"; "sausages"]
let LilyBreakfastSet = set ["milk"; "bacon"; "fried egg"]
printfn $"The difference of {LucyBreakfastSet} and {LilyBreakfastSet} is {(Set.difference LucyBreakfastSet LilyBreakfastSet)}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we got "boiled egg"; "sausages"&lt;br&gt;
result: The difference of set [boiled egg; milk; sausages] and set [bacon; fried egg; milk] is set [boiled egg; sausages]&lt;br&gt;
&lt;em&gt;Set.difference LucyBreakfastSet LilyBreakfastSet&lt;/em&gt; is equivalent to &lt;em&gt;LucyBreakfastSet - LilyBreakfastSet&lt;/em&gt;&lt;br&gt;
we can understand like this: remove the food Lily also had in Lucy's food&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let LucyBreakfastSet = set ["milk"; "boiled egg"; "sausages"]
let LilyBreakfastSet = set ["milk"; "bacon"; "fried egg"]
printfn $"The difference of {LucyBreakfastSet} and {LilyBreakfastSet} is {(LucyBreakfastSet - LilyBreakfastSet)}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>fsharp</category>
    </item>
    <item>
      <title>F# For Dummys - Day 14 Collections Map</title>
      <dc:creator>AllenZhu</dc:creator>
      <pubDate>Sat, 25 May 2024 08:25:32 +0000</pubDate>
      <link>https://dev.to/pythonzhu/f-for-dummys-day-14-collections-map-big</link>
      <guid>https://dev.to/pythonzhu/f-for-dummys-day-14-collections-map-big</guid>
      <description>&lt;p&gt;Today we learn Map, an immutable collection that stores key-value pairs&lt;/p&gt;

&lt;p&gt;Map is immutable, it cannot be changed after created. Adding or removing elements return a new Map instead of modifying the existing one&lt;/p&gt;

&lt;h4&gt;
  
  
  Create Map
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Explicitly specifying elements
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let notEmptyMap = Map [ (1, "a"); (2, "b") ]

printfn "%A" notEmptyMap // map [(1, a); (2, b)]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Map.ofList
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let map = 
    Map.ofList [
        (3, "three")
        (1, "one")
        (2, "two")
    ]

printfn "map: %A" map // map [(1, one); (2, two); (3, three)]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can see the key is sorted from 3,1,2 to 1,2,3&lt;br&gt;
what if the key is dulpicate&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let map = 
    Map.ofList [
        (3, "three")
        (1, "one")
        (2, "two") // first key 2
        (2, "overwrite") // second key 2
    ]

printfn "%A" map // map [(1, one); (2, overwrite); (3, three)]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the value of the same key 2 is the last one, it overwrite the first one&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Map.empty&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;create an empty map with int keys and string values&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let emptyMap = Map.empty&amp;lt;int, string&amp;gt;
printfn "%A" emptyMap // map []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Access element
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;ContainsKey&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tests if an element is in the domain of the map&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sample = Map [ (1, "a"); (2, "b") ]

printfn "sample contain key 1: %b" (sample.ContainsKey 1) // sample contain key 1: true
printfn "sample contain key 3: %b" (sample.ContainsKey 3) // sample contain key 3: false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;map.[key]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;access by key in the domain will return the value, raise Exception if key not exists&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sample = Map [ (1, "a"); (2, "b") ]

printfn "access by key 1: %s" sample.[1] // access by key 1: a
printfn "access by key 3: %s" sample.[3] // Unhandled exception. System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if you use browser environment, you may not see this Exception&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TryFind&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lookup an element in the map, returning a &lt;code&gt;Some&lt;/code&gt; value if the element is in the domain of the map and &lt;code&gt;None&lt;/code&gt; if not&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sample = Map [ (1, "a"); (2, "b") ]

printfn "TryFind key 1: %A" (sample.TryFind 1) // evaluates to Some "a"
printfn "TryFind key 3: %A" (sample.TryFind 3) // evaluates to None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;TryGetValue&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lookup an element in the map, assigning to &lt;code&gt;value&lt;/code&gt; if the element is in the domain of the map and returning &lt;code&gt;false&lt;/code&gt; if not&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sample = Map [ (1, "a"); (2, "b") ]

printfn "access by key 1: %A" (sample.TryGetValue 1) // evaluates to (true, "a")
printfn "access by key 3: %A" (sample.TryGetValue 3) // evaluates to (false, null)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;TryGetValue&lt;/code&gt; is added in F# 6.0&lt;/p&gt;

&lt;p&gt;Conclusion: &lt;code&gt;TryFind&lt;/code&gt; &lt;code&gt;TryGetValueThe&lt;/code&gt; provide a safer way to access elements, and &lt;code&gt;option&lt;/code&gt; type returned by &lt;code&gt;TryFind&lt;/code&gt; &lt;code&gt;TryGetValueThe&lt;/code&gt; fits naturally with F#'s pattern matching&lt;/p&gt;

&lt;h4&gt;
  
  
  Pattern Matching with Map
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;TryFind match
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mp = Map.ofList [("doot", 1); ("beef", 2); ("hoopty", 3)]

match Map.tryFind "doot" mp with
| Some value -&amp;gt; printfn "Value: %A" value
| None -&amp;gt; printfn "No value found!"

match Map.tryFind "goot" mp with
| Some value -&amp;gt; printfn "Value: %A" value
| None -&amp;gt; printfn "No value found!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;TryGetValue match
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mp = Map.ofList [("doot", 1); ("beef", 2); ("hoopty", 3)]

match mp.TryGetValue "doot" with
| (true, value) -&amp;gt; printfn "Value: %A" value
| (false, _) -&amp;gt; printfn "No value found!" // Value: 1

match mp.TryGetValue "goot" with
| (true, value) -&amp;gt; printfn "Value: %A" value
| (false, _) -&amp;gt; printfn "No value found!" // No value found!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Loop Map
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;for KeyValue(key, value) in
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let map = Map.ofList [("doot", 1); ("beef", 2); ("hoopty", 3)]

for KeyValue(key, value) in map do
    printfn "Key: %s, Value: %d" key value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Map.iter
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let map = Map.ofList [("doot", 1); ("beef", 2); ("hoopty", 3)]
map |&amp;gt; Map.iter (fun key value -&amp;gt; printfn "Key: %s, Value: %d" key value)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Modify element of Map
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Add &amp;amp; Remove
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let map = Map.ofList [("doot", 1); ("beef", 2); ("hoopty", 3)]

let mapWithAddedElement = map.Add("pork", 4)
printfn "Map after adding (4, \"four\"): %A" mapWithAddedElement

let mapWithRemovedElement = mapWithAddedElement.Remove("beef")
printfn "Map after removing key 2: %A" mapWithRemovedElement
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Change&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Map.change key func table&lt;/p&gt;

&lt;p&gt;Returns a new map with the value stored under key changed according to func.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let input = Map [ (1, "a"); (2, "b") ]

let changed_map = input |&amp;gt; Map.change 1 (fun x -&amp;gt;
    match x with
    | Some s -&amp;gt; Some (s + "z")
    | None -&amp;gt; None
)
printfn "changed map: %A" changed_map // evaluates to map [(1, "az"); (2, "b")]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Practice
&lt;/h4&gt;

&lt;p&gt;Counting Words in text: "hello world hello map in F#"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let text = "hello world hello map in F#"
let words = text.Split(' ') // ["hello", "world", "hello", "map", "in", "F#"]

let wordCountMap = 
    words 
    |&amp;gt; Array.fold (fun acc word -&amp;gt;
        if Map.containsKey word acc then // Static Member Function
            let count = acc.[word]
            acc.Add(word, count + 1)
        else
            acc.Add(word, 1)
    ) Map.empty

printfn "Word count map: %A" wordCountMap // Word count map: map [(F#, 1); (hello, 2); (in, 1); (map, 1); (world, 1)]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the solution use Map.containsKey which is &lt;em&gt;Static Member Function&lt;/em&gt;, we can use acc &lt;em&gt;Instance Member Function&lt;/em&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let text = "hello world hello map in F#"
let words = text.Split(' ')
printfn "words: %A" words

let wordCountMap = 
    words 
    |&amp;gt; Array.fold (fun (acc: Map&amp;lt;string, int&amp;gt;) word -&amp;gt;
        if acc.ContainsKey word then // Instance Member Function
            let count = acc.[word]
            acc.Add(word, count + 1)
        else
            acc.Add(word, 1)
    ) Map.empty

printfn "Word count map: %A" wordCountMap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Static Member Function&lt;/em&gt; and &lt;em&gt;Instance Member Function&lt;/em&gt; will be introduced in OOP&lt;/p&gt;

</description>
      <category>fsharp</category>
    </item>
    <item>
      <title>F# For Dummys - Day 13 Collections Array</title>
      <dc:creator>AllenZhu</dc:creator>
      <pubDate>Thu, 23 May 2024 14:27:57 +0000</pubDate>
      <link>https://dev.to/pythonzhu/f-for-dummys-day-13-collections-array-3f5p</link>
      <guid>https://dev.to/pythonzhu/f-for-dummys-day-13-collections-array-3f5p</guid>
      <description>&lt;p&gt;Today we introduce another collection Array&lt;br&gt;
Array is a type of collection that stores a fixed-size sequence of elements of the same type&lt;br&gt;
Array is mutable, you can change the elements after the array has been created&lt;/p&gt;
&lt;h4&gt;
  
  
  Create Array
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Explicitly specifying elements
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array1 = [| 1; 2; 3 |]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;the ; could be omit between elements if you write them in seperated lines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array1 =
    [|
        1
        2
        3
     |]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;all elements should be the same type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array1 = [| 1; 2; "3"|]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the first elements is int, the last is string, will get compile error:&lt;br&gt;
All elements of an array must be implicitly convertible to the type of the first element, which here is 'int'. This element has type 'string'&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using comprehensions
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array1 = [| for i in 1 .. 5 -&amp;gt; i * i |] // [| 1; 4; 9; 16; 25 |]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Array.Empty
Returns an empty array of the given type
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myEmptyArray = Array.empty // Evaluates to [| |]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Array.init
Creates an array given the dimension and a generator function to compute the elements
syntax: Array.init count initializer
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array0 = Array.init 5 (fun i -&amp;gt; i) // [| 0; 1; 2; 3; 4 |]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Array.create
syntax: Array.init count value
Creates an array whose elements are all initially the given value
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let zeroes = Array.create 5 0 // zeroes = [| 0; 0; 0; 0; 0 |]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Array.zeroCreate
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arrayOfTenZeroes : int array = Array.zeroCreate 5 // [| 0; 0; 0; 0; 0 |]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Get element of Array
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Using index
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array0 = Array.init 5 (fun i -&amp;gt; i)

printfn "first element %A" array0.[0] // first element 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Using slice
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array0 = Array.init 5 (fun i -&amp;gt; i)

printfn "elements from 0 to 2: %A" array0.[0..2] // elements from 0 to 2: 0,1,2
printfn "elements from 2 to the end: %A" array0.[2..] // elements from 2 to the end: 2,3,4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Using build in method get
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let inputs = [| "a"; "b"; "c" |]
let secondElement = Array.get inputs 1

printfn "secondElement: %s" secondElement // secondElement: b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Loop an Array
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;for...in
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array1 = [| for i in 1 .. 5 -&amp;gt; i * i |]

for number in array1 do
    printfn "Number: %d" number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Array.iter
syntax: Array.iter action array
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array1 = [| for i in 1 .. 5 -&amp;gt; i * i |]

Array.iter (printfn "Number: %d") array1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Modify element of Array
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Using index to set value
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let inputs = [| "a"; "b"; "c" |]
inputs.[0] &amp;lt;- "d"

printfn "inputs: %A" inputs // d,b,c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Using build in method set
syntax: Array.set array index value
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array1 = [| for i in 1 .. 5 -&amp;gt; i * i |]
printfn "array1 before set: %A" array1 // [| 1; 4; 9; 16; 25 |]

for i in 0 .. array1.Length - 1 do
    Array.set array1 i (i * 2)

printfn "array1 after set: %A" array1 // [| 0; 2; 4; 6; 8 |]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Pattern Matching with Array
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sumFirstTwoElements arr =
    match arr with
    | [| x; y |] -&amp;gt; x + y
    | [| x |] -&amp;gt; x
    | [| |] -&amp;gt; 0
    | _ -&amp;gt; failwith "Array has more than two elements."

let sum1 = sumFirstTwoElements [| 1; 2 |]
let sum2 = sumFirstTwoElements [| 5 |]
let sum3 = sumFirstTwoElements [| |]

printfn "Sum of first two elements: %d" sum1 // 3
printfn "Sum of first element: %d" sum2 // 5
printfn "Sum of empty array: %d" sum3 // 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Operate Array
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Concat
Builds a new array that contains the elements of each of the given sequence of arrays
Array.concat arrays
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let inputs = [ [| 1; 2 |]; [| 3 |]; [| 4; 5 |] ]
let newArray = inputs |&amp;gt; Array.concat

printfn "newArray: %A" newArray
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Append
Builds a new array that contains the elements of the first array followed by the elements of the second array
syntax: Array.append array1 array2
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let results = Array.append [| 1; 2 |] [| 3; 4 |]

printfn "results: %A" results
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Filter
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let originalArray = [| 1; 2; 3; 4; 5; 6 |]
let evenNumbers = Array.filter (fun x -&amp;gt; x % 2 = 0) originalArray

printfn "evenNumbers: %A" evenNumbers // 2, 4, 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Map
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let originalArray = [| 1; 2; 3 |]
let doubleArray = Array.map (fun x -&amp;gt; 2 * x) originalArray

printfn "doubleArray: %A" doubleArray // doubleArray: 2,4,6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Fold
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [| 1; 2; 3; 4; 5 |]
let sum = Array.fold (fun acc x -&amp;gt; acc + x) 0 numbers

printfn "sum: %i" sum // sum: 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>fsharp</category>
    </item>
    <item>
      <title>F# For Dummys - Day 12 Collections List</title>
      <dc:creator>AllenZhu</dc:creator>
      <pubDate>Thu, 23 May 2024 08:17:06 +0000</pubDate>
      <link>https://dev.to/pythonzhu/f-for-dummys-day-12-collections-list-52g4</link>
      <guid>https://dev.to/pythonzhu/f-for-dummys-day-12-collections-list-52g4</guid>
      <description>&lt;p&gt;Today we learn List, an immutable, ordered collection of elements of the same type&lt;br&gt;
The term "ordered" here means that the list maintains the sequence in which elements were added, doesn't mean List is sorted&lt;/p&gt;
&lt;h4&gt;
  
  
  Create List
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Explicitly specifying elements
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1; 2; 3; 4; 5]
let names = ["Alice"; "Bob"; "Charlie"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;the ; could be omit between elements if you write them in seperated lines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let list123 = [ 1 // first comment
                2 // second comment
                3 // third comment
                ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;all elements should be the same type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let list123 = ["1"; 2; 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the first elements is string, the rest is int, will get compile error:&lt;br&gt;
All elements of a list must be implicitly convertible to the type of the first element, which here is 'string'. This element has type 'int'.(1,21)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using a range
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1..5] // Equivalent to [1; 2; 3; 4; 5]
let evenNumbers = [2..2..10] // Equivalent to [2; 4; 6; 8; 10]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Using list comprehensions
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let doubles = [for x in 1..5 -&amp;gt; x * 2] // Equivalent to [2; 4; 6; 8; 10]
let doubles = [for x in 2..2..10 -&amp;gt; x * 2] // Equivalent to [4; 8; 12; 16; 20]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Get element of List
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Using index
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1; 2; 3]
let firstNumber = numbers.[0]  // first number 1
let thirdNumber = numbers.[2]  // third number 3

printfn "firstNumber %i thirdNumber %i" firstNumber thirdNumber
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;if you use index not exist in List, will got index out of range exception&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1; 2; 3]
let forthNumber = numbers.[4]

printfn "forthNumber %i" forthNumber
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;compile error:&lt;br&gt;
Unhandled exception. System.ArgumentException: The index was outside the range of elements in the list&lt;br&gt;
the browser environment have bug about this exception&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using build-in method List.item
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1; 2; 3]
let firstNumber =  numbers |&amp;gt; List.item 0

printfn "firstNumber %i" firstNumber
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;head and tail
List.head, get first element
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1; 2; 3]
let head = numbers |&amp;gt; List.head

printfn "head %i" head
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;List.tail, return list after removing the first element, not the last element&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1; 2; 3]
let tail = numbers |&amp;gt; List.tail

printfn "tail %A" tail // tail [2; 3]
printfn "numbers %A" numbers // numbers [1; 2; 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;tail is a new List not including the first element, it won't change the value of original List numbers&lt;/p&gt;

&lt;h4&gt;
  
  
  Loop a List
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;for...in
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1; 2; 3]

for number in numbers do
    printfn "Number: %d" number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;List.iter
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1; 2; 3]

List.iter (fun number -&amp;gt; printfn "Number: %d" number) numbers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;fun define a lambda function here, it's equivalent to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1; 2; 3]
let printElement number = 
    printfn "Number: %d" number

List.iter printElement numbers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Pattern Matching with List
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers0 = []
let numbers1 = [1]
let numbers3 = [1; 2; 3]
let describeList lst =
    match lst with
    | [] -&amp;gt; "The list is empty"
    | [x] -&amp;gt; sprintf "The list has one element: %d" x
    | [x; y] -&amp;gt; sprintf "The list has two elements: %d and %d" x y
    | x :: xs -&amp;gt; sprintf "The list starts with %d and has more elements" x

printfn "%s" (describeList numbers0)  // The list is empty
printfn "%s" (describeList numbers1)  // The list has one element: 1
printfn "%s" (describeList numbers3)  // The list starts with %d and has more elements
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Operate List
&lt;/h4&gt;

&lt;p&gt;lists are indeed immutable, which means that you cannot modify a list after it has been created. Any operation that appears to modify a list (such as appending or prepending an element) actually creates a new list&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prepending an element
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let originalList = [2; 3; 4]
let newList = 1 :: originalList

printfn "Original List: %A" originalList // Original List: [2; 3; 4]
printfn "New List: %A" newList //  New List: [1; 2; 3; 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Appending an element
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let originalList = [1; 2; 3]
let newList = originalList @ [4]

printfn "Original List: %A" originalList  // Original List: [1; 2; 3]
printfn "New List: %A" newList //  New List: [1; 2; 3; 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Concatenate Lists
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let originalList = [1; 2; 3]
let newList = List.append originalList [4]

printfn "Original List: %A" originalList
printfn "New List: %A" newList
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Filter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;you can not remove an element in List directly as List is immutable, but you can filter element based on some condition, put them into a new List&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1; 2; 3; 4; 5]
let evenNumbers = List.filter (fun x -&amp;gt; x % 2 = 0) numbers

printfn "evenNumbers: %A" evenNumbers // evenNumbers: [2; 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Map&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;apply an operation to every elements in List, you can use loop/List comprehensions to achieve that, or use Map&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1; 2; 3; 4; 5]
let doubleNumbers = List.map (fun x -&amp;gt; 2 * x) numbers

printfn "doubleNumbers: %A" doubleNumbers // doubleNumbers:  [2; 4; 6; 8; 10]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Fold&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"fold" or "compress" elements of List of into a single value by applying a function repeatedly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numbers = [1; 2; 3; 4; 5]
let sum = List.fold (fun acc x -&amp;gt; acc + x) 0 numbers  // 15

printfn "sum: %i" sum // sum: 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;step 1: &lt;code&gt;acc = 0&lt;/code&gt; and &lt;code&gt;x = 1&lt;/code&gt; New &lt;code&gt;acc = 0 + 1 = 1&lt;/code&gt;&lt;br&gt;
step 2: &lt;code&gt;acc = 1&lt;/code&gt; and &lt;code&gt;x = 2&lt;/code&gt; New &lt;code&gt;acc = 1 + 2 = 3&lt;/code&gt;&lt;br&gt;
step 3: &lt;code&gt;acc = 3&lt;/code&gt; and &lt;code&gt;x = 3&lt;/code&gt; New &lt;code&gt;acc = 3 + 3 = 6&lt;/code&gt;&lt;br&gt;
step 4: &lt;code&gt;acc = 6&lt;/code&gt; and &lt;code&gt;x = 4&lt;/code&gt; New &lt;code&gt;acc = 6 + 4 = 10&lt;/code&gt;&lt;br&gt;
step 5: &lt;code&gt;acc = 10&lt;/code&gt; and &lt;code&gt;x = 5&lt;/code&gt; New &lt;code&gt;acc = 10 + 5 = 15&lt;/code&gt;&lt;/p&gt;

</description>
      <category>fsharp</category>
    </item>
    <item>
      <title>F# For Dummys - Day 11 Collections Tuple</title>
      <dc:creator>AllenZhu</dc:creator>
      <pubDate>Wed, 22 May 2024 13:30:14 +0000</pubDate>
      <link>https://dev.to/pythonzhu/f-for-dummys-day-11-data-structure-tuple-8cd</link>
      <guid>https://dev.to/pythonzhu/f-for-dummys-day-11-data-structure-tuple-8cd</guid>
      <description>&lt;p&gt;Today we learn Tuple, as the first data structure of Collections&lt;br&gt;
tuple is a simple data structure that combine elements together. These elements can be different types&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(1, 2)

// Triple of strings.
("one", "two", "three")

// Tuple of generic types.
(a, b)

// Tuple that has mixed types.
("one", 1, 2.0)

// Tuple of integer expressions.
(a + 1, b + 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's do some practices&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;defining a tuple with 2 elements
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = ("John", 30)
let (name, age) = person   // this is unpack, unpack first element of person to name, second to age

printfn "Name: %s, Age: %d" name age
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can also get the element by fst snd for a tuple with 2 elements like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let person = ("John", 30)
let name = fst person   // "John"
let age = snd person    // 30

printfn "Name: %s, Age: %d" name age
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;defining a tuple with 3 elements
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let point = (3, 4, 5)
let (x, y, z) = point

printfn "Point coordinates: x = %d, y = %d, z = %d" x y z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;there is no trd to get third element of tuple with 3 elements&lt;br&gt;
there is no empty tuple, () is not empty tuple, it's more like Null&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ignore element of tuple
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let point = (3, 4, 5)
let (x, _, z) = point

printfn "Point coordinates: x = %d, z = %d" x z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the second element 4 will be ignored&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;return a tuple in function
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let getPersonInfo() = ("John", 30)
let (personName, personAge) = getPersonInfo()

printfn "Person Name: %s, Person Age: %d" personName personAge
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;access elements of tuple in match
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let print tuple1 =
   match tuple1 with
    | (a, b) -&amp;gt; printfn "Pair %A %A" a b // %A can format any type

print (1, 2)
print ("John", 30)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tuples are particularly useful when you need to return multiple values from a function or when you want to combine several values together&lt;/p&gt;

</description>
      <category>fsharp</category>
    </item>
    <item>
      <title>F# For Dummys - Day 10 Loop</title>
      <dc:creator>AllenZhu</dc:creator>
      <pubDate>Tue, 21 May 2024 12:12:33 +0000</pubDate>
      <link>https://dev.to/pythonzhu/f-for-dummys-day-10-5dog</link>
      <guid>https://dev.to/pythonzhu/f-for-dummys-day-10-5dog</guid>
      <description>&lt;p&gt;Today we learn loop, do a task repeatly&lt;/p&gt;

&lt;h4&gt;
  
  
  What is loop
&lt;/h4&gt;

&lt;p&gt;loop is used to repeat a block of code multiple times&lt;br&gt;
we need to set when to start, how many times it repeats or condition to end loops&lt;br&gt;
a loop never ends is called infinite loop&lt;/p&gt;
&lt;h4&gt;
  
  
  Three ways to loop
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;for loop
Iterates over a specified range of values. The loop variable takes each value in the range one by one
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Forward for loop
for i = 1 to 5 do
    printfn "Jim has run %d laps" i

// Reverse for loop
for i = 5 downto 1 do
    printfn "Days until birthday: %d days left" (i - 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;while loop
Repeatedly executes the block of code as long as the condition remains true
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mutable counter = 1
while counter &amp;lt;= 5 do
    printfn "Jim has run %d laps" counter
    counter &amp;lt;- counter + 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;recursive function
Calls itself with updated parameters&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;syntax: let rec funcName&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let rec recursiveLoop n =
    if n &amp;gt; 0 then
        printfn "Jim run 1 lap: %d laps left" (n - 1)
        recursiveLoop (n - 1)

recursiveLoop 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Understanding recursive function:&lt;/p&gt;

&lt;p&gt;Imagine you have a set of &lt;a href="https://www.quora.com/What-is-a-Russian-doll"&gt;Russian dolls&lt;/a&gt;, where each doll contains a smaller doll inside it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcpxmejowt131vlev0dxx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcpxmejowt131vlev0dxx.png" alt="Image description" width="602" height="452"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To find the smallest doll, you would:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open the current doll&lt;/li&gt;
&lt;li&gt;Check if there is another smaller doll inside&lt;/li&gt;
&lt;li&gt;If there is, repeat the process with the smaller doll
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Pseudocode
let findSmallestDoll doll =
    if doll contains no smaller doll then
        return doll
    else
        let innerDoll = open(doll) // smaller doll inside
        findSmallestDoll(innerDoll)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  How to choose
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;for loop is simple and easy to understand when iterating over a known range of values&lt;/li&gt;
&lt;li&gt;while loop is useful when the number of iterations is not known beforehand&lt;/li&gt;
&lt;li&gt;recursive function making code more expressive and often easier to reason about&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;exercises:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;1 + 2 + 3 + ... + 99 + 100 = ?&lt;/li&gt;
&lt;li&gt;1 + 3 + 5 + ... + 97 + 99 = ?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;answers:&lt;br&gt;
question 1&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mutable sum = 0
for i = 1 to 100 do
    sum &amp;lt;- sum + i
printfn "Sum using for loop: %d" sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;while
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mutable sum = 0
let mutable counter = 1
while counter &amp;lt;= 100 do
    sum &amp;lt;- sum + counter
    counter &amp;lt;- counter + 1
printfn "Sum using while loop: %d" sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;recursive function
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let rec sumRecursive n acc =
    if n = 0 then acc
    else sumRecursive (n - 1) (acc + n)

printfn "Sum using recursive function: %d" (sumRecursive 100 0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;question 2&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mutable sum = 0
for i = 1 to 100 do
    if i % 2 &amp;lt;&amp;gt; 0 then
        sum &amp;lt;- sum + i
printfn "Sum using for loop: %d" sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;while
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mutable sum = 0
let mutable counter = 1
while counter &amp;lt;= 100 do
    if counter % 2 &amp;lt;&amp;gt; 0 then
        sum &amp;lt;- sum + counter
    counter &amp;lt;- counter + 1
printfn "Sum using while loop: %d" sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;recursive function
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let rec sumOddRecursive n acc =
    if n = 0 then acc
    else 
        let newAcc = if n % 2 &amp;lt;&amp;gt; 0 then acc + n else acc
        sumOddRecursive (n - 1) newAcc

printfn "Sum using recursive function: %d" (sumOddRecursive 100 0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>fsharp</category>
    </item>
    <item>
      <title>F# For Dummys - Day 9 Branching</title>
      <dc:creator>AllenZhu</dc:creator>
      <pubDate>Mon, 20 May 2024 12:52:47 +0000</pubDate>
      <link>https://dev.to/pythonzhu/f-for-dummys-day-9-l9p</link>
      <guid>https://dev.to/pythonzhu/f-for-dummys-day-9-l9p</guid>
      <description>&lt;p&gt;Today we learn control flow, or branching&lt;br&gt;
sometimes we need to execute a block of code only when it meets some condition&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Pseudocode
if conditionOne then doTaskOne
else if conditionTwo then doTaskTwo
else doSomethingElse
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we introduce 2 ways of branching&lt;/p&gt;

&lt;h4&gt;
  
  
  if...then
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let temperature = 15

if temperature &amp;lt; 0 then
    printfn "Wear down coat"
elif temperature &amp;lt; 10 then
    printfn "Wear sweater"
elif temperature &amp;lt; 20 then
    printfn "Wear long sleeves"
elif temperature &amp;lt; 30 then
    printfn "Wear T-shirt"
else
    printfn "Wear bikini"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;temperature = 15, the first branch suits is &lt;em&gt;temperature &amp;lt; 20&lt;/em&gt; , even though &lt;em&gt;temperature &amp;lt; 30&lt;/em&gt; is also true, the rest logic will not execute&lt;/p&gt;

&lt;h4&gt;
  
  
  match...with
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Match expression. 
match test-expression with
| pattern1 [ when condition ] -&amp;gt; result-expression1
| pattern2 [ when condition ] -&amp;gt; result-expression2
| ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;equivalent example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let temperature = 15

match temperature with
| temperature when temperature &amp;lt; 0 -&amp;gt; printfn "Wear down coat"
| temperature when temperature &amp;lt; 10 -&amp;gt; printfn "Wear sweater"
| temperature when temperature &amp;lt; 20 -&amp;gt; printfn "Wear long sleeves"
| temperature when temperature &amp;lt; 30 -&amp;gt; printfn "Wear T-shirt"
| _ -&amp;gt; printfn "Wear bikini"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;AND Pattern: we can combine conditions with (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;) operator, it hits when both condition match&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let age = 10

match age with
| age when age &amp;gt; 0 &amp;amp;&amp;amp; age &amp;lt;= 1 -&amp;gt; printfn "Infant"
| age when age &amp;gt; 1 &amp;amp;&amp;amp; age &amp;lt;= 12 -&amp;gt; printfn "Child"
| age when age &amp;gt; 12 &amp;amp;&amp;amp; age &amp;lt;= 16 -&amp;gt; printfn "Teenager"
| age when age &amp;gt; 16 -&amp;gt; printfn "Adults"
| _ -&amp;gt; printfn "Not born"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR Pattern: we can combine patterns using the (&lt;code&gt;||&lt;/code&gt;) operator to match multiple patterns&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = 10

match x with
| x when x &amp;gt; 0 || x = 0 -&amp;gt; printfn "Positive or Zero"
| _ -&amp;gt; printfn "Negative"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can define a pattern matching function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let temperature = 15

let clothingAdvice temp =
    match temp with
    | t when t &amp;lt; 0 -&amp;gt; "Wear down coat"
    | t when t &amp;lt; 10 -&amp;gt; "Wear sweater"
    | t when t &amp;lt; 20 -&amp;gt; "Wear long sleeves"
    | t when t &amp;lt; 30 -&amp;gt; "Wear T-shirt"
    | _ -&amp;gt; "Wear bikini"

printfn "%s" (clothingAdvice temperature)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;another example from windows documentation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Shape =
    | Square of side: double
    | Rectangle of width: double * length: double

let getArea shape =
    match shape with
    | Square side -&amp;gt; side * side
    | Rectangle (width, length) -&amp;gt; width * length

let square = Square 2.0
printfn $"The area of the square is {getArea square}"
let rect = Rectangle (3.0, 2.0)
printfn $"The area of the rect is {getArea rect}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;getArea function calculate the area according to different shape &lt;br&gt;
for a Square, it use side * side = 2.0 * 2.0&lt;br&gt;
for a Rectangle, it use width * length = 3.0 * 2.0&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The area of the square is 4
The area of the rect is 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we didn't learn type yet, it defines a Union type here, and there is more powerful usage of &lt;strong&gt;match&lt;/strong&gt;, we will introduce type, Union and discover more usage of match in the coming days&lt;/p&gt;

</description>
      <category>fsharp</category>
    </item>
    <item>
      <title>F# For Dummys - Day 8 Function &amp;&amp; Pipeline</title>
      <dc:creator>AllenZhu</dc:creator>
      <pubDate>Mon, 20 May 2024 11:11:06 +0000</pubDate>
      <link>https://dev.to/pythonzhu/f-for-dummys-day-8-2bch</link>
      <guid>https://dev.to/pythonzhu/f-for-dummys-day-8-2bch</guid>
      <description>&lt;p&gt;Today we learn how to define a function and pipeline&lt;/p&gt;

&lt;h4&gt;
  
  
  what is function
&lt;/h4&gt;

&lt;p&gt;function is a block of code that takes input values (parameters), and returns an output value&lt;br&gt;
normally we perform some task with the input, it will be less useful just return the input value&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;funtion no param input:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let say_hello() = "Hello"
printfn "say_hello: %s" (say_hello())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;funtion one param input
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let say_hello_to name = "Hello " + name
printfn "say_hello_to: %s" (say_hello_to "Lucy")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;funtion two param input
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let say_hello_to name0 name1 = name0 + " say Hello to " + name1
printfn "say_hello_to: %s" (say_hello_to "Lucy" "Lily")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;function can be a param of other function
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let applyTwice f x = f (f x)
let increment x = x + 1
let result = applyTwice increment 5

printfn "Result: %d" result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;increment is the first param of function applyTwice here, it's increment(increment(x)), result is (5 + 1) + 1 = 7&lt;/p&gt;
&lt;h4&gt;
  
  
  lambda
&lt;/h4&gt;

&lt;p&gt;lambda expression is an anonymous function (a function without a name) that you can define in place, often to be passed as an argument to higher-order functions&lt;br&gt;
syntax of a lambda expression&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun arguments -&amp;gt; expression
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;a simple add function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let add x y = 
    x + y
printfn "%d" (add 3 5)  // Output: 8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;a lambda version&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;printfn "%d" ((fun x y -&amp;gt; x + y) 3 5)  // Output: 8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can not use this fun at other place as it &lt;em&gt;Dispose&lt;/em&gt; after finish&lt;/p&gt;

&lt;h4&gt;
  
  
  pipeline
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
forward pipeline |&amp;gt;, data flow from left to right&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;we can combine multiple functions one after one&lt;br&gt;
function1 |&amp;gt; function2 |&amp;gt; function3, it's called pipeline&lt;br&gt;
the function param example could be like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let applyTwice f x = f x |&amp;gt; f
let increment x = x + 1
let result = applyTwice increment 5

printfn "Result: %d" result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;another longer pipeline&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let addOne x = x + 1
let square x = x * x
let subtractFive x = x - 5

let result = 5 |&amp;gt; addOne |&amp;gt; square |&amp;gt; subtractFive // (5 + 1)^2 - 5
printfn "The result is %d" result // output: The result is 31
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the output of first function is the input of next function&lt;br&gt;
result = 5 |&amp;gt; addOne, addOne get 5, return 5 + 1 = 6, square get 6, return 6 * 6 = 36, substractFive get 36, return 36 -5 = 31&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&amp;lt;| backward pipeline, data flow from right to left
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let addOne x = x + 1
let square x = x * x
let subtractFive x = x - 5

let result = subtractFive &amp;lt;| (square &amp;lt;| (addOne 5))
printfn "The result is %d" result // output: The result is 31
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  why pipeline
&lt;/h4&gt;

&lt;p&gt;an example for morning routine&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let getOutOfBed () = "Out of bed"
let brushTeeth state = state + " -&amp;gt; Brushed teeth"
let takeShower state = state + " -&amp;gt; Took shower"
let getDressed state = state + " -&amp;gt; Got dressed"
let eatBreakfast state = state + " -&amp;gt; Ate breakfast"

let morningRoutine =
    getOutOfBed ()
    |&amp;gt; brushTeeth
    |&amp;gt; takeShower
    |&amp;gt; getDressed
    |&amp;gt; eatBreakfast

printfn "Morning routine completed: %s" morningRoutine // output: Morning routine completed: Out of bed -&amp;gt; Brushed teeth -&amp;gt; Took shower -&amp;gt; Got dressed -&amp;gt; Ate breakfast
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;there is an equivalent way to get the same result, which is nested function calls&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let getOutOfBed () = "Out of bed"
let brushTeeth state = state + " -&amp;gt; Brushed teeth"
let takeShower state = state + " -&amp;gt; Took shower"
let getDressed state = state + " -&amp;gt; Got dressed"
let eatBreakfast state = state + " -&amp;gt; Ate breakfast"

let morningRoutine = eatBreakfast(getDressed(takeShower(brushTeeth(getOutOfBed()))))
printfn "Morning routine completed: %s" morningRoutine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the last function &lt;strong&gt;eatBreakfast&lt;/strong&gt; is put in the first place&lt;br&gt;
which way do you prefer&lt;br&gt;
we can see the pipeline way is more readable in this style:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The order of operations is written in a natural, top-to-bottom manner, which is easier to read and understand&lt;/li&gt;
&lt;li&gt;  It is more intuitive because it reads like a sequence of steps or instructions&lt;/li&gt;
&lt;li&gt;  Adding or removing steps is straightforward, you just add or remove lines&lt;/li&gt;
&lt;li&gt;  Each function operates on the result of the previous function, clearly showing the flow of data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;pipeline way is recommended in F#, the data flows in pipeline, elegant!&lt;/p&gt;

</description>
      <category>fsharp</category>
    </item>
    <item>
      <title>F# For Dummys - Day 7 Operators</title>
      <dc:creator>AllenZhu</dc:creator>
      <pubDate>Sat, 18 May 2024 12:08:33 +0000</pubDate>
      <link>https://dev.to/pythonzhu/f-for-dummys-day-7-eig</link>
      <guid>https://dev.to/pythonzhu/f-for-dummys-day-7-eig</guid>
      <description>&lt;p&gt;Today we learn operators and do some exercises&lt;/p&gt;

&lt;h3&gt;
  
  
  Operators
&lt;/h3&gt;

&lt;p&gt;Operators are special symbols that perform operations on one or more operands (values or variables)&lt;br&gt;
operator for 2 variables:&lt;br&gt;
like &lt;strong&gt;+&lt;/strong&gt;: 1 + 1 = 2, the '+' here is operator, it add the left number 1 and right number 1&lt;br&gt;
we introduce 6 operators&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi6wv4j6sgef4t7xaywvx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi6wv4j6sgef4t7xaywvx.png" alt="Image description" width="630" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;+&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num0 = 1
let num1 = 1
let sum = num0 + num1

printfn "1 + 1 = %i" sum // output: 1 + 1 = 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;-&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num0 = 2
let num1 = 1
let difference = num0 - num1

printfn "2 - 1 = %i" difference // output: 2 - 1 = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;/&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num0 = 4
let num1 = 2
let quotient = num0 / num1

printfn "4 / 2 = %i" quotient // output: 4 / 2 = 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;%&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num0 = 3
let num1 = 2
let remainder = num0 % num1

printfn "3 %% 2 = %i" remainder // output: 3 % 2 = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&amp;lt;&amp;gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num0 = 3
let num1 = 2
let equal = num0 &amp;lt;&amp;gt; num1

printfn "is 3 not equals 2? %b" equal // output: is 3 not equals 2? true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;=&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num0 = 3
let num1 = 2
let equal = num0 = num1

printfn "is 3 equals 2? %b" equal // output: is 3 equals 2? false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  exercises
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;1 + 2 * 3 = ?&lt;/li&gt;
&lt;li&gt;(1 + 2) * 3 = ?&lt;/li&gt;
&lt;li&gt;2 + 2 = 2 * 2?&lt;/li&gt;
&lt;li&gt;there are 20 apples for lunch, 10 students in the class, how many apples one student can get?&lt;/li&gt;
&lt;li&gt;there are 21 apples for lunch, 10 students in the class, how many apples left?&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  answers
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;'*' has higher priority than +
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let results = 1 + 2 * 3
printfn "1 + 2 * 3 = %i" results // output: 1 + 2 * 3 = 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;() changed the priority, + inside has higher priority than *
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let results = (1 + 2) * 3
printfn "(1 + 2) * 3 = %i" results // output: (1 + 2) * 9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;= can compare expression, the right = has higher priority than left =, which asign value of results
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let results = 2 + 2 = 2 * 2
printfn "2 + 2 = 2 * 2 is %b" results // output: 2 + 2 = 2 * 2 is true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;4.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let number_of_apples = 20
let number_of_students = 10
let apples_of_each_student = number_of_apples / number_of_students
printfn "every student can get %i apple" apples_of_each_student // output: every student can get 2 apple
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let number_of_apples = 21
let number_of_students = 10
let apples_left = number_of_apples % number_of_students
printfn "%i apple left after distributing" apples_left // output: 1 apple left after distributing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>fsharp</category>
    </item>
    <item>
      <title>F# For Dummys - Day 6 Format</title>
      <dc:creator>AllenZhu</dc:creator>
      <pubDate>Fri, 17 May 2024 03:48:15 +0000</pubDate>
      <link>https://dev.to/pythonzhu/f-for-dummys-day-6-2cpj</link>
      <guid>https://dev.to/pythonzhu/f-for-dummys-day-6-2cpj</guid>
      <description>&lt;p&gt;Today we learn how to format the output of println, and a new type char&lt;/p&gt;

&lt;h3&gt;
  
  
  what is formatting?
&lt;/h3&gt;

&lt;p&gt;formatting is converting values into strings according to a specified format&lt;br&gt;
we introduce 2 ways to format:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;String interpolation&lt;/strong&gt;: one way to combine variables and text is to use something called interpolation
actually we used it yesterday
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name = "Lucy"
let age = 14
printfn $"my name is {name}, I'm {age} years old"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;$ tell the compiler: please use the variable we defined before in this string&lt;br&gt;
{} indicate what exactly the variables we need, here in this example is {name} and {age}&lt;br&gt;
so it will replace the value of variables at the location of brace&lt;br&gt;
like type of age is int, it's converted to string '14' in the final output, we didn't transfer it explicitly&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Specifiers&lt;/strong&gt;: You can also use format specifiers as part of what you're trying to print. Using specifiers is the most commonly used way to format in F#
Let's use specifier to format in our example:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name = "Lucy"
let age = 14
printfn "my name is %s, I'm %i years old" name age
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;you can see the output is the same&lt;br&gt;
%s is for name, it's for string type&lt;br&gt;
%i is for age, it's for int type&lt;br&gt;
we introduce 4 of them today:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe0npkbtrsw8ru2bznjf6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe0npkbtrsw8ru2bznjf6.png" alt="Image description" width="724" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;%b for bool&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let is_this_apple = true
let is_this_orange = false

printfn "Is this an apple? %b" is_this_apple
printfn "Is this an orange? %b" is_this_orange
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;%c for char&lt;br&gt;
we have learned string type to describe a text, it represents a sequence of characters&lt;br&gt;
while char represents a single character, like 'a', 'b', 'c', 'd' or '!', '@', '#', '$'&lt;br&gt;
string and char are different types, not just the length, you can not concat them like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let str = "Hello"
let ch = '!'
let result = str + ch

printfn "%s" result  // Output: Hello!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will get error: The type 'char' does not match the type 'string'&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ugsgbgplk5urhm45247.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ugsgbgplk5urhm45247.jpg" alt="Image description" width="800" height="132"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we can transfer a char to string like this, then can concat them properly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let str = "Hello"
let ch = '!'
let result = str + (string)ch

printfn "%s" result  // Output: Hello!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;the full specifiers for other types are &lt;a href="https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/plaintext-formatting#format-specifiers-for-printf"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>fsharp</category>
    </item>
    <item>
      <title>F# For Dummys - Day 5 Mutable</title>
      <dc:creator>AllenZhu</dc:creator>
      <pubDate>Thu, 16 May 2024 08:20:05 +0000</pubDate>
      <link>https://dev.to/pythonzhu/f-for-dummys-day-5-2h9j</link>
      <guid>https://dev.to/pythonzhu/f-for-dummys-day-5-2h9j</guid>
      <description>&lt;p&gt;Today we learn how to change the value of variable(not variable actually)&lt;br&gt;
remember how to define a value&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name = "Lucy"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can we change our name? let's try to change the value of our string variable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name = "Lucy"
name &amp;lt;- "Lily"
printfn $"{name}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run, whoops, compile error&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo1m9y1hxcnabl2n86aia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo1m9y1hxcnabl2n86aia.png" alt="Image description" width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the error message on the Problems window:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This value is not mutable. Consider using the mutable keyword, e.g. 'let mutable name = expression'.(2,1)&amp;lt;/br&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;obviously, change value of name is not allowed&lt;br&gt;
luckily, the error message is clear and friendly, it even suggest how to fix the problem&lt;br&gt;
let's try it's sugguestion:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mutable name = "Lucy"
name &amp;lt;- "Lily"
printfn $"{name}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;run the code, name has been changed successfully!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6e6kj2tm5qddfjr381zc.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6e6kj2tm5qddfjr381zc.jpg" alt="Image description" width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;conclusion:&lt;br&gt;
value we define in F# is immutable by default, we need to explicitly add 'mutable' before value, now we can call it variables, as we can change it's value later&lt;/p&gt;

&lt;p&gt;Let's do a practice:&lt;br&gt;
my name is Lucy, I'm 14 year's old&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name = "Lucy"
let age = 14
printfn $"my name is {name}, I'm {age} years old"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;my sister is Lily, she's 11 year's old, please introduce herself for Lily without define new values&lt;br&gt;
answer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mutable name = "Lucy" // name is variable now
let mutable age = 14
printfn $"my name is {name}, I'm {age} years old"
name &amp;lt;- "Lily"
age &amp;lt;- 11
printfn $"my name is {name}, I'm {age} years old"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;output:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fau3dqggle47ih955lymo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fau3dqggle47ih955lymo.jpg" alt="Image description" width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>fsharp</category>
    </item>
  </channel>
</rss>
