33

F# comes out of the box with an interactive REPL. C# has nothing of the sort and is in fact kinda difficult to play around without setting up a full project (though LINQpad works and its also possible to do via powershell).

Is there something fundamentally different about the languages that allows F# to have the interactive console but makes it difficult to implement it for C#?


Since many years later people are still coming to this question I should note that now there are many options. You can use powershell (pre-installed on every modern windows machine) to play with the .Net framework. Or you can use LINQpad to prototype arbitrary c# code. Or you can use ScriptCs or you can use an online jsfiddle-type environment like Complify.net or Jsil. Lots of options.

Robert Harvey
  • 200,592
George Mauer
  • 2,012
  • 2
  • 16
  • 18

4 Answers4

58

Is there something fundamentally different about the languages that allows F# to have the interactive console but makes it difficult to implement it for C#?

Yes.

F# is a descendant of the ML programming language, which in turn was heavily influenced by languages like Lisp and Scheme. Those languages were designed from day one to have three nice properties.

First, those languages do not really have statements the way you think of them in C#. Rather, almost everything is an expression that has a value, so an evaluate-and-then-print-the-value mechanism makes sense in almost every situation.

Second, those languages discourage programming with side effects, so you can make evaluations without worrying that you’re going to be messing up global state.

Third, most of the work you do in those languages is “at the top level”; there is typically no enclosing “class” or “namespace” or other context.

By contrast, C# emphasizes programming control flow with statements that produce side effects, and those statements are always in multiple nested containers -- a namespace, a class, a method, and so on.

So these are all things that make it harder for C# to have a REPL, but certainly not impossible. We’d just need to figure out what the semantics are for statements and expressions that appear outside of the usual context, and what the semantics are of mutations that change name bindings, and so on.

Why does F# have an interactive mode but not C#?

Because the F# team decided that having a REPL loop was a priority-one scenario for them. The C# team historically has not. Features do not get implemented unless they are the highest priority features that fit into the budget; until now, a C# REPL has not been at the top of our list.

The Roslyn project has a C# REPL (and will eventually have a VB REPL as well, but it is not ready yet.) You can download a preview release of it to see how you like it at

http://www.microsoft.com/en-us/download/details.aspx?id=27746

Robert Harvey
  • 200,592
Eric Lippert
  • 46,558
22

Mono has a C# repl: http://www.mono-project.com/CsharpRepl

It even has a GUI version that allows you to directly manipulate graphics objects, or create Gtk# widgets:

enter image description here

sehe
  • 180
Lee
  • 927
3

I believe it's mostly a historic thing. REPL environments have always been associated with functional languages, ML family languages included, and F# stays true to that tradition. Keep in mind that interactive environment is something that users coming from functional background take for granted, lack of such a feature would be put VS and - by extension - F#, at a disadvantage.

On the other hand, no such feature has been commonplace in the OOP community.

There are however REPLs available for many non-functional languages, including C, Java or C#. Also while it's a far cry from a full-fledged REPL, the Autos feature in VS shows that it's certainly doable with C#.

scrwtp
  • 4,542
1

I believe C# is primarily object-oriented. To write even simplest code, you should divide it into multiple classes. To use REPL, you would need to write lots of code.

F# being primarily functional doesn't have this problem and you can easily write even complex code in straightforward fasshion and convert it into object/s later.

It is easier to write a single line of function than to write class, that spans many lines.

Euphoric
  • 38,149