F# Tutorial on F# Program Structure

f# is a functional programming language.

in f#, functions work like data types. you can declare and use a function in the same way like any other variable.

in general, an f# application does not have any specific entry point. the compiler executes all top-level statements in the file from top to bottom.

however, to follow procedural programming style, many applications keep a single top level statement that calls the main loop.

the following code shows a simple f# program −

open system
(* this is a multi-line comment *)
// this is a single-line comment

let sign num =
   if num > 0 then "positive"
   elif num < 0 then "negative"
   else "zero"

let main() =
   console.writeline("sign 5: {0}", (sign 5))

main()

when you compile and execute the program, it yields the following output −

sign 5: positive

please note that −

  • an f# code file might begin with a number of open statements that is used to import namespaces.

  • the body of the files includes other functions that implement the business logic of the application.

  • the main loop contains the top executable statements.