To implement a function by using other functions is one of the basic concepts in most programming languages. Therefore this feature is very well supported in a lot of programming languages, especially in functional programming languages. Within this article I want to show you different possibilities to implement such a function in F#.
In our example we want to implement a function which calculates the square root of a number and returns an integer value. Therefore the result must be rounded. We want to round up the value to the next integer. To implement the needed function we may use the three framework functions: Math.Sqrt, Convert.ToInt32 and Math.Ceiling.
Function calls
A first way to implement the function is to call the three needed framework functions. The results of the functions will be stored in a value y and the value is used as input parameter for the other function calls.
let ceilingSqrt x = let y = Math.Sqrt x let y = Math.Ceiling y Convert.ToInt32 y
This is a possible solution but it is more an implementation you will do when you use a procedural programming language. Functional languages will offer other solutions.
Function chain
At next we may create an enhanced solution by removing the not needed temporary value y. This value can be removed by creating a chain of function calls. The following source code shows the according modification.
let ceilingSqrt x = Convert.ToInt32 (Math.Ceiling (Math.Sqrt x))
But of course this is also a procedural solution. Only the temporary value to store the partial result is removed.
Pipe operator
I am not very happy with the previous solutions because the nice features of the functional programming language F# are not used yet. Therefore for the next implementation of the function I want to use the F# pipe operator. The following source code will show the modified example.
let ceilingSqrt x = x |> Math.Sqrt |> Math.Ceiling |> Convert.ToInt32
That’s a solution I really like because it is easy to read and easy to understand. But at the end it is also a function chain with some syntactical sugar. Functions are a basic element in functional programming languages. They must be supported in a way like variables. Therefore to combine functions must be easy like to sum up two variables. So the pipe operator offers a nice solution but there must be an even better one.
Composition operator
In F# there exists an operator to combine functions: “>>”. By using this operator a new function may be created by linking different functions together. The following source code shows the new solution.
let ceilingSqrt x = (Math.Sqrt >> Math.Ceiling >> Convert.ToInt32) x
Furthermore it is possible to remove the function parameter:
let ceilingSqrt = Math.Sqrt >> Math.Ceiling >> Convert.ToInt32
Now we have a really nice solution which uses the features of functional programming languages. It creates a function by adding other functions together. And that’s easy like to add some variables together.
Summary
Functional programming languages offer powerful features to handle functions. If you use such languages you should also use these features. Especially if you previously worked with procedural or object oriented languages, you should try this new way of functional thinking.