A pure function is one that will compute the same value for the same inputs each time (so it can't depend on an externally mutable value or IO), and it performs no externally visible changes (has no side effects). Side effects are any changes visible outside the function. That might be due to a call to another method that's not pure, or directly or indirectly changing a mutable value in F#. It's unrelated to any difference between F# and other .NET code.

Neither F# nor .NET have any specific support for pure functions (other than some metadata attributes via state your function is pure, for the sake of static analysis tools). Purity is left up to the programmer, as in most other languages. F# makes this easier to achieve via the style it promotes (such as immutability and recursion).

In many scenarios and in many languages, it's a good idea to try to keep functions as pure as possible. This makes your code easier to test and reason about.

By on 12/7/2009 8:59 AM ()

Hi Michael,

Thank you for the notes on F# purity and safety. I've also read another interpretation of purity in functional programming: limiting the code to the use of functions exclusively. That seems an even more of an ideal - and less attainable goal.

What are the meta-data attributes you mentioned? Do you have a link for more info (maybe some sample code that includes them)?

Just picked up a book on functional programming (using Haskel), to get a better feel for the pattern/style advocated. Having someone voice F#'s implementation helps make that it clearer.

Thanks again,

BRN..

By on 12/8/2009 5:31 AM ()

The metadata is via Code Contracts. Code Contracts: [link:msdn.microsoft.com]

By on 12/8/2009 1:02 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper