Qwertie
Qwertie

Reputation: 17186

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"

Upvotes: 4

Views: 1339

Answers (7)

Stu1986C
Stu1986C

Reputation: 1510

http://www.samskivert.com/code/fsharp/fsharp-cheat-sheet.pdf

It's a pretty comprehensive cheat sheet that has helped me out a lot with learning F#

Upvotes: 1

Rich McCollister
Rich McCollister

Reputation: 531

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.

Upvotes: 2

stringer
stringer

Reputation:

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

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

Upvotes: 0

Dustin Campbell
Dustin Campbell

Reputation: 9855

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.

Upvotes: 4

MichaelGG
MichaelGG

Reputation: 10006

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.

Upvotes: 3

Brian
Brian

Reputation: 118865

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

Upvotes: 6

JaredPar
JaredPar

Reputation: 754745

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

Upvotes: 2

Related Questions