Chapter 2. Lexical Structure

WHAT'S IN THIS CHAPTER?

  • Understanding basic syntax

  • Defining values and identifiers

In any language, some basic lexical ideas have to be laid down before programmers can begin to understand the concepts behind the language — questions such as "What makes a comment?" or "What is allowed in identifier names?," although intrinsically boring in many ways, have to be defined before any further progress into the language's structure and form.

F#'s lexical structure derives strongly from its immediate ancestor, OCaml, which is itself a derivative of the pure functional language ML. This means that for the most part, although F# strives to be .NET-friendly in terms of its syntax, the C# and Visual Basic developer can find a number of new and interesting syntactic ideas, some of which will be surprising. Fortunately, most of those will be pleasant surprises because much of the syntax is less restrictive than the other .NET languages offered from Microsoft.

COMMENTS

The easiest place to begin with any language is a simple definition of what makes a comment (meaning, syntax that the compiler will ignore during processing). F# supports three different styles of comments:

  • Multi-line comments using the (* and *) delimiters, such as:

    (* This is
    a multi-line
    comment *)

    Note that multi-line comments nest, meaning that the multi-line comment will only be terminated when the number of end-comment pairs match the number of begin-comment pairs preceding it.

  • Single-line comments using ...

Get Professional F# 2.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.