After watching Jeff Fritz’s F# Friday Stream, I was inspired to do some tinkering of my own. I have looked at F# in the past, but often times have not done much with it. After the stream, I decided to keep plugging away at it and one of the first things I thought about doing was organizing my code into separate files. As simple as it may seem, it was no easy task and while there are many different ways of doing it, the method described in this writeup is one of the easiest I found. Keep in mind though that this may not be the appropriate way of doing it although it seems to work nicely for our purposes. In this solution I will create a set of mathematical operations that will be organized into a single module that will then be imported and used inside of my console application. Source code for this sample solution can be found at this link.
This solution was built on a Mac, but should work on both Windows and Linux environments.
Once you have everything installed, you can create a new F# project by entering the following command into the console.
dotnet new console -lang f#
The goal of this solution will be to create functions that add, subtract, multiply and divide two numbers. Since all of these functions are mathematical operations, we can organize them inside a single module. Therefore, we can create a new file inside of our project called
Math.fs which will contain all of our functions.
Now that our file has been created, we need to add it to our list of files to be compiled by our project. To do so, we can add an entry for our
Math.fs file to the
ItemGroup property inside our
fsproj file. The contents of the file should look as follows after the addition:
Once we have our module created and our
fsproj file configured, we can call the module from our console application. To do so, we need to first import it into our
Once imported, we can call the
Math module functions from our
main function in the
printfn "Add: %i" (add 1 2)
Now that everything is set up, we can build and run our console application with the following commands:
The output should be the following:
In this writeup, I went over how to create an F# console application that organizes a set of mathematical functions into a module whose contents are stored in a separate file. This is good for organization and to reduce clutter. Happy coding!