15

Is it possible to force F# to behave like a pure functional language like Haskell? Maybe 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 :)

abatishchev
  • 98,240
  • 88
  • 296
  • 433
r00kie
  • 843
  • 1
  • 11
  • 12
  • 8
    Why not just learn Haskell? Its syntax is almost a subset of F#'s, and with knowledge in Haskell F# will be a piece of cake. Possible side-effect: you will hate F#. – luqui Oct 06 '10 at 13:17

5 Answers5

14

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.

elmattic
  • 12,046
  • 5
  • 43
  • 79
  • 4
    "Don't forget functional programming is all about limiting and isolating side-effects." - or representing them, so they're no longer on the "side" :) – mokus Oct 06 '10 at 17:32
  • 1
    "Don't forget functional programming is all about limiting and isolating side-effects" - For me at least, it's also about being able to define types correctly such that invalid states become unrepresentable. You can do this in other languages (like C++) but the type system of F# and other functional languages makes this easier. – sashang Oct 07 '10 at 02:16
10

Nope, sorry. You just have to use discipline.

Brian
  • 117,631
  • 17
  • 236
  • 300
  • Can you even do it? You'd have to create monads for everything you'd ordinarily do that have side-effects (like I/O). – Gabe Oct 06 '10 at 05:00
  • 1
    @Gabe: There are other models for IO besides Haskell's monad system, though it is the best tradeoff between flexibility and simplicity (amazingly enough) that I've heard of. You might be interested in reading Simon Peyton Jones's "Awkward Squad" paper if you want to read some very in-depth reasoning on the subject. – Chuck Oct 06 '10 at 05:23
  • Chuck: Are you saying that F# has a purely functional I/O primitive that isn't a monad? Or that whatever you'd implement to do I/O wouldn't have to be a monad? – Gabe Oct 06 '10 at 06:05
  • 1
    F# has no purely functional I/O primitive -- it uses effectful functions and call-by-value. And yes, there are other ways to purely functional IO than monads. Check out the awkward squad paper. – luqui Oct 06 '10 at 13:15
4

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.

iand675
  • 1,118
  • 1
  • 11
  • 23
2

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.

Chuck
  • 234,037
  • 30
  • 302
  • 389
0

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

Yin Zhu
  • 16,980
  • 13
  • 75
  • 117