I wrote a quick and dirty function to compare file contents (BTW, I have already tested that they are of equal size):
let eqFiles f1 f2 =
let bytes1 = Seq.ofArray (File.ReadAllBytes f1)
let bytes2 = Seq.ofArray (File.ReadAllBytes f2)
let res = Seq.compareWith (fun x y -> (int x) - (int y)) bytes1 bytes2
res = 0
I’m not happy with reading the whole contents into an array. I’d rather have a lazy sequence of bytes, but I can’t find the right API in F#.
If you want to use the full power of F#, then you can also do it asynchronously. The idea is that you can asynchronously read a block of the specified size from both files and then compare the blocks (using standard & simple comparison of byte arrays).
This is actually an interesting problem, because you need to generate something like an asynchronous sequence (a sequence of
Async<T>values that is generated on demand, but without blocking threads as with simpleseq<T>or iteration). The function to read the data and declaration of async sequence could look like this:EDIT I also posted the snippet to http://fssnip.net/1k which has nicer F# formatting 🙂
The asynchronous workflow to do the comparison is then quite simple: