tags:

views:

406

answers:

6
+2  Q: 

F# syntactic sugar

F# clearly contains a lot of things that are syntactic sugar, and as I try to learn it--without the aid of a book--I am overwhelmed by the sheer variety of syntax. Is there a simpler "core" language hidden behind all that syntactic sugar? Is there a cheat sheet list of the syntactic sugars and how they map to the core language?

And hey, is there a reason F# requires a different "assignment" operator for function definitions than for lambdas, or was it a random decision? e.g. "let inc x = x+1" vs "fun x -> x+1"

+2  A: 

Only one of the two expressions you mentioned uses an assignment operator.

This defines a function bound to the identifier "inc" which takes a single argument of type int and returns the argument +1.

let inc x = x + 1;

This on the other hand, is an expression which represents a lambda. It is bound to no identifier cannot exist solely by itself. It must be part of a larger expression.

fun x -> x + 1
JaredPar
I know what functions are and I know what a lambda is.
Qwertie
@Qwertie, then you should state your question in a clearer fashion
JaredPar
I think it's pretty clear I didn't ask WHAT a function or WHAT a lambda is. Rather it was a question about the syntax.
Qwertie
@Qwertie, i disagree. I find your last sentence ambigous and chose to give an answer for one interpretation.
JaredPar
Okay, sorry, it's just that I'm a newbie. I don't know the correct terminology. The assignment operators I was referring to are "=" in the case of "let" and "->" in the case of "fun".
Qwertie
@Qwertie, not an issue. I'm an F# newbie as well (hint trust whatever Brian says). I'm not sure if -> actually classifies as assignment in F# though because as assignment requires LHS identifier. A lambda by definition has no identifier. Brian would know for sure
JaredPar
+4  A: 

A great deal of F# syntax comes from OCaml - many OCaml programs will compile and run in F# with no changes.

Good starter links:

http://msdn.microsoft.com/en-us/fsharp/default.aspx

http://en.wikibooks.org/wiki/F_Sharp_Programming

http://lorgonblog.spaces.live.com/blog/cns!701679AD17B6D310!887.entry?_c=BlogPart

Brian
+3  A: 

F#, barring .NET interop, seems quite simpler than, say C#. (Not really picking on it in particular, but since it's another popular .NET language...)

Take F# functions, for instance:

  • There's no magic "void" type-thats-not-really-a-type
    • Hence everything is an expression
  • All functions are unary
  • Functions are first class type

Right there you have a function system that's vastly easier than, say, C#, where void creates a special case, and there's no generalization possible over all functions.

As to your specific question, with "let f x = x" versus "let f = fun x -> x", that's probably an inherited trait from ML. (I don't see any particular reason why it couldn't be "let f = fun x = x", except that perhaps it'd be more confusing and perhaps make the grammar more complex?. (Personally I'd prefer "let f = \x.x".)) Anyways, while in most cases they are equivalent, sometimes you must define a syntactic function instead of a function value.

.NET interop can, unfortunately, make things a bit more complicated, although probably not more or much more than other .NET languages.

MichaelGG
'no magic "void" type-thats-not-really-a-type' other than unit of course.
Richard
Yeah, just what I was thinking. Or Haskell's (). Although I think it was ill-advised that C#/C/C++/Java doesn't have "define function" and "define variable" keywords, I don't think C# syntax is nearly as bad as C++, the language it was largely based on.
Qwertie
Unit has a value, (). No magic there:> id ();;val it : unit = ()How do you pass a value of void? How can you, say, in C#, declare a delegate that encompasses any function? You can't. You're stuck with separate codepaths for void (Action) versus nonvoid (Func).
MichaelGG
"any function"? I don't know of a statically typed language that lets you call "any function" without regard for its argument types. And if you can't convert Func<int,int> to Action<int> for example, that's a limitation of the .NET Framework itself (bad variance support) and not of the C# language.
Qwertie
Vote for covariant return types at https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=90909 ...
Qwertie
I didn't say call, I said generalize. You can't do Func<void, int> in C#, so you often end up having a split set of functions to do any higher order function work.
MichaelGG
+3  A: 

Along with Brian's links, I'd like to pass along links to my own blog series, Why I Love F#, which covers some of the basics.

With regard to your second question, "let inc x = x+1" vs "fun x -> x+1" are two ways of expressing a function, but only the first one uses the assignment operator. In fact,

let inc x = x + 1

Is equivalent to:

let inc = (fun x -> x + 1)

The first is shorthand for the second, but the second might illuminate how all functions in F# are really lambdas bound to names.

Dustin Campbell
A: 

Notice that you can apply directly a lambda in an expression.

let i = (fun x -> x + 1)0  //i equal 1
A: 

If you are looking for a cheat sheet for F#, this is the one that I use on a regular basis.

fsharpcheatsheet.pdf

It doesn't cover everything, things like Units of Measure or quotations, and doesn't talk about the various libraries, but it does cover a lot of the syntax.

Rich McCollister