learnxinyminutes-docs/red.html.markdown

224 lines
7.4 KiB
Markdown
Raw Permalink Normal View History

2014-01-29 19:14:56 +04:00
---
name: Red
category: language
language: Red
filename: learnred.red
contributors:
- ["Arnold van Hofwegen", "https://github.com/iArnold"]
---
2015-10-08 06:11:24 +03:00
Red was created out of the need to get work done, and the tool the author wanted to use, the language of REBOL, had a couple of drawbacks.
2014-01-29 19:14:56 +04:00
It was not Open Sourced at that time and it is an interpreted language, what means that it is on average slow compared to a compiled language.
Red, together with its C-level dialect Red/System, provides a language that covers the entire programming space you ever need to program something in.
2015-10-08 06:11:24 +03:00
Red is a language heavily based on the language of REBOL. Where Red itself reproduces the flexibility of the REBOL language, the underlying language Red will be built upon,
Red/System, covers the more basic needs of programming like C can, being closer to the metal.
2014-01-29 19:14:56 +04:00
2015-10-08 06:11:24 +03:00
Red will be the world's first Full Stack Programming Language. This means that it will be an effective tool to do (almost) any programming task on every level
from the metal to the meta without the aid of other stack tools.
Furthermore Red will be able to cross-compile Red source code without using any GCC like toolchain
2014-01-29 19:14:56 +04:00
from any platform to any other platform. And it will do this all from a binary executable that is supposed to stay under 1 MB.
Ready to learn your first Red?
```
All text before the header will be treated as comment, as long as you avoid
using the word "red" starting with a capital "R" in this pre-header text.
This is a temporary shortcoming of the used lexer but most of the time you
start your script or program with the header itself.
2015-10-08 06:11:24 +03:00
The header of a red script is the capitalized word "red" followed by a
whitespace character followed by a block of square brackets []. The block of
brackets can be filled with useful information about this script or program:
the author's name, the filename, the version, the license, a summary of what
the program does or any other files it needs. The red/System header is just
like the red header, only saying "red/System" and not "red".
2017-01-01 02:43:35 +03:00
```
```red
Red []
2014-01-29 20:23:11 +04:00
;this is a commented line
2014-01-29 19:14:56 +04:00
print "Hello Red World" ; this is another comment
2014-01-29 19:14:56 +04:00
comment {
2014-01-30 12:57:20 +04:00
This is a multiline comment.
You just saw the Red version of the "Hello World" program.
2014-01-29 19:14:56 +04:00
}
; Your program's entry point is the first executable code that is found
; no need to restrict this to a 'main' function.
2015-10-08 06:11:24 +03:00
; Valid variable names start with a letter and can contain numbers,
2017-08-23 11:14:39 +03:00
; variables containing only capital A through F and numbers and ending with 'h'
; are forbidden, because that is how hexadecimal numbers are expressed in Red
; and Red/System.
2014-01-29 19:14:56 +04:00
; assign a value to a variable using a colon ":"
2014-01-29 19:14:56 +04:00
my-name: "Red"
2015-10-08 06:11:24 +03:00
reason-for-using-the-colon: {Assigning values using the colon makes
the equality sign "=" exclusively usable for comparisons purposes,
exactly what "=" was intended for in the first place!
Remember this y = x + 1 and x = 1 => y = 2 stuff from school?
}
2014-01-29 19:14:56 +04:00
is-this-name-valid?: true
; print output using print, or prin for printing without a newline or linefeed
; at the end of the printed text.
2014-01-29 19:14:56 +04:00
prin " My name is " print my-name
My name is Red
print ["My name is " my-name lf]
My name is Red
; If you haven't already noticed: statements do NOT end with a semicolon ;-)
2014-01-29 19:14:56 +04:00
;
; Datatypes
;
2015-10-08 06:11:24 +03:00
; If you know Rebol, you probably have noticed it has lots of datatypes. Red
; does not have yet all those types, but as Red want to be close to Rebol it
2014-01-29 19:14:56 +04:00
; will have a lot of datatypes.
2015-10-08 06:11:24 +03:00
; You can recognize types by the exclamation sign at the end. But beware
; names ending with an exclamation sign are allowed.
; Some of the available types are integer! string! block!
; Declaring variables before using them?
; Red knows by itself what variable is best to use for the data you want to
; use it for.
2015-10-08 06:11:24 +03:00
; A variable declaration is not always necessary.
2014-01-29 19:14:56 +04:00
; It is considered good coding practise to declare your variables,
; but it is not forced upon you by Red.
; You can declare a variable and specify its type. a variable's type
; determines its size in bytes.
2014-01-29 19:14:56 +04:00
; Variables of integer! type are usually 4 bytes or 32 bits
my-integer: 0
; Red's integers are signed. No support for unsigned atm but that will come.
; To find out the type of variable use type?
type? my-integer
integer!
2015-10-08 06:11:24 +03:00
; A variable can be initialized using another variable that gets initialized
2018-10-27 15:54:03 +03:00
; at the same time. Initialize here refers to both declaring a variable and
; assigning a value to it.
2014-01-29 19:14:56 +04:00
i2: 1 + i1: 1
; Arithmetic is straightforward
i1 + i2 ; result 3
i2 - i1 ; result 1
i2 * i1 ; result 2
i1 / i2 ; result 0 (0.5, but truncated towards 0)
; Comparison operators are probably familiar, and unlike in other languages
; you only need a single '=' sign for comparison. Inequality is '<>' like in Pascal.
; There is a boolean like type in Red. It has values true and false, but also
; the values on/off or yes/no can be used
2014-01-29 19:14:56 +04:00
2014-01-30 00:35:28 +04:00
3 = 2 ; result false
3 <> 2 ; result true
2014-01-30 00:35:28 +04:00
3 > 2 ; result true
3 < 2 ; result false
2 <= 2 ; result true
2 >= 2 ; result true
2014-01-29 19:14:56 +04:00
;
; Control Structures
2015-10-08 06:11:24 +03:00
;
2014-01-29 20:23:11 +04:00
; if
2018-11-26 11:56:09 +03:00
; Evaluate a block of code if a given condition is true. IF returns
; the resulting value of the block or 'none' if the condition was false.
2014-01-29 20:23:11 +04:00
if a < 0 [print "a is negative"]
; either
; Evaluate a block of code if a given condition is true, else evaluate an
; alternative block of code. If the last expressions in both blocks have the
; same type, EITHER can be used inside an expression.
either a > 0 [
msg: "positive"
][
2014-01-29 20:23:11 +04:00
either a = 0 [
msg: "zero"
][
msg: "negative"
]
]
print ["a is " msg lf]
2015-10-08 06:11:24 +03:00
; There is an alternative way to write this
; (Which is allowed because all code paths return a value of the same type):
2014-01-29 20:23:11 +04:00
msg: either a > 0 [
"positive"
][
2014-01-29 20:23:11 +04:00
either a = 0 [
"zero"
][
"negative"
]
]
print ["a is " msg lf]
; until
2015-10-08 06:11:24 +03:00
; Loop over a block of code until the condition at end of block, is met.
; UNTIL always returns the 'true' value from the final evaluation of the last expression.
2014-01-29 20:23:11 +04:00
c: 5
until [
prin "o"
c: c - 1
2014-01-30 00:35:28 +04:00
c = 0 ; the condition to end the until loop
2014-01-29 20:23:11 +04:00
]
; will output:
ooooo
; Note that the loop will always be evaluated at least once, even if the
; condition is not met from the beginning.
2014-01-29 20:23:11 +04:00
; while
2015-10-08 06:11:24 +03:00
; While a given condition is met, evaluate a block of code.
; WHILE does not return any value, so it cannot be used in an expression.
2014-01-29 20:23:11 +04:00
c: 5
while [c > 0][
prin "o"
c: c - 1
]
; will output:
ooooo
2014-01-29 19:14:56 +04:00
;
; Functions
;
2014-01-30 00:35:28 +04:00
; function example
2014-01-29 19:14:56 +04:00
twice: function [a [integer!] /one return: [integer!]][
c: 2
a: a * c
either one [a + 1][a]
]
b: 3
print twice b ; will output 6.
2014-01-29 19:14:56 +04:00
2014-01-29 20:23:11 +04:00
; Import external files with #include and filenames start with a % sign
#include %includefile.red
; Now the functions in the included file can be used too.
2014-01-29 19:14:56 +04:00
```
## Further Reading
2015-10-08 06:11:24 +03:00
The main source for information about Red is the [Red language homepage](http://www.red-lang.org).
2014-01-29 19:14:56 +04:00
The source can be found on [github](https://github.com/red/red).
2014-01-29 20:23:11 +04:00
The Red/System language specification can be found [here](http://static.red-lang.org/red-system-specs-light.html).
To learn more about Rebol and Red join the [chat on Gitter](https://gitter.im/red/red). And if that is not working for you drop a mail to us on the [Red mailing list](mailto: red-langNO_SPAM@googlegroups.com) (remove NO_SPAM).
2020-07-25 20:34:36 +03:00
Browse or ask questions on [Stack Overflow](https://stackoverflow.com/questions/tagged/red).
2014-01-29 19:14:56 +04:00
Maybe you want to try Red right away? That is possible on the [try Rebol and Red site](http://tryrebol.esperconsultancy.nl).
2015-10-08 06:11:24 +03:00
You can also learn Red by learning some [Rebol](http://www.rebol.com/docs.html).