Looking for work? Check out our jobs area.

Rebol Programming For The Absolute Beginner

By: Nick Antonaccio
Updated: 2-6-06

Contents:

  1. For New Programmers
  2. Using the Rebol Interpreter to Speak to the Computer
  3. Learning the Rebol Language
  4. First Code Examples - Creating a GUI Window
  5. More Examples
  6. Understanding Variables and Functions
  7. Rebol Words
  8. GUI Words and Grammar - Some More Depth
  9. Creating Your Own Variable Words
  10. Blocks
  11. Function Words
  12. Several Ways to Create Functions in Rebol - Passing Variables
  13. Conditional Operations
  14. Looping
  15. Working With Longer Examples
  16. Embedding Binary Data
  17. Modular Programming and Code Reuse
  18. 6 Complete Rebol Programs For You To Study
    1. Chat Room
    2. Image Effector
    3. Sliding Tile Game
    4. Guitar Chord Diagram Maker
    5. Listview Database
    6. 3D Example
  19. Menus
  20. Understanding the CGI Interface and Web Programming with Rebol
  21. What Next?
  22. One Final Point

1. For New Programmers

This tutorial demonstrates how easy it is to accomplish real world programming goals with a flexible and powerful language called Rebol. The text aims to teach average users to program computers to do useful things, without the long and difficult learning curve imposed by other programming languages. If you're an experienced programmer, be sure to check out the final example applications. You'll be amazed at Rebol's compact code and simple cross-platform versatility.

The concepts demonstrated throughout the tutorial will help new programmers understand other languages and programming tools, and will lay the groundwork for novices to acquire mainstream coding skills. Before diving into the raw mechanics, here's a fundamental perspective that's helpful to understand:

Modern computers, operating systems and programs all do very basic things, in limited ways, with a limited scope of data types:

  • They let users input various types of data: text, images, sounds, video, etc.
  • They let users save, retrieve, organize, share/transfer, manipulate, alter, view and otherwise deal with that data in useful ways.
Everything that can be done with a modern computer basically involves manipulating text and non-text data (non-text data is called "binary" data). In the current state of modern computing, data of all types is typically input, manipulated, and returned via graphical user interfaces such as Windows program interfaces, web forms displayed in browsers, and other keyboard/mouse driven "GUI"s. Data is saved on local hard drives and storage devices (CDs, thumb drives, etc.) and on remote web servers, and is typically transferred via local networks and Internet connections. Knowing how to control those familiar computing elements to allow users to manipulate data, is the goal of learning to program. It doesn't matter whether you're interested in writing business applications to work with inventory and scheduling (text data), programs to alter Internet web pages or emails (text and image data), programs to organize or play music (binary data), programs to transfer files across networks (text and/or binary data), programs to broadcast video and sound across the Internet (rapidly transferred sequential frames of binary data), programs to play games, etc... They all require learning to input, manipulate, and return data of some sort. You can do all those things with Rebol, and once you've done it in one language, it's easier to do with other specialized languages and programming tools. Rebol handles common user interfaces and data types easily and intuitively. It allows programmers to quickly build graphic interfaces to input and return all common types of data. It can easily manipulate text, graphics, and sounds in useful ways, and it provides simple methods to save, retrieve, and share data across all types of hardware, networks, and the Internet. That makes it a great way to begin learning how to program.

2. Using the Rebol Interpreter to Speak to the Computer

The Rebol interpreter is a program that runs on your computer. It translates written text organized in the Rebol language syntax ("source code") to instructions the computer understands. One of the great things about Rebol is that it's a very small program, contained in a single file, that runs on just about every type of computer and operating system (Windows PC, Macintosh computer, Linux web server, etc.), without any complicated install process. It lets you speak to all those machines using the same language. You just download the Rebol interpreter program, feed it a text file full of code, and the machine does what you want, with the data you want. It's easy to use.

To get the free Rebol interpreter for Microsoft Windows, go to http://rebol.com/view-platforms.html and download the view.exe file for Windows - it's clearly marked, just click it with your mouse and save it to your hard drive. When you run view.exe for the first time, you can install it if you want, but you don't have to. Just follow the instructions on screen. Once you've got the Rebol interpreter downloaded and running on your computer, click the "Console" icon, and you're ready to start typing in Rebol programs. To create your first program, type the following line into the Rebol interpreter, and then press the [Enter] (return) key on your keyboard:

alert "Hello world!"


Voila, simple.

If you want to run Rebol on any other operating system, just select, download and run the correct file for your computer. It works the same way on every operating system.

3. Learning the Rebol Language

To learn any programming language, you need to type in lots of code by rote. Be sure to manually key in the examples in this tutorial, not just to see them work, but to get used to speaking the language. Avoid copying and pasting. At first, you'll learn to use the new programming language in the same way you'd learn a new spoken language - by immersing yourself in the words and phrases (even if at first you don't understand what's being said). Repeating sentences by rote is a natural way to get a feel for how a language sounds and works. Certain words, phrases and patterns eventually become familiar, and language syntax becomes clear and more intuitive. Seeing the examples run is absolutely essential - associations must be formed between things the computer does, and chunks of code that make those things happen. Just reading through the code isn't enough - seeing how it executes is the key.

Because programming languages require more specific syntax than spoken languages, learning the exact grammar is more important than in spoken language. Simple examples like the ones that follow are easy to remember - just like simple phrases memorized in spoken foreign languages. To become more fluent, however, the correct order of words, characters and other elements must be typed in exactly, in a much more specific way than components of spoken language. The interpreter is not intelligent enough to "guess" what you intend to do, and if you get the grammar wrong (even one character), it'll interpret your code incorrectly and do something different then you want. Sometimes, you'll get an error, and the interpreter will tell you where you got the grammar wrong in your code. Sometimes your program just won't work correctly. Getting intimately familiar with the syntax, from the beginning, will help you avoid those problems.

4. First Code Examples - Creating a GUI Window

Computer programs typically use Graphical User Interfaces to get data from the user and to display data to the user. GUIs work intuitively, and modern computer users are familiar with them. They contain clickable buttons, text entry fields, menus, images, and other "widgets" that allow the user to interact with the computer. Windows programs are GUIs. Web pages are also GUIs. Users click buttons with the mouse pointer to perform actions, select settings from menus, type text data into fields, etc. Most modern programming languages include some facility to build graphic interfaces that can be used to interact with the user. Rebol makes GUI creation easy. To create a simple GUI window, just type the following line into the Rebol interpreter, and press [ENTER]. Notice the "view layout" words in the examples below - you'll use them every time you create a GUI interface in Rebol:

view layout/size [] 400x300


That line of code creates a window 400 pixels across and 300 pixels down (pixels are dots on the computer screen). It doesn't do anything yet, but the GUI window can be moved around the screen, minimized and closed (with the "X" in the upper right hand corner), just like any other Windows program. In other programming languages, just creating a window like that can take several pages of code and lots of preliminary understanding. Rebol makes it easy.

To add a button to the above GUI, type the following code. Notice that the word "button" has been added between the brackets:

view layout/size [button] 400x300


Now the GUI has a generic blue button that you can click with the mouse. To add some text to the button, type the following code. Notice that the text "Click Me" has been added after the button:

view layout/size [button "Click Me"] 400x300


To make the button do something, type the following code, and then click the GUI button with your mouse pointer. Notice that the text [alert "Hello World"] has been added after the button text:

view layout/size [button "Click Me" [alert "Hello World"]] 400x300


Now when you click the button in your GUI, the computer responds by alerting you with the message "Hello World". To make the program do something a bit more interactive, type the following, and then click the GUI button once again. Notice the "data: request-text" addition at the beginning of the line. That code requests some text from the user and assigns it to the word "data", so it can be referred to and used in the program:

data: request-text view layout/size [
    button "Click Me" [alert data]] 400x300


The code above is split onto two lines so that it fits within the width of this web page, but it can be typed into the Rebol interpreter as a single line. That one line is all it takes to create a program which gets some data from the user, creates a graphic user interface that waits for user interaction, and does something with the input data (displays it in a little dialog box).

Next, we'll save some data to your computer's hard drive. Type in the following code, and click "yes" if the Rebol interpreter asks for your permission to write to the hard drive. Notice the "write %/c/data.txt data" added to the end of the line. That code writes the data collected from the user, to a file on the C: drive called "data.txt"

data: request-text view layout/size [
    button "Click Me" [alert data]] 400x300 write %/c/data.txt data


If you look on your computer's C: drive after closing the GUI, you'll see that there now exists a text file (C:\data.txt) containing the text you typed into the program. (If you're working in an operating system other than Windows, you'll need to change the "c" character in the above line to refer to a root directory on your hard drive).

With that one line of code, you've got a working program that actually does something useful. It gets, displays, and saves some data from a user, using familiar GUI interactions. You could adjust it to store phone numbers, usernames/passwords, or any other useful information. It's easy - and things only get more interesting from there!

Rebol is great at dealing with all types of common data - not just text. You can easily display photos and other graphics in your GUIs, play sounds, display web pages, etc. And it's just as good at dealing with data transferred across networks and the Internet. Here's some code that downloads an image from a web server and displays it in a GUI - notice the "view layout" words again:

view layout [image load http://rebol.com/view/bay.jpg]


The word "image" inside the brackets displays a picture in the GUI. The word "load" downloads the image to be displayed.

Rebol allows many built-in effects to be applied to images. Notice the "effect [effect type]" code in the following examples:

view layout [image load http://rebol.com/view/bay.jpg effect [Grayscale]]
view layout [image load http://rebol.com/view/bay.jpg effect [Emboss]]
view layout [image load http://rebol.com/view/bay.jpg effect [Flip 1x1]]


Here's how you can read that same file from the web server and save it to your C: drive. The "/binary" modifier is used whenever dealing with binary (non-text) data:

write/binary %/c/bay.jpg read/binary http://rebol.com/view/bay.jpg


Now you can read the image directly from your hard drive and display it in a GUI. Just type in the code below. Notice the "view layout" words again, and this time in between the brackets, the file is loaded from the local C: drive:

view layout [image load %/c/bay.jpg]


It's important to note here that you could also use that image in other graphic applications on your computer - including other programs that aren't written in Rebol. You could, for example, open that file in an image editing program, or attach it to an email and send it to a friend. It's critical to understand that data can be interoperable between languages, programming tools, operating systems, and other connected domains. As you learn more about programming, you may find specialized tools that accomplish certain programming goals easily and effectively. You can trade data between various tools by just saving it to a shared storage medium. In that way, all programming languages and tools can be used together to accomplish complex goals. That's a key concept to keep in mind when learning about general programming. Very little technology is truly new. The basis for most computing applications has been around for several decades (hard drives for storing files within directory structures, hardware for inputting and outputting text, graphic, audio, and other data, networks for transferring data between machines, etc.). Those base components haven't changed too dramatically. They've simply improved in speed and capacity - and the software tools used to work with them have evolved to allow programmers to do high level things more quickly and easily. An adept programmer writing an application to deal with large amounts of text data shared across the Internet, however, will realize that the data all still resides in files on a hard drive somewhere, in a machine connected to a network, and that data can be accessed by various old fashioned programming means - even if it was created and put there by a program using cutting edge database technology, displayed in a flashy new graphic interface, and transferred over broadband wireless connections. It's still just text data saved in a file somewhere, and it can be manipulated via virtually any language that provides access to networks and text files! Keep that in mind as your exposure to various programming tools expands...

5. More Examples

Below are some more short examples of reading, writing, and manipulating data on the hard drive and Internet, and interacting with the user. Type them into the Rebol interpreter to familiarize yourself with a bit more of the Rebol language.

The following line displays the current day and time in the interpreter:

print now


The word "print" displays text data directly in the Rebol interpreter. The word "now" refers to the current date and time.

The following line performs some mathematical computations, and displays the result:

print (10 + 12) / 2


The following code asks the user to choose a file on the hard drive:

request-file


The code below allows the user to choose a color:

request-color


The following code asks the user a yes-no question:

request "Are you having fun yet?"


Here's a nice way to let the user select a date:

request-date


The code below requests a username and password from the user:

request-pass


The following code opens your computer's web browser and displays the indicated web page:

browse http://rebol.com


The following code launches Rebol's built in text editor, and opens the file c:\test.txt

editor %/c/test.txt


Notice the percent character ("%") in the example above. In Rebol, that character is used to represent all file labels. Because Rebol can be used on many operating systems, and because those operating systems all use different syntax formats to refer to drives, paths, etc., Rebol uses the universal format: %/drive/path/path/.../file.ext . For example, "%/c/windows/notepad.exe" refers to "C:\Windows\Notepad.exe" in Windows. Rebol converts that syntax to the appropriate operating system format, so that your code can be written once and used on every operating system, without alteration.

The following line sends an email to user@website.com, containing the text "Hi user. How are you doing?". Try replacing the username and website with your own email address (If you downloaded and ran Rebol without actually installing it, you'll need to run the configuration wizard in order to send the email. To do that, type "install" and follow the instructions.):

send user@website.com "Hi user.  How are you doing?"


The line below sends a web page to user:

send user@website.com read http://www.rebol.com


The code below displays the contents of user's email inbox:

print read pop://user:pass@website.com


The following line uploads a single file to user's web server using ftp:

write/binary ftp://user:pass@website.com read/binary %file


The following uploads an entire directory of files to user's web server:

foreach file load %./ [if not dir? file [write/binary join
    ftp://user:pass@website.com/ file read/binary file]]


It all looks a lot like spoken English, doesn't it? You just need to type things in correctly, and the computer does what you want. The more of the language you learn, the more you'll be able to make the computer do your bidding... Easy, right?

6. Understanding Variables and Functions

All programming languages make use of two important features: variables and functions. Functions are commands that tell the computer to do something. They're similar to verbs in spoken language. You've used some functions already in the earlier examples. For example, "print" is a function - it can be thought of as a verb that represents some action.

Variables are names given to data to be input, stored, manipulated, displayed, etc. They're similar to nouns in spoken language. For example, "now" can be thought of as a variable. It's a noun referring to a piece of data (the current time).

Functions and variables must be used in a syntax defined by the programming language you're using. No existing computer language allows you to speak freely as in the natural language sentence "show the user some photos of their children". You have to write the commands in a more specific way - using a syntax that invariably involves variables and functions. As a programmer, to get the computer to "show the user a photo", you'd:

  • assign the picture filename to a variable (noun)
  • use a function (verb) to display the image referred to by the variable
  • assign the function action to a button or some other graphic widget in a GUI (which is itself created by arranging variables and functions in a specific order)
Rebol does many things in as "high level" a way as can be expected - as close to speaking to the computer in human English as is currently possible, but it still requires specific syntax and structure. All contemporary computer languages do. If you want to learn how to program in any language, you'll need to learn how to use variables and functions in the grammar the language defines. That's at the core of all programming.

In Rebol, variables and functions are assigned to "words".

7. Rebol Words

If you want to give a label to some data - so that it can be used in your program - you must assign it a word. The same is true for actions performed by functions. There are many function words built into Rebol that represent common actions ("print", "alert", "request-date", etc). You've seen a number of those built-in words already in the earlier examples. To create your own functions, you take previously created function words and variables, group them together in a specific order that accomplishes what you want, and assign a word to that collection of code. Then you can refer to that group of actions and/or related data, using the word you've assigned to it.

Learning the function words that are already defined in a programming language is a big part of learning to program. Those words are the existing vocabulary of the language, and in order to work correctly, they typically expect some other words to follow afterwards, in a specific order and format. The word "write", for example, writes data to a storage device (hard drive, flash drive, web server, etc). It's a function that performs an action, and expects the name of a file to be written to the hard drive, and then the name of the data to write to that file. It's gotta be written in that order:

write %/c/text.txt "This is some random text"
If you type the above line in the wrong order, it won't work:
write "This is some random text" %/c/text.txt ; WRONG


If you type it in backwards, Rebol won't understand the syntax, and you'll receive an error.

8. GUI Words and Grammar - Some More Depth

As you saw in the earlier examples, the words "view layout", followed by two brackets ("[]") can be used to display a Graphic User Interface in Rebol. You can put elements that you want to see in the GUI, inside the brackets. Rebol contains words that display all the common graphic elements used in GUIs. Try typing in the following code examples:

view layout [button]
view layout [field]
view layout [text "Rebol is really pretty easy to program"]
view layout [text-list]
view layout [
    button
    field
    text "Rebol is really pretty easy to program"
    text-list
    check
]


Notice that the words can be separated by "white space" inside the brackets - extra spaces, carriage returns, etc. It's ignored by the interpreter. Tab stops are traditionally used to indent the lines within brackets, but they're not required.

More descriptive characteristics about the graphic elements can be included directly after each of their respective words. Such modifiers are called "facets" in Rebol, and they allow you to adjust all characteristics of every type of graphic widget (size, color, displayed text), etc. Try typing in the code below - it's the same as the above code, with some additional facet characteristics:

view layout [
    button red "Click Me"
    field "Enter some text here"
    text "Rebol is really pretty easy to program" purple
    text-list 400x300 "line 1" "line 2" "another line"
    check yellow
]


IMPORTANT: If you want a graphic element to perform an action, just put the action word(s) in brackets after it. Type in the following code to see how it works:

view layout [button [alert "You clicked the button."] ]
view layout [button red "Click Me" [alert "You clicked the red button."]]
view layout [
    text "Some action examples.  Try using each widget:"
    button red "Click Me" [alert "You clicked the red button."]
    field 400 "Type some text here, then press [Enter] on your keyboard."
        [alert value]
    text-list 400x300 "Select this line" "Then this line" "Now this line"
        [alert value]
    check yellow [alert "You clicked the yellow check box."]
    button "Quit" [quit]    
]


Here are some other GUI elements used in the Rebol language:

view layout [
    backcolor white
    h1 "More GUI Examples:"
    box red 500x2
    bar: progress
    slider 200x16 [bar/data: value show bar]
    area "Type here"
    drop-down
    across 
    toggle "Click" "Here" [print value]
    rotary "Click" "Again" "And Again" [print value]
    choice "Choose" "Item 1" "Item 2" "Item 3" [print value]
    radio radio radio
    led
    arrow
    return
    text "Normal"
    text "Bold" bold
    text "Italic" italic
    text "Underline" underline
    text "Bold italic underline" bold italic underline
    text "Serif style text" font-name font-serif
    text "Spaced text" font  
    return
    h1 "Heading 1"
    h2 "Heading 2"
    h3 "Heading 3"
    h4 "Heading 4"
    tt "Typewriter text"
    code "Code text"
    below
    text "Big" font-size 32
    title "Centered title" 200
    across
    vtext "Normal"
    vtext "Bold" bold
    vtext "Italic" italic
    vtext "Underline" underline
    vtext "Bold italic underline" bold italic underline
    vtext "Serif style text" font-name font-serif
    vtext "Spaced text" font  
    return
    vh1 "Video Heading 1"
    vh2 "Video Heading 2"
    vh3 "Video Heading 3"
    vh4 "Video Heading 3"
    label "Label"
    below
    vtext "Big" font-size 32
    banner "Banner" 200
]


The examples above demonstrate how Rebol creates GUIs that can be used to input and display data in a variety of ways that are familiar to users. They can be customized using facets, and they can perform actions. That's a big part of building a typical modern computer program! For more information about GUI design, see http://rebol.com/docs/easy-vid.html and http://rebol.com/docs/view-guide.html. To make your GUIs do useful things, you need to learn more about making the language manipulate data in useful ways...

  Next Page





 

Other Views

corner
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A BootstrapLabs project.