Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn Red ? Fundamentals of Red

You're reading from   Learn Red ? Fundamentals of Red Get up and running with the Red language for full-stack development

Arrow left icon
Product type Paperback
Published in May 2018
Publisher Packt
ISBN-13 9781789130706
Length 252 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Ivo Balbaert Ivo Balbaert
Author Profile Icon Ivo Balbaert
Ivo Balbaert
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Red's Mission FREE CHAPTER 2. Setting Up for Development 3. Using Words, Values, and Types 4. Code-Controlling Structures 5. Working with Series and Blocks 6. Using Functions and Objects 7. Working with Files 8. Parsing Data 9. Composing Visual Interfaces 10. Advanced Red 11. Assessments
12. Other Books You May Enjoy

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).

You have been reading a chapter from
Learn Red ? Fundamentals of Red
Published in: May 2018
Publisher: Packt
ISBN-13: 9781789130706
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image