Is it possible to force F# to behave like a pure functional language like Haskell? May be using some compiler directives?
ps: since i come from a C/C++ background, I want to force myself to learn functional programming without learning haskell :)
Is it possible to force F# to behave like a pure functional language like Haskell? May be using some compiler directives?
ps: since i come from a C/C++ background, I want to force myself to learn functional programming without learning haskell :)
No. Currently F# compiler cannot do this checking.
Why not just start writing some F# program to learn functional programming? I know a few people who learned ML/Ocaml/F# first and then moved to Haskell.
Then you will have a better understanding of purity. (If you really haven't touched any functional programming language before, your understanding of pure functional could be superficial. )
If you're anything like me, you will probably avoid the 'good stuff' if you don't force yourself to use Haskell instead of F# and use Haskell as idiomatically as you can. Using algebraic data types instead of objects, learning to love laziness, embracing the monad, and more are much more of a core part of Haskell, and arguably some of the finer points (in my opinion) of pure functional programming.
F# doesn't have as steep of a learning curve in many ways, but you sound like you are learning it for fun, so why not challenge yourself anyways? I can attest that moving to F# after using Haskell can give you a much better feel for how F# in general ought to be used anyways.
Food for thought.
The purely functional aspects of Haskell are fundamental to the language. You can't just transplant it between languages. It leads to major design decisions — for instance, Haskell's purely functional nature led to the invention of the IO monad. F# does not have such an "escape-valve" for stateful computation.
Also, learning to program in a language that supports functional programming but doesn't absolutely enforce it might actually be instructive. Many people get confused between Haskell's individual design decisions (e.g. the IO monad, to hit the big example again) and how functional programming works in general.
In short: No, you can't this. But what you can do is watch very carefully and question everything you do that involves maintaining state and sequencing operations to make sure that there isn't a more pure abstraction you're missing.
You can't force this behavior in F#, but as Brian said discipline is your best friend. E.g. don't use mutable
, for
or while
loops, ref
keywords, etc. Also stick with purely immutable data structures (discriminated union, list, tuple, map, etc). If you need to do IO at some point, architect your program so that they are separated from your purely functional code.
Don't forget functional programming is all about limiting and isolating side-effects.
There is no such thing as "pure". All languages have to deal with the real world and this is where the so called "purity" begins and ends. F# is as pure as Haskell in every way, even if you use imperative means to get the IO, which basically what Haskell does anyway.
What F# gives you, is the option to use declarative practises much more easily than say its cousin C#. This is its niche in the market. Great for financial and scientific programming.
I think that reading around the subject as opposed to the implementations would give you a heads up of what people are talking about when dealing with purity. Its only pure in respect to "side effect free" programming.
What cracks me up though is that keyboards, mice, monitors, computer hardware all depend on these evil evil things called side effects to be useful!