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 8054269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:06:36+00:00 2026-06-05T08:06:36+00:00

Reading Beginning F# – Robert Pickering I focused on the following paragraph: Programmers coming

  • 0

Reading Beginning F# – Robert Pickering I focused on the following paragraph:

Programmers coming from an OCaml background should be careful when
using exceptions in F#. Because of the architecture of the CLR,
throwing an exception is pretty expensive—quite a bit more expensive
than in OCaml. If you throw a lot of exceptions, profile your code
carefully to decide whether the performance costs are worth it. If the
costs are too high, revise the code appropriately.

Why, because of CLR, throwing an exception is more expensive if F# than in OCaml? And what is the best way to revise the code appropriately in this case?

  • 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-06-05T08:06:37+00:00Added an answer on June 5, 2026 at 8:06 am

    Reed already explained why .NET exceptions behave differently than OCaml exceptions. In general, .NET exceptions are suitable only for exceptional situations and are designed for that purpose. OCaml has more lightweight model and so they are used to also implement some control flow patterns.

    To give a concrete example, in OCaml you could use exception to implement breaking of a loop. For example, say you have a function test that tests whether a number is a number we want. The following iterates over numbers from 1 to 100 and returns first matching number:

    // Simple exception used to return the result
    exception Returned of int
    
    try
      // Iterate over numbers and throw if we find matching number
      for n in 0 .. 100 do
        printfn "Testing: %d" n
        if test n then raise (Returned n)
      -1                 // Return -1 if not found
    with Returned r -> r // Return the result here
    

    To implement this without exceptions, you have two options. You could write a recursive function that has the same behaviour – if you call find 0 (and it is compiled to essentially the same IL code as using return n inside for loop in C#):

    let rec find n = 
      printfn "Testing: %d" n
      if n > 100 then -1  // Return -1 if not found
      elif test n then n  // Return the first found result 
      else find (n + 1)   // Continue iterating
    

    The encoding using recursive functions can be a bit lengthly, but you can also use standard functions provided by the F# library. This is often the best way to rewrite code that would use OCaml exceptions for control flow. In this case, you can write:

    // Find the first value matching the 'test' predicate
    let res = seq { 0 .. 100 } |> Seq.tryFind test
    // This returns an option type which is 'None' if the value 
    // was not found and 'Some(x)' if the value was found.
    // You can use pattern matching to return '-1' in the default case:
    match res with
    | None -> -1
    | Some n -> n
    

    If you’re not familiar with option types, then take a look at some introductory material. F# wikibook has a good tutorial and MSDN documentation has useful examples too.

    Using an appropriate function from the Seq module often makes the code a lot shorter, so it is preferrable. It may be slightly less efficient than using recursion directly, but in most of the cases, you don’t need to worry about that.

    EDIT: I was curious about the actual performance. The version using Seq.tryFind is more efficient if the input is lazily generated sequence seq { 1 .. 100 } instead of a list [ 1 .. 100 ] (because of the costs of list allocation). With these changes, and test function that returns 25th element, the time needed to run the code 100000 times on my machine is:

    exceptions   2.400sec  
    recursion    0.013sec  
    Seq.tryFind  0.240sec
    

    This is extremely trivial sample, so I think the solution using Seq will not generally run 10 times slower than equivalent code written using recursion. The slowdown is probably due to the allocation of additional data structures (object representing the sequence, closures, …) and also due to additional indirection (the code needs numerous virtual method calls, instead of just plain numeric operations and jumps). However, exceptions are even more costly and don’t make the code shorter or more readable in any way…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently reading Beginning Rails 3. I'm coming from PHP and trying to learn
I was reading Apress Beginning Spring 2 From Novice to Professional , and I
I am reading this book Beginning Java™ EE 6 Platform with GlassFish™ 3: From
I am currently reading Beginning CakePHP:From Novice to Professional by David Golding. At one
I am reading Beginning C# to refresh my memory on C# (background in C++).
I'm new to Java but not to programming. I'm reading the book Beginning Android
I'm reading The C Programming Language (2nd ed.) and near the beginning, it has
Reading some posts from Jimmy Boggard and wondering - how exactly is it possible
I'm reading the Beginning CouchDB book by Apress and there is a line that
I've been reading three books (iPhone Game Development, iPhone cookbook and beginning iPhone development),

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.