23
OCT
2009

Broadened Horizons

People at work (and elsewhere) sometimes ask me if I'm a C or a Java programmer. Sometimes they even assume that I don't know how to write C code, based on the product written in Java that I am currently spending most of my time on. This annoys me a great deal.

Before I was asked these questions I never even considered people to be programmers of one specific language. It would be like asking a carpenter if he (or she) is a hammer carpenter or a screwdriver carpenter. In order to be a good developer, or a good carpenter for that matter, you should really know how to use more than one tool. You don't want to be a one trick pony.

It's important to broaden your horizons and see what kind of different languages and paradigms are out there. Learning a new language can really put things into perspective and make you think about programming in a new way.

For example, learning REBOL was the best thing I ever did, because it taught me new concepts that I didn't even know existed. I have been able to use this knowledge in other languages where these concepts may not have been obvious to me otherwise.

I have tried somewhere around 20 different programming languages and I encourage you to do the same, if you haven't already. It will be worth it.


5
SEP
2009

Self-Modifying Fun

An interesting consequence of code being data in REBOL is that it is very easy to modify code on the fly. You can write code that writes code. It's like in the Terminator movies where machines build machines, except that using REBOL probably won't result in judgement day. I hope!

You can use some of the series functions on instances of the function type as well. If you want to access the body of a function you get the second value of the object. Let me illustrate:

f: func [] [print "I am a function"]

The first block is the parameter block and since this function does not take any parameters as input, it is empty. The second block is the body of the function and it contains the code to print the string "I am a function".

So, let's examine the body of the function by getting the second element of the function instance series. Note that >> is the interactive REBOL prompt and == is the result of whatever operation you execute at the prompt.

>> second :f
== [print "I am a function"]

The : character is used to retrieve the value that f is bound to without executing the function. Calling the function yields the following expected result:

>> f
I am a function

OK, let's modify it to print something else.

>> poke second :f 2 "I am a modified function"
== "I am a modified function"

Poke replaces a value in a series with another value at a given index. In the example above, the value at index 2 in the body of the function is replaced with the value "I am a modified function". Since REBOL uses 1-based indexing, the value at index 2 was previously "I am a function". So let's check the source of the function now.

>> source f
f: func [] [print "I am a modified function"]

It seems like the modification worked. Calling the function yields the following result:

>> f
I am a modified function

Being able to modify code like this on the fly is a powerful feature. But, like Spider-Man's uncle Ben used to say: "With great power comes great responsibility". It is very easy to produce self-modifying code that is downright weird. Here is an example:

f: func [:g] [append second :g [+ 1] 1]

What does this function do? It takes a function as a parameter and adds + 1 at the end of the body of that function and then returns 1. There is nothing keeping us from passing the function f as a parameter to itself, so let's see what happens if we do. The : in the parameter specification suppresses evaluation of the parameter, so we can pass in the word bound to the function f instead of getting its value like we'd do with second :f. Here we go:

>> f f
1
>> f f
2
>> f f
3
>> source f
f: func [:g] [append second :g [+ 1] return 1 + 1 + 1 + 1]

Each call to the function modified the function itself, so the returned value was incremented with one everytime. There's no counter variable or anything, we simply added the code for + 1 to the function each time.


6
AUG
2009

Functionally Functional

How do you declare/define a function in REBOL? There has to be some kind of keyword or specific definition syntax for that purpose, right? Nope, I tell you! Nope!

First, let's take a look at some other languages. In C you define a function by sticking to a specific syntax (There is a subtle difference between declaring and defining a function, but let's not think about that right now). Here's an example:

int say_number(int number) {
    printf("%dn", number);
    return number;
}

Javascript has the function keyword for declaring functions:

function say_number(number) {
    document.write(number);
    return number;
}

The Javascript function keyword can also be used for declaring anonymous functions:

var say_number = function (number) {
    document.writeln(number);
    return number;
}

In REBOL, all functions, and I mean all functions, are anonymous. So, defining a function is like using the Javascript keyword then? No, not quite. Here's the REBOL version:

say-number: func [number] [
    print number
    number
]

I know what you're thinking. You're thinking that func sure looks like a keyword! It's not, I assure you. The func symbol is just a symbol that has been bound to a function. A function that creates functions from its two parameters: a list of parameter symbols and a list of data representing the body of the function.

So, let me reiterate; REBOL uses a function to define functions. I know, right? I'm sure it's like totally blowing your mind right now!


6
AUG
2009

Syntactic Beauty

Do you want to know something that I really like about REBOL? No? Well, too bad, because I'm telling you anyway. What I really like is the syntax of REBOL, or rather its lack of syntax.

Let's say you want to create an associative array in PHP, for example. It might look something like this:

$fruit_colors = array("banana" => "yellow",
                      "apple" => "green");

To print the color of a banana, you would then do something like this:

echo $fruit_colors["banana"];

Obviously, there's a whole lot of syntax going on there. If you have a swedish keyboard layout, like I do, you would have pressed a modifier key, like shift or alt, seven times while typing the last line alone, which strains your fingers. What if we were to do the same thing in REBOL?

fruit-colors: [
    banana yellow
    apple green
]
 
print fruit-colors/banana

It's so clean and beautiful that it makes me want to cry tears of joy. Well, not really. But almost.


26
JUL
2009

What's So Great About REBOL?

One of my favorite programming languages goes by the name of REBOL. It was designed by Carl Sassenrath, who wrote the original Amiga OS kernel. I first encountered the language before it was released on the REBOL1 website (rebol.com), which contained a description of the main concepts and some example code. That was all it took for me to get hooked. I knew immediately that it was going to be a great language.

Rebol Logo

I downloaded the first public release of the interpreter in 1997 and since then, I have written REBOL code almost every day. I have tried Perl, Lua, Ruby, Python and a bunch of other scripting type languages and there is nothing out there that rivals REBOL in terms of productivity. For me, at least.

Most people have never heard of REBOL. I have won no less than three official REBOL contests (here's one), which either goes to show that I am a REBOL genius or that the REBOL user base is limited. Unfortunately, I am leaning towards the latter.

I have informed a number of software engineers of the existence of REBOL and they always ask me what's so great about it. And here's the kicker: I can't for the life of me explain it in a way that does REBOL justice!

There are so many great little features that you can't really boil it down to a brief summary. And it's not just the features, it's how everything is tied together in a cohesive mass of pure awesomeness.

Code as Data

REBOL is all about the data. There are about 40 different data types. Even the code itself is data. This property of a programming language is referred to as homoiconicity. As a consequence of possessing this property, REBOL is its own meta-language! Usually, C programmers are hopelessly lost at this point, but anyone that has an understanding of Scheme or LISP probably gets it.

Rebol has no keywords. Instead it has symbols. The symbols are data constructs that have no inherent meaning; they are only words. It is not until a symbol is bound to a specific meaning in a specific context that the symbol resembles something a C programmer would think of as code.

You can think of REBOL code as a system of axioms and functions that are derived from these axioms. In REBOL, the common "for" loop is actually a function, which in turn is derived from the "while" loop function. The "while" function is a native function, which means it's implemented in C in the interpreter, i.e. it is an axiom. REBOL provides a number of native functions, and then every other function is built on top of the native functions.

I could literally go on forever, but I will leave REBOL's other fantastic features, such as the series abstraction and the domain specific dialecting, for a future blog entry.


  1. REBOL and the REBOL logo are registered trademarks of REBOL Technologies. 


About This Site

Hello, my name is Martin Johannesson and this is my home on the web. I live in Stockholm, Sweden, where I work as a software engineer at a software company.

Ever since I was a kid and discovered the art of programming on my C64, I've been tinkering with my own little software projects and experiments. This site is one such experiment.
more...

Recent Entries RSS Feed

Tags

Amiga blog C game GLGX GLSL iPad iPhone Java jQuery OpenAL OpenGL Programming REBOL Shaders Vertex Shader web

Blog Archive

2010: 01 02 03 04 05 06 07 08 09 10 11 12
2009: 01 02 03 04 05 06 07 08 09 10 11 12

Random Images Load new images

loading
loading
loading
loading
loading
loading
loading
loading
loading
loading
loading
loading

People I Know