Function
In this post I present the first element, which allows us to model functional programs created in the language F#. It is the loose function. In the next posts will be presented next items. With the plugin Visual Model For F# can be modeled simple functional programs and generate F# source code based on them. How to do it, you will learn in subsequent posts on this blog.
Functions are the basic units of the program in any programming language, also in functional F #. When it comes to functions’ syntax in F#, we can distinguish the following elements:
- The name of the function
- Parameters that take arguments
- The body of the function
- The return type
The function name is an identifier that represents a function. The parameter list consists of the following parameters separated by spaces. You can explicitly specify the type for each parameter (Listing 1). Listing 1 shows that for the parameter x defined type int. If you do not specify a particular type of argument, the compiler tries to infer the type of body functions. Body functions are the expression or the submission of expressions, which finally gives the return value. The return value of functions in the function declaration may be omitted because it is optional. If you do not give explicitly return type, the compiler itself will define this type.
let f (x : int) = x + 1
Listing 1: Explicitly set type for the parameter
In F # we can treat functions as values. Then return constants (Listing 2).
let f = 1
Listing 2: The function returns a constant
Functions in F# are defined using the let keyword, if it is a function non recursive. If the recursive function is defined, it must use a combination of keywords: let rec. Recursive function is determined by placing the keyword rec.
This is shown in the following listing 3
// Non-recursive function definition. let [inline] function-name parameter-list [ : return-type ] = function-body // Recursive function definition. let rec function-name parameter-list = recursive-function-body
Listing 3: Definition of the function in F#
On below Listing 4 is shown a recursive function counting given value of n Fibonacci sequence. It is shown here the keyword rec talking about recursiveness of functions. The word fib is the name of the function. After the = sign is visible function’s body. Declaration of the return type is omitted, because the specification of F# is not forced to enter it.
let rec fib n = if n < 2 then 1 else fib (n - 1) + fib (n - 2)
Listing 4: Example of a recursive function
The function body must be mindful of the respective complement indented. Only then it will be treated as a definition of this function.
The proposed modeling language is an element that represents a function. It is shown below in Figure 1
For the above item, we can set properties such as:
- The function name (Name) – compliant with the specification of function names for the language F#
- The list of parameters (Name Type List) – dynamically created by the user from identifying the name and type of parameters
- Is recursive function (Is Recursive)
- Type returned by the function (Return Type)
It should be noted that for the F# programming language, we can optionally specify the return type for the function. By default, the generated source code fragment contained no return type. Return Type property is empty by default. If there is a value there, it will be inserted in the generated source code.
This element (Figure 1) is mapped to a graphical representation of the source code in a programming language F# as in Listing 5
let Function1 =
Listing 5: The generated source code
The default is a function without arguments. This function in F# can be called a defined value.
On the two following figures 2 and 3 are shown the functions for which they are defined list of parameters.
The first parameter is x which has a generic type. The argument may be the value of any type. The second parameter is y also with a default generic type. The third parameter is a tuple (a, b: int), which has two elements: a of any type and b that accepts only a value of type int.
The first function on figure 1 is defined as non recursive, on the another 2 for comparison recursive.
With these elements we obtain the source code as below.
let Function1 x y (a,b : int) : int =
Listing 6: The source code of the function with parameters
or
let rec Function1 x y (a,b : int) : int =
Listing 7: The source code of a recursive function with parameters
Properties for each function we can specify in the dialog with the properties of elements of the diagram, as shown in Figure 4 below.
To edit the list of parameters is used the window shown in Figure 5
Elements of tuples can be edited using the window shown below (Figure 6).