Quickstart

From Reia

Jump to: navigation, search
Bored of this? Try out the advanced quickstart

Here's a quick tutorial to take you through the basics of Reia. This is all based around what actually works. As more features become available the tutorial will be expanded.

The tutorial assumes you've already built Reia. You can start Reia's interactive interpreter directly from the checkout directory itself:

~/src/reia$ bin/ire
Reia Interactive Shell (prerelease)
Running on Erlang (BEAM) emulator version 5.6.3 [source] [smp:2] [async-threads:0] [kernel-poll:true]

>>

At the ">>" prompt you can enter any Reia expression you want. So what do Reia expressions look like? Let's start with the cliched "Hello, world!" example:

>> puts("Word up, holmes")
Word up, holmes
=> nil
>>

What just happened here? The puts statement requested the string "Word up, holmes" be printed with a newline character on the end. We see that string printed, followed by an => and the special keyword nil. Then we see the ">>" prompt which means we can put in another command.

So what does the => mean? This indicates that the value nil was returned from the puts function. In Reia, everything you do returns a value of some sort. In this case, the puts function returned the special value nil which means the return value is irrelevant. You can think of nil as being like the "null" value you've seen in other languages, or "None" in Python.

How about we try an example that actually returns a meaningful value? Let's try doing some arithmetic:

>> 2 + 2
=> 4

Well that was simple enough. Let's try something a bit more involved:

>> (14 * 8) ** 6 % 13
=> 12

Here we see three operators: the * operator which means multiply (there's also the / operator which means divide). Next is the ** which indicates an exponent, so in this case it's taking 14 * 8 to the 6th power, which is a very large number. Lastly comes the % operator, which takes the remainder of 14 * 8 to the 6th power when divided by 13. You also see parens being used to override the default order of operation, where 8 ** 6 would have higher precedence.

Can we compare numbers? Yes we can!

>> 3 < 2
=> false
>> 10 >= 3
=> true
>> 1 == 1
=> true
>> 5 != 10
=> true

We can break our programs up into smaller pieces by putting it into functions within modules:

>> module Foo
..   def plustwo(n)
..     n + 2
.. 
=> ~ok

The first thing you'll probably notice is that the prompt changed from ">>" to "..". This means that it's expecting you to enter more lines to complete the statement. We have to enter a blank statement at the end to let it know we're done.

The next thing you'll probably notice is that the code is indented. Why bother to do that on the shell? Well, the reason is that Reia is indentation sensitive and requires you to indent your code.

After defining the module, the shell returns the atom ~ok. We'll talk about these squiggly little guys later, but for now just know that it means module compiled correctly (don't worry, it won't leave any files around).

Now that the module has been declared, we can call it:

>> Foo.plustwo(2)
=> 4

As you've probably guessed, modules can contain multiple functions. Let's declare a new version of the Foo module:

>> module Foo
..   def plustwo(n)
..     n + 2
..   def plusthree(n)
..     plustwo(n) + 1
.. 
=> ~ok
>> Foo.plusthree(3)
=> 6

Here we see a module with two functions. We also see that one function (plusthree) is calling another (plustwo).

Let's try to do something useful. How about Fibonacci numbers? The first number of the sequence is 0, the second number is 1, and each subsequent number is equal to the sum of the previous two numbers of the sequence itself. That's a nice straightforward definition... can we do the same thing in code?

>> module Fibonacci
..   def calc(1)
..     0
..   def calc(2)
..     1
..   def calc(n)
..     calc(n - 1) + calc(n - 2)
.. 
=> ~ok
>> Fibonacci.calc(10)
=> 34

Now what's going on here? We have three functions, but they all have the same name. That seems a little odd. And what's more, it's being defined with a parameter already in place in two out of the three cases.

Here the three functions are actually acting as "clauses" of a single larger function, called "calc". When you call calc, the clauses are walked in order. If the parameter you pass matches the value in the definition, then a given clause is executed. When you pass "1" indicating you want the first Fibonacci number, the value zero is returned.

Looking back at the definition of Fibonacci numbers given above, you can see how clearly it maps to the above implementation. The definition in code almost perfectly matches the English description. And it's not like I worded it that way intentionally... the definition is verbatim from Wikipedia.

Personal tools