<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>.Net</title>
        <link>http://geekswithblogs.net/Domagala/category/10207.aspx</link>
        <description>.Net</description>
        <language>en-US</language>
        <copyright>Domagala</copyright>
        <managingEditor>Domagala.Lukas@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>FSharp Pattern Matching gotcha and Active Patterns</title>
            <link>http://geekswithblogs.net/Domagala/archive/2009/06/25/fsharp-pattern-matching-gotcha-and-active-patterns.aspx</link>
            <description>&lt;p&gt;While I was building the position heuristic function for Connect Four I ran into an interesting gotcha with F# pattern matching. Lets see if you see it before I tell you what it is:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let rec &lt;/span&gt;heuristic (positions: (int * int) list) (pos: int*int) =
    &lt;span style="color: blue"&gt;match &lt;/span&gt;positions &lt;span style="color: blue"&gt;with
    &lt;/span&gt;| [] &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;0
    | position::_ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;1 + (heuristic (List.tl positions) pos)
    | _ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;heuristic (List.tl positions) pos&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Looking at it, it seems the code should tell me how often value is in position, right? Well it doesn’t! And there is a good reason for it. When pattern matching you are capturing a new value instead of testing for one. So in line 4 this code defines pos to be the head of position, now matter what is in there. That’s the reason the compiler warns us that last rule will never match.&lt;/p&gt;

&lt;p&gt;What we should have done is this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let rec &lt;/span&gt;heuristic (positions: (int * int) list) (pos: int*int) =
    &lt;span style="color: blue"&gt;match &lt;/span&gt;positions &lt;span style="color: blue"&gt;with
    &lt;/span&gt;| [] &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;0
    | position::_ &lt;span style="color: blue"&gt;when &lt;/span&gt;pos = position &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;1 + (heuristic (List.tl positions) pos)
    | _ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;heuristic (List.tl positions) pos&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;But I have to say, this looks a little ugly compared to the first version. There is a &lt;a href="http://blogs.msdn.com/chrsmith/archive/2008/10/03/f-zen-the-literal-attribute.aspx" target="_blank"&gt;blog post&lt;/a&gt; by Chris Smith about this, where he tells us to use the Literal attribute if we want it match against a value. Well this does not work for anything other then primitive types… &lt;/p&gt;

&lt;p&gt;So how do we get our pattern matching code to look nice again? &lt;a href="http://www.infoq.com/articles/Beyond-Foundations-FSharp" target="_blank"&gt;Active Patterns&lt;/a&gt; to the rescue! Active Patterns are a little hard to grasp at first, but they can save you a lot of code and make your designs much nicer, if used right. I always think of an Active Pattern as pattern matching against a function application. &lt;/p&gt;

&lt;p&gt;Lets see what happens when we use them in our example: &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let rec &lt;/span&gt;heuristic (positions: (int * int) list) (pos: int*int) =
    &lt;span style="color: blue"&gt;let &lt;/span&gt;(|Same|Different|) position = &lt;span style="color: blue"&gt;if &lt;/span&gt;pos = position &lt;span style="color: blue"&gt;then &lt;/span&gt;Same &lt;span style="color: blue"&gt;else &lt;/span&gt;Different
    &lt;span style="color: blue"&gt;match &lt;/span&gt;positions &lt;span style="color: blue"&gt;with
    &lt;/span&gt;| [] &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;0
    | Same ::_  &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;1 + (heuristic (List.tl positions) pos)
    | Different ::_ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;(heuristic (List.tl positions) pos)&lt;/pre&gt;

&lt;p&gt;I think this code is a lot clearer then what the second version and works with any type you throw at it. &lt;/p&gt;

&lt;p&gt;Active Patterns are good for a lot more then this but I’ll get into other applications of it some other time. Just remember this pattern matching gotcha so you don’t waste any time pulling out your hair;)&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Just to complete this, here is the version that you should be using, if you want to copy paste this code. It uses tail recursion with an accumulator argument so that the stack doesn’t grow.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let rec &lt;/span&gt;heuristic (positions: (int * int) list) (pos: int*int) =
    &lt;span style="color: blue"&gt;let &lt;/span&gt;(|Same|Different|) position = &lt;span style="color: blue"&gt;if &lt;/span&gt;pos = position &lt;span style="color: blue"&gt;then &lt;/span&gt;Same &lt;span style="color: blue"&gt;else &lt;/span&gt;Different
    &lt;span style="color: blue"&gt;let rec &lt;/span&gt;loop positions acc =        
        &lt;span style="color: blue"&gt;match &lt;/span&gt;positions &lt;span style="color: blue"&gt;with
        &lt;/span&gt;| [] &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;acc
        | Same :: _  &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;(loop (List.tl positions) (acc + 1))
        | Different :: _ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;(loop (List.tl positions) acc)
    loop positions 0&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:dbbae6a2-2fdd-4e53-b226-77e52fe6f44d" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/.Net" rel="tag"&gt;.Net&lt;/a&gt;,&lt;a href="http://technorati.com/tags/F%23" rel="tag"&gt;F#&lt;/a&gt;,&lt;a href="http://technorati.com/tags/FSharp" rel="tag"&gt;FSharp&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Pattern+Matching" rel="tag"&gt;Pattern Matching&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Active+Patterns" rel="tag"&gt;Active Patterns&lt;/a&gt;,&lt;a href="http://technorati.com/tags/gotcha" rel="tag"&gt;gotcha&lt;/a&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/Domagala/aggbug/133054.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Domagala</dc:creator>
            <guid>http://geekswithblogs.net/Domagala/archive/2009/06/25/fsharp-pattern-matching-gotcha-and-active-patterns.aspx</guid>
            <pubDate>Thu, 25 Jun 2009 20:18:07 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Domagala/comments/133054.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Domagala/archive/2009/06/25/fsharp-pattern-matching-gotcha-and-active-patterns.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Domagala/comments/commentRss/133054.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Domagala/services/trackbacks/133054.aspx</trackback:ping>
        </item>
        <item>
            <title>Performance of Lists, Sequences and Arrays as loopcounters</title>
            <link>http://geekswithblogs.net/Domagala/archive/2009/06/23/performance-of-lists-sequences-and-arrays-as-loopcounters.aspx</link>
            <description>&lt;p&gt;I ran into some rather interesting numbers while trying to optimize my Connect Four implementation. Try to guess what this code will print out:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;test=
    &lt;span style="color: blue"&gt;let &lt;/span&gt;stop1 = Stopwatch.StartNew()
    &lt;span style="color: blue"&gt;let &lt;/span&gt;list = [1..1000000]     
    &lt;span style="color: blue"&gt;let &lt;/span&gt;bla = list |&amp;gt; List.fold (&lt;span style="color: blue"&gt;fun &lt;/span&gt;state x &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;state + x) 0
    stop1.Stop()
    &lt;span style="color: blue"&gt;let &lt;/span&gt;stop2 = Stopwatch.StartNew()
    &lt;span style="color: blue"&gt;let &lt;/span&gt;seq = seq{1..1000000} |&amp;gt; Seq.fold (&lt;span style="color: blue"&gt;fun &lt;/span&gt;state x &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;state + x) 0
    stop2.Stop()  
    &lt;span style="color: blue"&gt;let &lt;/span&gt;stop3 = Stopwatch.StartNew()
    &lt;span style="color: blue"&gt;let &lt;/span&gt;arr = [|1..1000000|]     
    &lt;span style="color: blue"&gt;let &lt;/span&gt;arr1 = arr|&amp;gt; Array.fold (&lt;span style="color: blue"&gt;fun &lt;/span&gt;state x &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;state + x) 0
    stop3.Stop()
    &lt;span style="color: blue"&gt;let &lt;/span&gt;stop4 = Stopwatch.StartNew()
    &lt;span style="color: blue"&gt;let &lt;/span&gt;seqList = [1..1000000] |&amp;gt; Seq.fold (&lt;span style="color: blue"&gt;fun &lt;/span&gt;state x &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;state + x) 0
    stop4.Stop()  
    printfn &lt;span style="color: maroon"&gt;"list: %d seq: %d array: %d seqList: %d" 
            &lt;/span&gt;stop1.ElapsedTicks 
            stop2.ElapsedTicks 
            stop3.ElapsedTicks 
            stop4.ElapsedTicks&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;list: 18357402 seq: 10837883 array: 8420279 seqList: 15246287&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;It seems creating a list enumerable takes about double the time it takes to create either a sequence or an array. Usually this does not matter that much, but it can give you worse performance if you are using fold/map instead of manual looping. I guess there is a performance hit for simplicity…&lt;/p&gt;

&lt;p&gt;Lets see how well we can do in the imperative world:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;test2=
    &lt;span style="color: blue"&gt;let &lt;/span&gt;stop1 = Stopwatch.StartNew()
    &lt;span style="color: blue"&gt;let mutable &lt;/span&gt;i = 1
    &lt;span style="color: blue"&gt;let mutable &lt;/span&gt;y = 0
    &lt;span style="color: blue"&gt;while &lt;/span&gt;i &amp;lt; 1000000 &lt;span style="color: blue"&gt;do
        &lt;/span&gt;y &amp;lt;- y+i
        i &amp;lt;- i+1
    stop1.Stop()
    printfn &lt;span style="color: maroon"&gt;"loop: %d" &lt;/span&gt;stop1.ElapsedTicks&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;loop: 40852&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Interesting, it seems most of the time is wasted creating the data structure we want to loop over, so what happens if we don’t count the creation of the array?&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;test3=
    &lt;span style="color: blue"&gt;let &lt;/span&gt;array = [|1..1000000|] 
    &lt;span style="color: blue"&gt;let &lt;/span&gt;stop = Stopwatch.StartNew()
    &lt;span style="color: blue"&gt;let &lt;/span&gt;result = array |&amp;gt; Array.fold (&lt;span style="color: blue"&gt;fun &lt;/span&gt;state x &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;state + x) 0
    stop.Stop()  
    printfn &lt;span style="color: maroon"&gt;"array: %d" &lt;/span&gt;stop.ElapsedTicks&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;array: 65653&lt;/p&gt;

&lt;p&gt;Well we are getting closer. Although we have to problem now that we have to cache the arrays we want to loop over. Lets try not being so wasteful and doing it all in a nice recursive loop:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;test4=
    &lt;span style="color: blue"&gt;let &lt;/span&gt;sum x=
        &lt;span style="color: blue"&gt;let rec &lt;/span&gt;loop x y =
            &lt;span style="color: blue"&gt;if &lt;/span&gt;y = 0 &lt;span style="color: blue"&gt;then &lt;/span&gt;x
            &lt;span style="color: blue"&gt;else &lt;/span&gt;loop (x+y) (y-1)
        loop 0 x
    &lt;span style="color: blue"&gt;let &lt;/span&gt;stop = Stopwatch.StartNew()
    &lt;span style="color: blue"&gt;let &lt;/span&gt;result =sum 1000000
    stop.Stop()
    printfn &lt;span style="color: maroon"&gt;"recursive: %d" &lt;/span&gt;stop.ElapsedTicks&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;recursive: 28952&lt;/pre&gt;

&lt;p&gt;Nice, its faster then the imperative version. The problem with this code is that it does not state its intent at all. When i folded over the array version it took a second and i knew what the code was supposed to do. The same thing can’t be said for the imperative or the recursive version. It seems you have to sacrifice a lot of performance for readability sometimes. I´m not really looking forward to rewriting all my tight loops into these recursive monsters… &lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:714b61da-7a32-42f9-9993-bc91935447f1" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/.Net" rel="tag"&gt;.Net&lt;/a&gt;,&lt;a href="http://technorati.com/tags/F%23" rel="tag"&gt;F#&lt;/a&gt;,&lt;a href="http://technorati.com/tags/FSharp" rel="tag"&gt;FSharp&lt;/a&gt;,&lt;a href="http://technorati.com/tags/List" rel="tag"&gt;List&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Array" rel="tag"&gt;Array&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Sequence" rel="tag"&gt;Sequence&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Loops" rel="tag"&gt;Loops&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Performance" rel="tag"&gt;Performance&lt;/a&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/Domagala/aggbug/133000.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Domagala</dc:creator>
            <guid>http://geekswithblogs.net/Domagala/archive/2009/06/23/performance-of-lists-sequences-and-arrays-as-loopcounters.aspx</guid>
            <pubDate>Tue, 23 Jun 2009 13:05:10 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Domagala/comments/133000.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Domagala/archive/2009/06/23/performance-of-lists-sequences-and-arrays-as-loopcounters.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Domagala/comments/commentRss/133000.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Domagala/services/trackbacks/133000.aspx</trackback:ping>
        </item>
        <item>
            <title>Connect Four in F# (Part 2)</title>
            <link>http://geekswithblogs.net/Domagala/archive/2009/06/19/connect-four-in-f-part-2.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://geekswithblogs.net/Domagala/archive/2009/06/17/connect-four-in-f-part-1.aspx" target="_blank"&gt;Last time&lt;/a&gt; we saw how to implement the basic miniMax algorithm, this time we will continue by designing game board representation.&lt;/p&gt;  &lt;p&gt;Basically I want to start out with the “simplest thing that might possibly work” and optimize from there. The simplest thing to use for this in F# is a 2 dimensional list. The problem is that we get some really bad performance for random access into them, because unlike C# Lists, called ResizeArray in F#, they really are represented by lists internally. To be precise they are actually just this:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;list&amp;lt;'T&amp;gt; =
  | ( [] )
  | ( :: ) &lt;span style="color: blue"&gt;of &lt;/span&gt;'T * 'T list&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Ok so what do we choose instead of lists? We could take Array2D which is just what we need, but we would loose all the nice functional methods we have on other F# collections. So we will have to stick with an array of arrays I guess. So what put into those lists? In C# i would probably have started with something Byte, but F# has the nice concept of &lt;a href="http://en.wikibooks.org/wiki/F_Sharp_Programming/Discriminated_Unions" target="_blank"&gt;discriminated unions&lt;/a&gt; which are basically types which can be in one of the given states. Here is what we will be using for now:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;field = Yellow | Red | Empty&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Usually we could just stick with our nice discriminated unions, but again we have to think at least a little about performance. Discriminated unions are represented by classes that hold integers for every state the union can be in, which would mean we had references which are huge compared to a small byte. Well I guess we cant always get the the easiest thing possible, so we will go with bytes this time. –1y for one player, 0y for the other and 1y for the second player.&lt;/p&gt;

&lt;p&gt;Now we need some way to initialize a 2D list to be filled with 0y, to get our starting game board. In an imperative language i might have used something like to nested loops to fill in the lists. There is nothing wrong with this approach but its not really functional and kind of discouraged in the F# world. The closest you might see would be something like:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;createEmptyBoard rows cols =
    [&lt;span style="color: blue"&gt;for &lt;/span&gt;i &lt;span style="color: blue"&gt;in &lt;/span&gt;0..(rows-1) &lt;span style="color: blue"&gt;do
        for &lt;/span&gt;y &lt;span style="color: blue"&gt;in &lt;/span&gt;0..(cols-1) &lt;span style="color: blue"&gt;-&amp;gt; 0y&lt;/span&gt;]&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is using something called &lt;a href="http://en.wikibooks.org/wiki/F_Sharp_Programming/Sequences" target="_blank"&gt;sequence expressions&lt;/a&gt; which is really useful for creating all kinds of sequences, lists and so on. There is an easier way though:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static member &lt;/span&gt;EmptyPosition cols rows =
    Position((Array.create 7 (Array.create 6 0y)),1y)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now that we can create an empty board lets think about how we would drop in a disc to create a new board. Notice that we do not want to change the old board, but to create a new one. We already know what our function definition should look like so lets work from there:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;toggle player=
    player * -1y&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;member &lt;/span&gt;p.CanDropIn x =
    &lt;span style="color: blue"&gt;if &lt;/span&gt;board.[x] |&amp;gt; Array.tryFindIndex (&lt;span style="color: blue"&gt;fun &lt;/span&gt;y &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;y = 0y) = None &lt;span style="color: blue"&gt;then false
    else true
member &lt;/span&gt;p.DropIn x =
    &lt;span style="color: blue"&gt;if &lt;/span&gt;not (p.CanDropIn x) &lt;span style="color: blue"&gt;then &lt;/span&gt;failwith &lt;span style="color: maroon"&gt;"you tryed to put into a full slot"
    &lt;/span&gt;&lt;span style="color: blue"&gt;else
        let &lt;/span&gt;idx = board.[x] |&amp;gt; Array.findIndex (&lt;span style="color: blue"&gt;fun &lt;/span&gt;y &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;y = 0y)
        &lt;span style="color: blue"&gt;let &lt;/span&gt;updatedRow = board.[x] |&amp;gt; Array.mapi ( &lt;span style="color: blue"&gt;fun &lt;/span&gt;y field &lt;span style="color: blue"&gt;-&amp;gt; if &lt;/span&gt;y = idx &lt;span style="color: blue"&gt;then &lt;/span&gt;player &lt;span style="color: blue"&gt;else &lt;/span&gt;field)
        &lt;span style="color: blue"&gt;let &lt;/span&gt;newFields = board |&amp;gt; Array.mapi (&lt;span style="color: blue"&gt;fun &lt;/span&gt;y row &lt;span style="color: blue"&gt;-&amp;gt; if &lt;/span&gt;y = x &lt;span style="color: blue"&gt;then &lt;/span&gt;updatedRow &lt;span style="color: blue"&gt;else &lt;/span&gt;row)
        Position (newFields, (toggle player))&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Firs we check if its possible to dropIn at that position we create the new board by mapping the Empty slot we found with the color we were provided. We already have most of the things we need to finish our board representation, which will make writing the AI really easy.&lt;/p&gt;

&lt;p&gt;Next time we will encapsulate all this into a class so that it is easier to use and I will show you how we can calculate a reasonable score for a board position. I´m actually still not sure if i should build a class or put all of this into a module. Some input would be very welcome.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:27e80f33-1b67-4865-a854-16229416f450" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/.Net" rel="tag"&gt;.Net&lt;/a&gt;,&lt;a href="http://technorati.com/tags/F%23" rel="tag"&gt;F#&lt;/a&gt;,&lt;a href="http://technorati.com/tags/FSharp" rel="tag"&gt;FSharp&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Connect+Four" rel="tag"&gt;Connect Four&lt;/a&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/Domagala/aggbug/132913.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Domagala</dc:creator>
            <guid>http://geekswithblogs.net/Domagala/archive/2009/06/19/connect-four-in-f-part-2.aspx</guid>
            <pubDate>Fri, 19 Jun 2009 02:21:57 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Domagala/comments/132913.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Domagala/archive/2009/06/19/connect-four-in-f-part-2.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Domagala/comments/commentRss/132913.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Domagala/services/trackbacks/132913.aspx</trackback:ping>
        </item>
        <item>
            <title>Integer overflow and why making your type smaller might be better&amp;hellip;</title>
            <link>http://geekswithblogs.net/Domagala/archive/2009/06/18/integer-overflow-and-why-making-your-type-smaller-might-be.aspx</link>
            <description>&lt;p&gt;Ok, so do you have any idea what happens when you evaluate this?&lt;/p&gt;
&lt;pre class="code"&gt;printfn &lt;span style="color: maroon;"&gt;"%d" &lt;/span&gt;-Int32.MinValue;;&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Well I would have guessed I´d get Int32.MaxValue, but well I was wrong. Actually it overflows again and prints Int32.MinValue. Interesting behavior that drove me mad while i was trying to implement Alpha-Beta pruning. &lt;/p&gt;
&lt;p&gt;There is a simple reason this happens: “The &lt;strong&gt;Int32&lt;/strong&gt; value type represents signed integers with values ranging from negative 2,147,483,648 through positive 2,147,483,647.” – MSDN&lt;/p&gt;
&lt;p&gt;Ok so there is no positive version of Int32.MinValue and we Overflow. The C# compiler wont even compile code that has –Int32.MinValue anywhere. I guess its smarter then me, because i would have been fine with an Int32 that is actually intuitive to use and has one less number;). Ah well I guess I am just to used to using non-overflowing Ruby numbers… &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:80fb6adf-97cc-42cb-b6c2-848387621d4b" style="margin: 0px; padding: 0px; display: inline; float: none;"&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/.Net"&gt;.Net&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Int32"&gt;Int32&lt;/a&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/Domagala/aggbug/132904.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Domagala</dc:creator>
            <guid>http://geekswithblogs.net/Domagala/archive/2009/06/18/integer-overflow-and-why-making-your-type-smaller-might-be.aspx</guid>
            <pubDate>Thu, 18 Jun 2009 18:47:58 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Domagala/comments/132904.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Domagala/archive/2009/06/18/integer-overflow-and-why-making-your-type-smaller-might-be.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Domagala/comments/commentRss/132904.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Domagala/services/trackbacks/132904.aspx</trackback:ping>
        </item>
        <item>
            <title>Connect Four in F# (Part 1)</title>
            <link>http://geekswithblogs.net/Domagala/archive/2009/06/17/connect-four-in-f-part-1.aspx</link>
            <description>&lt;p&gt;Lately there has been a lot of buzz about functional programming, mostly because it is supposed to be the cure to all of our concurrency troubles. The answer to this from MS has been to productize F#, a functional, object oriented language, running on .Net . There have been a lot of great blog posts and articles about F# in the internet, so if you are looking for a basic introduction this is not really the place.&lt;/p&gt;
&lt;p&gt;This series is supposed to fill in the whole i saw while searching for F# content. It seems to me that there are no real step by step guides to building anything bigger then a little script, so I will be trying to build a &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Connect_Four"&gt;Connect Four&lt;/a&gt; game using F#. I will be concentrating on the AI part of the program but might implement some kind of GUI too if I feel like it any some is reading this:)&lt;/p&gt;
&lt;p&gt;I will start with the basic implementation of the AI, because its not that hard to do in F#. Basically we will be using the &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Minimax#Minimax_algorithm_with_alternate_moves"&gt;MiniMax&lt;/a&gt; algorithm and we will be making it better over time. So here is mine:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;let rec &lt;/span&gt;minMax (position:Position) depth player=&lt;br /&gt;    &lt;span style="color: blue;"&gt;let &lt;/span&gt;heuristic = (runHeuristic position) * player&lt;br /&gt;    &lt;span style="color: blue;"&gt;if &lt;/span&gt;depth = 0 || heuristic = Int32.MaxValue || heuristic = Int32.MinValue &lt;span style="color: blue;"&gt;then  &lt;/span&gt;heuristic&lt;br /&gt;    &lt;span style="color: blue;"&gt;else&lt;br /&gt;        let &lt;/span&gt;newPositions = position.NewPositions&lt;br /&gt;        &lt;span style="color: blue;"&gt;if &lt;/span&gt;newPositions |&amp;gt; Seq.length = 0 &lt;span style="color: blue;"&gt;then &lt;/span&gt;heuristic&lt;br /&gt;        &lt;span style="color: blue;"&gt;else &lt;br /&gt;            &lt;/span&gt;newPositions |&amp;gt; Seq.map (&lt;span style="color: blue;"&gt;fun &lt;/span&gt;pos &lt;span style="color: blue;"&gt;-&amp;gt; &lt;/span&gt;-(minMax pos (depth-1) -player)) |&amp;gt; Seq.max&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;let &lt;/span&gt;miniMax (position:Position) depth =&lt;br /&gt;    &lt;span style="color: blue;"&gt;let &lt;/span&gt;positions = position.Moves&lt;br /&gt;    &lt;span style="color: blue;"&gt;let &lt;/span&gt;result = positions |&amp;gt; Seq.map (&lt;span style="color: blue;"&gt;fun &lt;/span&gt;pos &lt;span style="color: blue;"&gt;-&amp;gt; -&lt;/span&gt;(minMax (position.DropIn pos) (depth-1) -1), &lt;br /&gt;                                                            pos) &lt;br /&gt;                           |&amp;gt; Seq.maxBy fst &lt;br /&gt;                           |&amp;gt; snd&lt;br /&gt;    result&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The miniMax function takes a position, which is just a representation of the game board, and the depth it should search to. It asks the position for all possible moves from here and then calls minMax with the moves applied, returning the move with the highest score. MinMax on the other hand recurs until either the maximum depth is reached or the game is won. It switches players on every recursion so that we do not have to write two nearly identical functions.&lt;/p&gt;
&lt;p&gt;What is really nice here is that the F# code is very close to the Pseudo Code used to describe the algorithm. If we checked for a full board in the runHeuristic function we might even be shorter. I will show you the heuristic function next time, but it does not do much more then check if someone has won the game and if not it gives a basic score for the board. We will use the score to our advantage when we switch to a better miniMax implementation.&lt;/p&gt;
&lt;p&gt;If you have any comments, they very welcome:) &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:8873caa4-1401-4a5a-82da-571c1e87e975" style="margin: 0px; padding: 0px; display: inline; float: none;"&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/.Net"&gt;.Net&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/F%23"&gt;F#&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Fsharp"&gt;Fsharp&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/MiniMax"&gt;MiniMax&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Game+Programming"&gt;Game Programming&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Lukas+Domagala"&gt;Lukas Domagala&lt;/a&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/Domagala/aggbug/132868.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Domagala</dc:creator>
            <guid>http://geekswithblogs.net/Domagala/archive/2009/06/17/connect-four-in-f-part-1.aspx</guid>
            <pubDate>Wed, 17 Jun 2009 04:07:51 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Domagala/comments/132868.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Domagala/archive/2009/06/17/connect-four-in-f-part-1.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Domagala/comments/commentRss/132868.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Domagala/services/trackbacks/132868.aspx</trackback:ping>
        </item>
        <item>
            <title>What is it all about?</title>
            <link>http://geekswithblogs.net/Domagala/archive/2009/06/16/what-is-it-all-about.aspx</link>
            <description>&lt;p&gt;Coding would be the right answer I guess, but more about that later.    &lt;br /&gt;I will be starting out this blog with a totally useless post just to try out the GWB blogging software, so if you are reading this spare yourself the pain...     &lt;br /&gt;Other then that I will soon start with a series on F# programming and hope to continue bringing in some .Net content.     &lt;br /&gt;    &lt;br /&gt;Cheers to you all and please don't flame a new blogger to much;)&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:18b4c5ae-da29-4b9c-a9b7-2c50d289f989" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/.Net" rel="tag"&gt;.Net&lt;/a&gt;,&lt;a href="http://technorati.com/tags/F%23" rel="tag"&gt;F#&lt;/a&gt;,&lt;a href="http://technorati.com/tags/FSharp" rel="tag"&gt;FSharp&lt;/a&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/Domagala/aggbug/132856.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Domagala</dc:creator>
            <guid>http://geekswithblogs.net/Domagala/archive/2009/06/16/what-is-it-all-about.aspx</guid>
            <pubDate>Tue, 16 Jun 2009 18:33:46 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Domagala/comments/132856.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Domagala/archive/2009/06/16/what-is-it-all-about.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Domagala/comments/commentRss/132856.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Domagala/services/trackbacks/132856.aspx</trackback:ping>
        </item>
    </channel>
</rss>
