Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# - Should I learn with or without #light?

Tags:

f#

I'm in the process of learning F# and am enjoying it so far. Almost all of the examples online use the lightweight syntax (#light); however, also give a comment about it being on for said example in most cases.

Is it better to learn F# using #light enabled or disabled? I'm planning on eventually learning it w/o it turned on but am curious on if it would be better to learn it at the beginning or work on applying it after I know the core language more.

like image 592
JamesEggers Avatar asked Jan 20 '09 15:01

JamesEggers


4 Answers

I'd definitely prefer learning F# with the #light syntax. The non-light version is sometimes useful for understanding some tricks about the F# syntax, but the #light syntax gives you much pleasant experience.

For example - using #light

let add a b c = 
  let ab = a + b
  printfn "%d" ab
  c - ab

Using non-light you can write the same thing like this:

let add a b c = 
  let ab = a + b in // 'in' keyword specifies where the binding (value 'ab') is valid
  printfn "%d" ab;  // ';' is operator for sequencing expressions
  c - ab;;          // ';;' is end of a function declaration

This for example shows that you cannot write something like:

let doNothing a b = 
  let sum = a + b in 

There is an 'in' keyword at the end but the function doesn't have any body (because there is no expression following 'in'). In this case non-light syntax is sometimes interesting to understand what's going on... But as you can see, the #light code is a lot simpler.

like image 187
Tomas Petricek Avatar answered Nov 06 '22 06:11

Tomas Petricek


The "#light" will probably become the default in a future release of the language, so I would learn it that way. I think it's rare for anyone to use the heavier syntax except for OCaml-compatibility (either when cross-compiling, or because the human sitting at the keyboard knows OCaml and is making a smoother transition to F#).

like image 40
Brian Avatar answered Nov 06 '22 05:11

Brian


Because I learned F# from an OCaml book (and I use an OCaml mode for Emacs to edit F# code), I prefer to use the "heavy" syntax. I have worked with #light code, and of course most of the F# examples are written using the light syntax so having some general familiarity is useful. That said, it's quite a bit easier to switch from heavy to light than the other way around, so it's certainly not a bad idea to learn it using the heavy syntax.

I have come across the occasional annoying bug with heavy syntax being treated as a second class citizen (combine was broken for computation expressions a couple releases back), but these are pretty rare. Generally speaking, I don't think the differences are very significant and I need to look close to determine which syntax is being used when looking at code in isolation. YMMV.

like image 7
Derek Slager Avatar answered Nov 06 '22 04:11

Derek Slager


If I remember correctly, book "Expert C#" mentions that #light will be the default when F# ships and that non-light syntax is intended for compatibility only.

like image 1
bh213 Avatar answered Nov 06 '22 06:11

bh213