Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Armani Tallam edited this page Mar 31, 2022 · 1 revision

The Hasdrubal Language

Hasdrubal is a fairly high-level, statically-typed, general-purpose functional programming language. It's inspired by its fellow functional languages like F#. This means that we have (or plan to include) a lot features from those languages too such as first-class functions, ADTs, pattern matching and sum types.

So what makes Hasdrubal different then?

Let's explain. As a functional language, we really like side-effect free code. Wait, what's a side effect? They are the observable results of calling a function, other than the return value. Side effects include printing to the console, mutating values and throwing exceptions. We don't like side-effects because they make code harder to follow. Sometimes it's almost impossible to figure out when and where a value was mutated or where an exception was thrown.

Even though side effects are dangerous, we obviously still want need them. After all, what's the point of doing all these impressive calculations if we can't even see the answer? Luckily, we functional programmers have come up with many ways of doing side effects in a safe way. The problem is that some are harder to understand than others.

Some languages allow you to freely use side effects but encourages you to avoid them. But that can lead to unsafe code since you can't be sure about what a function call will do. other languages use monads. But those come with a very high cognitive overhead and some very hard limits (e.g. requiring monad transformers for some things).

This is where Hasdrubal comes in. What differentiates Hasdrubal from most other languages is the use of Algebraic Effects to handle side effects in code.

"Hello, World!" in Hasdrubal

# My first program in Hasdrubal.
let main(args) :=
    let message = "Hello, World!"
    println(message)
    return(0)
end

All executable programs in Hasdrubal have a main function. Without main, the compiler will assume that any file passed to it is a library so it'll just compile the file without runnning it.

The main function always takes 1 argument (usually called args) which is a list of the command line arguments passed in to the program from the command line. The main function also always returns an Int which is the program's exit status.

You may have noticed that return looks like a function call instead of a keyword. This is because it is. return is just a function that returns whatever you give it untouched. And since functions automatically return the value on its last line, main above returns 0.

Clone this wiki locally