Cover by Chris Smith

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Anatomy of an F# Program

By now, you might want to learn how to take the F# code we have been writing in the FSI window and convert it into actual F# programs. But in reality, every code snippet you have seen so far has been a full program!

Most other languages, like C#, require an explicit program entry point, often called a main method. Yet our F# programs so far haven’t declared any special markup indicating where the program should begin. In F#, for single-file applications, the contents of the code file are executed from top to bottom in order (without the need for declaring a specific main method).

For multifile projects, however, code needs to be divided into organization units called modules or namespaces.

Modules

All the code we have written so far has been in a module. By default, F# puts all your code into an anonymous module with the same name as the code file with the first letter capitalized. So, if you have a value named value1, and your code is in file1.fs, you can refer to it by using the fully qualified path: File1.value1.

Creating modules

You can explicitly name your code’s module by using the module keyword at the top of a code file. After that point, every value, function, or type defined will belong to that module:

module Alpha

// To refer to this value outside the module
// use: Alpha.x
let x = 1

Nested modules

Files can contain nested modules as well. To declare a nested module, use the module keyword followed by the name of your module and an equals sign =. Nested ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required