Hello, Fino!
This tutorial aims to be beginner-friendly to people new to Fino, or new to the functional programming paradigm. It assumes some prior programming experience.
Now that we've hopefully installed Fino correctly, it's time to test out the language - with the classic Hello, Fino! program of course.
In this short section, we will:
- Write a simple program which prints a message
- Extend it to take in user input
- Dip our toes in a few simple but powerful features of the language
- Get an idea of how it feels to write effectful code in Fino
Writing the code
Get started by creating a new file, call it whatever you want, and preferably give it a .fn
file extension. Open up the file in your favourite text editor and proceed by writing the following lines of code:
import prelude
fn main : unit -> unit
() = println "Hello, Fino!"
Click on a tab to understand what a specific line of code does:
- Line 1
- Line 3
- Line 4
This line imports the prelude
module from the standard library, which in turn brings a small set of essential modules into scope, such as the io
module which we use println
from (on line 4).
This line defines the variable main
as a function which accepts a unit
as input and evaluates to a unit
as output. The unit
type only has one value - ()
- so it is often used like this when a function only produces side effects and does not evaluate to anything useful (in this case, the main
function only prints out a string).
There's a few different things happening on this line.
First, on the left of the =
sign is a pattern. Specifically, it is a pattern which matches the ()
literal. Pattern matching is covered in depth elsewhere in the documentation.
On the right of the =
sign is the body of the function, the expression which is evaluated when the function is called. In this case, the expression simply consists of the function println
being applied to a string literal. The function println
has a type of str -> unit
, so it takes in a str
as input and evaluates into a unit
as output.
Running the program
As described in the previous section on usage, to compile the program use the following command:
- Linux / MacOS
- Windows
fino -f hello-world.fn -o hello-world.bin
fino -f hello-world.fn -o hello-world.exe
And then simply run the executable to run the program:
- Linux / MacOS
- Windows
./hello-world.bin
hello-world.exe
You should see the following output:
Hello, Fino!
Some more
While writing a Hello, Fino! program is fun, let's try exploring some more of the language. Specifically, let's extend our program to take in user input in the form of a name, and then output a greeting.
As before, create a new file, name it whatever you want, and give it a .fn
file extension. Edit the file and write the following few lines of code:
import prelude
fn main : unit -> unit
() = do
println "What is your name?"
let name = input ()
println ("Hello, " + name + "!")
Currently, do
notation (and consequently mdo
) is only a planned feature and is not implemented in the compiler yet. To actually have a working program, look at the code below.
Now we've introduced a few more language features: do
notation, let
bindings, and user-defined operators.
[WIP]
Without do
notation
Here is the same function written without do
notation
import prelude
fn main : unit -> unit
() =
let _ = println "What is your name?" in
let name = input () in
println ("Hello, " + name + "!")