Geeks With Blogs

News
.Net
FSharp Pattern Matching gotcha and Active Patterns
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: let rec heuristic (positions: (int * int) list) (pos: int*int) = match positions with | [] -> 0 | position::_ -> 1 + (heuristic (List.tl positions) pos) | _ -> heuristic (List.tl positions) pos Looking at it, it seems the code should tell me how often value is in position, right? Well it doesn’t! And there is ......

Posted On Thursday, June 25, 2009 10:18 PM

Performance of Lists, Sequences and Arrays as loopcounters
I ran into some rather interesting numbers while trying to optimize my Connect Four implementation. Try to guess what this code will print out: let test= let stop1 = Stopwatch.StartNew() let list = [1..1000000] let bla = list |> List.fold (fun state x -> state + x) 0 stop1.Stop() let stop2 = Stopwatch.StartNew() let seq = seq{1..1000000} |> Seq.fold (fun state x -> state + x) 0 stop2.Stop() let stop3 = Stopwatch.StartNew() let arr = [|1..1000000|] let arr1 = arr|> Array.fold (fun state ......

Posted On Tuesday, June 23, 2009 3:05 PM

Connect Four in F# (Part 2)
Last time we saw how to implement the basic miniMax algorithm, this time we will continue by designing game board representation. 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 ......

Posted On Friday, June 19, 2009 4:21 AM

Integer overflow and why making your type smaller might be better…
Ok, so do you have any idea what happens when you evaluate this? printfn "%d" -Int32.MinValue;; 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. There is a simple reason this happens: “The Int32 value type represents signed integers with values ranging from negative 2,147,483,648 through positive 2,147,483,647.” – MSDN Ok so there ......

Posted On Thursday, June 18, 2009 8:47 PM

Connect Four in F# (Part 1)
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. This series is supposed to fill in the whole i saw while searching for F# content. It ......

Posted On Wednesday, June 17, 2009 6:07 AM