Cartesian Product in F# May, 2010
Here is a simple function to create Cartesian product sequence of tuples from x & y ranges. Nice for iterating 2d space:
module Seq = let public cartesianProduct xRange yRange = seq {for y in yRange do for x in xRange do yield (x,y) } open System Seq.cartesianProduct {0..3} {2..3} |> Seq.iter (fun (x,y) -> Console.WriteLine("({0}, {1})", x, y)) Console.ReadKey() |> ignore
As an aside, looks like you cannot create type extensions for modules (Which is what I wanted to do for this function). Yeah, I know, it's "type" extensions not "module" extensions. The thing is though that F# modules compile down to public static classes; and you *can* create a type extension for System.Linq.Enumerable, which *is* a public static class. Doesn't seem consistent.