Red Foundations

From Programming in RED
Jump to navigation Jump to search

(WORK IN PROGRESS)

(Let's introduced manipulating block first, using RED Elements)


A Red program is just a set of elements in a container called a block. The code you create in your text editor, once loaded, it is inserted inside a block whose square braces are not visible.

Here is a simple program:

[Red [Tittle: {My Program}] a: 22 Probe a]

DO [Red [Tittle: {My Program}] a: 22 Probe a] 22 == 22 >>

Let's assign to the word my-program the above block of code

my-program: [Red [Tittle: {My Program}] a: 22 Probe a]

And run it

DO my-program 22 == 22 >>

The result is the same.

A block is composed of elements recognizable by Red interpreter. The above block is made of 6 elements:

probe length? my-program == 6 >>

Let's print the first element

probe pick my-program/1 == Red >>

Now, let's print the last element:

probe pick my-program/6 == a >>

Or use a shortcut:

probe last my-program == a >>

We can modify the program to print 33 instead of 22:

change at my-program 4 33

Read it as: change (at my-program 4) 33

Run the code on the console:

>> change (at my-program 4) 33 == [Probe a] >> probe my-program [Red [Tittle: "My Program"] a: 33 Probe a] == [Red [Tittle: "My Program"] a: 33 Probe a] >>

And run the code:

>> do my-program 33 == 33 >>

Now the program has been changed and it outputs 33


You have the following bricks of Red Element in blocks:

set-words: [a: a: a:]

numbers: [22 33 55]

istructions: [probe probe probe]

blocks: [[] [] []]

code-to-execute: []

...


They can be in any order when stored but to be executed the order should follow some simple rules.


When not interpreted, elements can be in any order, when you evaluate the block, a set of rules are applied to interpret the block elements a data takes life in a program.