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.


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 Cocoa game GLGX GLSL iOS iPad iPhone Java jQuery Mac Mac OS X Objective-C OpenAL OpenGL Programming REBOL Shaders Vertex Shader web

Blog Archive

2011: 01 02 03 04 05 06 07 08 09 10 11 12
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