Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3600044
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:26:41+00:00 2026-05-18T20:26:41+00:00

I wrote a quick and dirty function to compare file contents (BTW, I have

  • 0

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#.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:26:42+00:00Added an answer on May 18, 2026 at 8:26 pm

    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 simple seq<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 🙂

    open System.IO
    
    /// Represents a sequence of values 'T where items 
    /// are generated asynchronously on-demand
    type AsyncSeq<'T> = Async<AsyncSeqInner<'T>> 
    and AsyncSeqInner<'T> =
      | Ended
      | Item of 'T * AsyncSeq<'T>
    
    /// Read file 'fn' in blocks of size 'size'
    /// (returns on-demand asynchronous sequence)
    let readInBlocks fn size = async {
      let stream = File.OpenRead(fn)
      let buffer = Array.zeroCreate size
    
      /// Returns next block as 'Item' of async seq
      let rec nextBlock() = async {
        let! count = stream.AsyncRead(buffer, 0, size)
        if count > 0 then return Ended
        else 
          // Create buffer with the right size
          let res = 
            if count = size then buffer
            else buffer |> Seq.take count |> Array.ofSeq
          return Item(res, nextBlock()) }
    
      return! nextBlock() }
    

    The asynchronous workflow to do the comparison is then quite simple:

    let rec compareBlocks seq1 seq2 = async {
      let! item1 = seq1
      let! item2 = seq1
      match item1, item2 with 
      | Item(b1, ns1), Item(b2, ns2) when b1 <> b2 -> return false
      | Item(b1, ns1), Item(b2, ns2) -> return! compareBlocks ns1 ns2
      | Ended, Ended -> return true
      | _ -> return failwith "Size doesn't match" }
    
    let s1 = readInBlocks "f1" 1000
    let s2 = readInBlocks "f2" 1000
    compareBlocks s1 s2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a quick and dirty function to compare file contents (BTW, I have
I wrote a quick and dirty logger as a jQuery plugin... (function($){ $.log =
i wrote a quick little application that takes a base file of code with
I wrote a quick and dirty wrapper around svn.exe to retrieve some content and
I recently wrote a quick-and-dirty proof-of-concept proxy server in C# as part of an
Just a quick question if someone has already that kind of setup or if
I wrote a quick console application that uses SharePoint dll files. If I attempt
I recently wrote a quick VB.NET app that injects a DLL into a running
I wrote a quick program in python to add a gtk GUI to a
Hey guys i wrote a quick test. I want delete to call deleteMe which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.