Rebol

advertisement
Rebol
Just a quick look
QuickTime™ and a
decompressor
are needed to see this picture.
Philosophy






Rebol tries to be very simple to use, especially at first
Syntax is very simple, largely intuitive
Vocabulary can be quite large
Rebol depends on “dialects”: special-purpose vocabularies
for different kinds of tasks
Rebol is described as a “messaging language,” and has great
support for web and email
Data, including sounds and images, is represented as
characters
Elements

A Rebol program consists of three kinds of things:
 Values, such as numbers, booleans, etc.


Blocks



Words represent things (such as variables) and actions (such as
functions)
Also comments--they begin with a semicolon ( ; )
Rebol is homoiconic: there is no distinction between “code” and
“data,” other than what the program does with it
Rebol is interpreted, not compiled


A block is a list of things enclosed in square brackets, [ ], and
separated by white space
Blocks are evaluated only when the program says to evaluate them
Words


(but there’s a lot of “etc.”)
Values












Numbers: 5, 3.14, 1.2E7
Booleans: true, false
Hex: #{FF00FF}
Strings: "Hello world"
Characters: #"a"
Money: $4.99, EUR$4.99
URLs:
http://google.com
Email: steve@apple.com
Files: %letter.txt
Dates: 8-Dec-2010
Times: 10:30AM
tags: <head>, </head>





tags: <head>, </head>
Tuples: 127.0.0.1
Pairs: 1024x768
Issues: 1-800-555-1212
Blocks: [red green
blue]

There are about 45 types

Conversion functions are
to-type, e.g. to-char, tohex, to-image, etc.
Blocks

Blocks are things (values, words, other blocks)
enclosed in square brackets



There is no real distinction between code and data



[red green blue]
[if 2 < 4 [print 4]]
Thus, you can easily manipulate code
Implication: Functions--or any chunks of code--are first class
objects
Blocks are evaluated if the program evaluates them,
but not automatically
Words

There are five ways to use words:

As function names or variables: fun, value



Setting a word to a value: value: 5, foo: func [i] [i < 10]
Retrieving a value: :word

Useful for treating a function as a value, for example, to pass it as an
argument to some other function
Using a word as a symbol, standing for itself: 'Bob-Smith
 Using the word as a refinement: dump/hex, now/date
There are no reserved words
 For example, you can redefine if
 ...but it’s probably not a good idea!


When executed: Call the function or return the value
Evaluating a block

do will evaluate a block and return the last value


reduce will evaluate a block and return a block of results



>> do [now/date colors [colors $12] 4]
== 4
>> data: reduce [now/date colors [colors $12] 4]
== [12-Dec-2013 [red green blue] [colors $12.00] 4]
Notice that only top-level elements are evaluated
If a block contains both code and data, compose will evaluate
only things within parentheses

>> compose [ now/date (now/date) ]
== [now/date 12-Dec-2013]
Control structures










do [...]
if expr [...]
either expr [...][...]
while [expr] [...]
for i min max step [...]
until [... expr]
loop count [...]
repeat var max [...]
forever [...]
switch/default var [ 1 [...] 2 [...] ] [...]
Logic









false or none
true or anything that isn’t false or none
and x y
or x y
not x
xor x y
all [...]
any [...]
<
<=
=
==
!=
<>
>=
>
Numbers






+, -, *, /, **, // (remainder)
exp value, log-10 value, log-2 value, log-e value,
square-root value
absolute value, negate value
min x y, max x y,
sine, cosine, tangent, arcsine, arccosine, arctangent
No precedence; evaluation is done left to right
 Hence:
>> 2 + 3 * 10
== 50
Useful Functions











read source returns the string read from the source (file, url, etc.)
write dest data writes data to the destination (file, url, etc.)
ask question prompts the user with the question, returns the entered
string
input reads a line from the console
to-integer value converts a value to an integer
to-date value converts a value to a date
to-file value converts a value to a filename
prin data prints data without a line break
print data prints data, appends a line break
foreach act list [...] executes the block for every element act in list
now returns the current date/time
Defining functions

dump-i: [ print ["i =" i] ]



Not a function, just a block of code
>> i: 7
== 7
>> dump-i: [ print ["i =" i] ]
== [print ["i =" i]]
>> do dump-i
i = 7
dump-i: func [][ print ["i =" i] ]


This is a “real” function
>> dump-i: func [][ print ["i =" i] ]
>> dump-i
i = 7
Objects

account: make object! [
name: "Flintstone"
balance: $100
ss-number: #1234-XX-4321
deposit: func [amount] [balance: balance + amount]
withdraw: func [amount] [balance: balance - amount]
]

print account/balance
$100.00

account/deposit $300
print ["Balance for" account/name "is" account/balance]
Balance for Flintstone is $400.00
Quick example: Text editor

view layout [
h1 "Text Editor:"
f: field 600 "filename.txt"
a: area 600x350
across
btn "Load" [
f/text: request-file
show f
a/text: read to-file f/text
show a
]
btn "Save" [
write to-file request-file/save/file f/text a/text
alert "Saved"
]
]
Quick example: Email client

view layout[
h1 "Send:"
btn "Server settings" [
system/schemes/default/host: request-text/title "SMTP Server:"
system/schemes/pop/host:
request-text/title "POP Server:"
system/schemes/default/user: request-text/title "SMTP User Name:"
system/schemes/default/pass: request-text/title "SMTP Password:"
system/user/email: to-email request-text/title "Your Email Addr:"
]
a: field "user@website.com"
s: field "Subject"
b: area
btn "Send"[
send/subject to-email a/text b/text s/text
alert "Sent"
]
h1 "Read:"
f: field "pop://user:pass@site.com"
btn "Read" [editor read to-url f/text]
]
99 Bottles of Beer on the Wall

sing: func [count rest] [
prin pick ["99 bottles " "no bottles "
"1 bottle "[count "bottles "]]
min 4 count + 2
print rest
]
for bottles 99 0 -1 [
sing bottles "of beer on the wall,"
sing bottles "of beer."
print pick [
"Take one down, pass it around,"
"Go to the store, buy some more,"
] bottles > 0
sing bottles - 1 "of beer on the wall."
print ""
]
References

Rebol Quick Start, http://www.rebol.com/docs/quick-start.html,
by Carl Sassenrath, is a great introduction

Learn REBOL, http://www.re-bol.com/rebol.html, by Nick
Antonaccio, is a very good, and very thorough, tutorial

A Programming Tutorial for REBOL (PDF),
http://www.rebol.com/article/0201.html, by Victor Pavlu, is
thorough, but possibly a bit out of date


I got many examples from this tutorial
The recommended book is REBOL—A Programmer’s Guide, by
Olivier Auverlot and Peter William Alfred Wood
The End
“Ideas that require people to reorganize their
picture of the world provoke hostility.”
― James Gleick, Chaos: The Making of a New Science
Download