Book Image

Learn Red ? Fundamentals of Red

By : Ivo Balbaert
Book Image

Learn Red ? Fundamentals of Red

By: Ivo Balbaert

Overview of this book

A key problem of software development today is software bloat, where huge toolchains and development environments are needed in software coding and deployment. Red significantly reduces this bloat by offering a minimalist but complete toolchain. This is the first introductory book about it, and it will get you up and running with Red as quickly as possible. This book shows you how to write effective functions, reduce code redundancies, and improve code reuse. It will be helpful for new programmers who are starting out with Red to explore its wide and ever-growing package ecosystem and also for experienced developers who want to add Red to their skill set. The book presents the fundamentals of programming in Red and in-depth informative examples using a step-by-step approach. You will be taken through concepts and examples such as doing simple metaprogramming, functions, collections, GUI applications, and more. By the end of the book, you will be fully equipped to start your own projects in Red.
Table of Contents (13 chapters)
11
Assessments

What makes Red special?

Here we concentrate on the characteristics that sets Red apart from other contemporary programming languages.

A full-stack language – from the metal to the meta

Together with its foundation Red/System, Red forms a full-stack language—this means it can be used to develop high-level applications (domain-specific languages, graphical frontends, and high-level scripting) as well as low-level programs (embedded systems, operating systems, and device drivers), as shown in the following diagram in comparison with other languages:

It is quite unique in programming history that a single language spans the whole programming stack. This means that you as a Red developer only have to know one language from low to high level coding!

Code is data and data is code

Red is a modern descendant of the venerable LISP programming language. Instead of LISP's cumbersome parenthesis usage, it has taken Logo's simplified syntax. Everything in LISP is some kind of LISt Processing, and the same goes for Red—basically all code amounts to a combination of actions on blocks, which is the Red equivalent to LISP lists.

A block is simply a grouping of values between rectangular brackets [ ], such as [7 13 42]. You'll soon see that blocks are the fundamental building unit in Red code. This has the consequence that only a minimal syntax is used to represent code as well as data.

It will become clear that [ ] are used a lot in Red. On English or US keyboards this is easy, with its dedicated keys for [ and ]. On my (Dutch) keyboard it is ALTGR + ^ and ALTGR + $. On some keyboards, it is Alt + 5 and Alt + 6. I find it easier to switch to US keyboard layout while coding.

Like LISP, it has a very powerful capacity—it is homoiconic, literally self-representing. What this means is that Red can work with its code just as it does with its data values, because data and code are represented the same way, namely as blocks. This makes it possible for programs to write other programs, in other words, to do metaprogramming. We'll see several examples of this feature in the coming chapters.

Data types built in

For you as a developer to work with data efficiently, Red has more than 45 and counting built-in data types, from common types such as integers, chars, and strings to less common ones, such as pairs, tuples, files, URLs, emails, and so on. Moreover, you use the same set of operations to work with them, which greatly eases the learning curve. To do that in other languages, you would have to create objects from specialized classes to start working with them, or even import a library. As a first example, you can add and subtract days from a date value simply as follows:

;-- see Chapter01/special.red:
1-Feb-2018 + 14 ; == 15-Feb-2018
1-Feb-2018 – 42 ; == 21-Dec-2017

Small toolchain and executables

As we will see in the next chapter, when we're setting up, the complete toolchain (compilers, standard-library, docs, and REPL) comes packaged in a minimal 1 MB file, with no installation and configuration requirements—here we can see the reduction at work! Compare this with a Java runtime, which is about 150x bigger, or the Python runtime which also is 50 times bigger.

After compilation, Red produces executables of sizes typically around 0.5 to 1 MB, with no dependencies needed. This simplifies deployment of apps into production enormously. This aspect, combined with the low memory footprint, makes Red a very appealing choice for mobile and embedded apps. A garbage collector, which will be integrated in one of the next releases, ensures that memory consumption stays low.

Coding in Red is very expressive and concise, reducing script size, in the range of KBs. This will become apparent in the coming chapters. Just as an example, to read a web page into a string page you do the following:

page: read http://www.red-lang.org

In 2013, the Redmonk website published an article (which you can view here: http://redmonk.com/dberkholz/2013/03/25/programming-languages-ranked-by-expressiveness/) examining the expressiveness of languages. The results were summarized in the following diagram:

On the vertical axis, the number of lines of code per commit are depicted. We see that REBOL (Red's direct parent) scores very low compared to mainstream languages, indicating that fewer lines are needed in coding comparable tasks.

Dialects

One of Red's greatest strengths is the ability to easily create domain-specific languages (DSLs) or dialects. These are small and optimized sub-languages designed for a specific task, such as parse for transforming text (see Chapter 8, Parsing Data), view and draw for composing graphical user interfaces (see Chapter 9, Composing Visual Interfaces), and Red/C3 for blockchain programming (see Chapter 10, Advanced Red).