explorer
explorer

Reputation: 12090

Can F# teach me good functional programming practices?

I am planning to immerse myself in the functional programming paradigm and make the thought process second nature to me. The .Net/Mono is the only framework/objectmodel I am familiar with and I dont want to learn any other type system. That makes F# an obvious consideration since the burden of learning a new type system will not be a distracting factor.

But my concern is about the language itself. Will it be as good as any other classical functional programming language (e.g. Haskell) in drilling FP into my skull ?

Upvotes: 8

Views: 850

Answers (5)

David W.
David W.

Reputation: 271

Functional Programming paradigm? Haskell springs immediately to mind. By the way, there's no inherit framework in Haskell. As far as I know, your framework is a terminal window and a programming editor.

If you want a functional programming language that's in your .NET framework, you'll have to stick with F#.

IronPython, but the way, is not a functional programming language.

Upvotes: 2

CodexArcanum
CodexArcanum

Reputation: 4016

F#, in many regards, may be better than most "other classical functional programming language[s]". Since you already know .NET, you'll be able to fully leverage all your .NET knowledge while picking up two useful things from F#:

  1. The functional programming paradigm. That is, how to program in the style of FP. You can use that in any language. I often learn towards FP in my C# code, because I find it to be a much more maintainable and reusable way to write software.

  2. One language's functional programming syntax. F# is very similar to OCAML, and the ML family makes up one of the 3 or so major dialects of FP languages. The other two biggies are the Miranda family (of which Haskell is the major modern representative) and the LISP family (where Common Lisp and Scheme are the two big reps).

In F#, it might help you to make the distinction very clearly between F# types and .NET types. Records, Tuples, and Discriminated Unions are (pretty much) purely F# things, and they're used in almost all functional languages. Use these a lot if you want to learn FP. Classes, structs, and interfaces are also in there, but those are .NET concepts. Use them as needed, but try to avoid them in general, lest you end up writing "C# with a goofy syntax" instead of idiomatic F#.

The things you'll really miss out on from Haskell are true purity (eh, not that big of a deal), lazy evaluation by default (a big change), and type classes. You can do lazy eval in F#, but you have to use the explicit Lazy<> classes to get it. Having lazy eval everywhere is one of the really unique and powerful things in Haskell, but it's not a strict FP concept. Type Classes are very powerful, but you can get by without them for most real world situations. They're just a higher level of abstraction that cuts down on some boiler plate and repetition.

True purity is usually more academic. It has certain advantages, but if you're already used to .NET and impure techniques, you'll mostly just find it a pain to work around until you've fully bought into the FP mindset.

Upvotes: 3

Tomas Petricek
Tomas Petricek

Reputation: 243041

I think the benefit of learning functional programming with F# (especially if you already have experience with C# and .NET) is that you'll find it a lot easier to create some fun project.

  • One reason is that you're already familiar with .NET libraries (that are easy to access from F#)
  • Second reason is that F# is not pure, so you can start writing code that works and then improve it to make it more functional.

F# supports multiple paradigms, but it is mainly a functional language (compared to Python or C# that support multiple paradigms too, but aren't primarilly functional). This makes it easier to force yourself to use the functional style. It is easier to avoid imperative patterns, because they are more difficult to write in F# (and the code looks worse).

I think that once you "get" the basics functional programming, it will be easier to learn other functional languages if you'll still be interested. It is definitely nice to know a little bit about Haskell or Scheme, because they are more radical (in some ways).

If you're good at understadning concepts without actually using them in some real projects then starting with some classic language using some classic book may be a good option too (e.g. Haskell School of Expression or Structure and Interpretation of Computer Programs). I don't mean this in any negative sense - I personally quite like reading technical books without trying examples, because I can still take the interesting concepts from just reading (and for practice, there is always Google). Of course, you can write some code in Haskell or Scheme, but as a .NET programmer, you'll probably find writing F# code more fun...

Upvotes: 8

jason
jason

Reputation: 241621

Yes, it can. Especially helpful and germane here is Real World Functional Programming which comes at FP from both the C# and especially the F# perspective. You'll love it.

Upvotes: 10

Peter C
Peter C

Reputation: 6307

Yes, it will definitely help you learn functional programming. F# does support object-oriented programming too, so the switch should be easier. If you want to move to a full functional language eventually, you could, but I actually find the combination of OO and FP better than OO or FP alone (I'm a Scala programmer, which is like F#).

Upvotes: 4

Related Questions