In this chapter, we take a look at theÂ main characteristics of Red, more specifically, what makes Red stand out compared to other programming languages. This will motivate you to start learning Red. Don't worry if some statements are a bit abstract at this stage; they will become clearer when we start using Red in the following chapters. It may be a good idea to come back to this chapter later.
We will cover the following topics:
- A short history of Red
- What makes Red special?
- Red's main features
You can find the code for this chapter here:Â https://github.com/PacktPublishing/Learn-Red-Fundamentals-of-Red/tree/master/Chapter01
RedÂ is an open source, modern programming language and platform, designed and developed byÂ Nenad Rakocevic. Red started out in early 2011 and is now at Version 0.6.3 (with 0.6.4 coming in the near future), aiming for a 1.0 release in the next year or so. In order to better understand its features, we have to explore its name and history a bit.
- Red builds upon REBOL, a languagethat gained some fame around the millennium and was builtÂ by Carl Sassenrath. In fact, Red is syntactically and semantically a 95% clone of REBOL, but tries to improve and enhance it where possible. This also means REBOL scripts can be easily converted to Red.
- Red (like REBOL) wants to reduce today's code complexityâit aims to have compact runtime environments and dependencies (around 1 MB), instead of the hundreds of MB of the Java or .NET platform.
Development of Red started out with Red/Systemâthis is a low-level language (like C) to enable programming at the full system level. Red/System is like an older siblingâit looks very much like Red, but it is aimed at lower-level programming so it is more restricted. We'll come back to it in Chapter 10, Advanced Red.
Gradually, Red evolved this foundation, adding with each release more and more data types, actions, and functions. A REPL interactive coding console, execution on ARM processors, a parsing engine, a cross-platform GUI dialect (working on Windows and macOS, and nearing completion on Linux and Android), reactive programming, and macros are among the hallmarks of this evolution.
From January 2015, the Fullstack TechnologiesÂ companyÂ was formed to continue Red's ongoing development, together with an ever-growing community of open source contributors. The company raised capital from InnovationWorks and GeekFounders (Chinese VC early-stage investors) and has its headquarters in Beijing's Silicon Valley.
In January 2018, Red joined the blockchain revolutionâthrough an Initial Coin Offering (ICO) with its own cryptocurrency, RED (Red Community Token)Â token, and it gained a vast amount of new funding capital to sustain and accelerate its completion. Using its unique capabilities, a special dialect of Red calledÂ Red/C3Â is being built, specifically dedicated to blockchain and smart contracts programming.
Since spring 2018, the Red project has been managed by the Red Foundation, based in Paris (France), and also led by Nenad Rakocevic. This organization will also foster a new economic modelÂ for open source projects,Â using the RED token to drive development.
Here we concentrate on the characteristics that sets Red apart from other contemporary programming languages.
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!
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, literallyself-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.
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
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.
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).
As a programming language, Red embraces nearly all paradigms:
- Imperative: Providing everything needed for controlling code execution and error-handling (see Chapter 4,Â Code Controlling Structures and Chapter 5, Working with Series and Blocks)
- Functional: With functions as first class values (see Chapter 6, Using Functions and Objects)
- Object-oriented: Complete with inheritance, but not class-based (see Chapter 6, Using Functions and Objects)
- Concurrency: With async task support and an actor model (from v 0.9.0)
- Symbolic: Being able to manipulate its own code as if it was plain data (all chapters)
- Reactive: Included in the GUI (Graphical User Interface) system (see Chapter 9,Â Composing Visual Interfaces and Chapter 10, Advanced Red)
Aside from REBOL and LISP, Red has taken inspiration from the following:
Although it can be statically compiled, Red has a graphical REPL console (Read Evaluate Print Loop) like other dynamic and scripting languages, to experiment with code interactively. We will explore this console in Chapter 2, Setting Up for Development.
In contrast to REBOL, which is an interpreted language only, RedÂ alsoÂ compiles to native codeÂ through Red/System,Â thus achieving much better performance, something like Crystal compared to Ruby. Compared to C, at this moment Red/System is 2-4x slower.
Red runs in the Windows, Linux, Android, macOS, and FreeBSD environments in a 32-bit version (the 64-bit version is planned) with cross-compilation capability. Furthermore, it runs on ARMv5 processors and on the Raspberry Pi and Arduino boards.
- High-level scripting (for example, glue code such as Lua)
- Web apps
- GUI desktop applications
- Android apps (from Version 0.7Â onward)
- 2D games
- Data processing
- Grammar parsing and constructing specialized languages
- Network/system programming, IoT devices programming, robotics
- Why does Red have a Red/System component?
- What is a full-stack language, and why is Red an example of that?
- What do the the Red and LISP languages have in common?
- Give three ways in which Red reduces software complexity.
- What is a dialect in Red? Give some examples.
- Why is Red faster than REBOL?
- Name some of the environments in which Red can run.