Skip to content
Rizo Isrof edited this page Mar 11, 2015 · 15 revisions

This page will introduce you to the main concepts of the Fold programming language and teach you how to effectively use the tools for building, testing and running your Fold programs.

Hello World

-- Here is a classical program which first implementation
-- dates to 1974, written by Brian Kernighan at Bell Labs.

print "hello, world!"

If saved in a file called hello.foldit can be run from your shell by invoking the Fold compiler.

$ fold hello

Or you can start the Fold REPL and open the program from there. And also test other examples from this guide.

$ fold
-> open hello
hello, world
 = ()

At the first glance it seems that we can't learn much from this small example, but in fact, it shows as some important though invisible aspects of the language.
The first one is that there are no special requirements for programs to start doing things. No namespace declaration and no main function. Just a pretty nostalgic greeting.

Literals

...

Variable Bindings

Values may happily exist by themselves but for us it is more useful if we keep track of them by binding them to variables.

x = 42
str_1 = "This is a string"

Blocks

A block is a sequence of expressions that has a value of the last one.

x = do
    2 + 2
    42
end

x == 42

Blocks can be used anywhere in Fold, even in the middle of simple arithmetic expressions:

40 + do
    print "Hey, we're in the middle of the block!"
    2
end

x == 42

Blocks are so useful that actually they are used by almost all core functions in Fold. Even the very function that creates functions uses blocks.

-- Next we will show how some important language
-- constructs _desugar_ into elementary expressions.
function sum x y =
    x + y
end

-- Is transformed into:
sum = x y => do
    x + y
end

Functions

Functions are also first-class values. Which means we may bind them to a variable, pass as an argument to another function, or store in a collection.

You may have noticed in our hello world program, which used print function, that the syntax for function invocation differs from the common mathematical notation – it omits the parentheses. Why do we need them at all? And even if we wanted we could add the parentheses and in many cases we actually have to.

(print "Hello, world")
Clone this wiki locally