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!


 
Name:





CAPTCHA Image
Reload Image
(Surround code with --code-- and --/code--)


Comments are subject to review and will not be shown until they have been approved, for the purpose of keeping spammers and morons away.


 
To show this off better, an example might be in order. How about defining a new "keyword" which lets you put the body of a function before the parameters when you define it. (I'll call it reversefunc).

JavaScript just can't do it within the language:


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


But in Rebol this is a snap:


reversefunc: func [body args] [
func args body
] ; **that's all it takes!!!**

number-say: reversefunc [
print number
number
] [number]


Though to be honest this doesn't blow my mind. In fact it makes logical sense to me that code should work like this! LISP laid a lot of groundwork and other languages just ignored it because it was kind of an unreadable mess. But that's all fixable, Rebol's done most of the work...

What actually blows my mind is that people bother to use languages that have no chance at doing this without a preprocessor, after decades of knowing that being able to flex the language to meet the problem domain is a good thing.

But Rebol's func is weak on scope, good thing R3 has funcT which makes assignments (set-words) local by default. I keep pleading for them to rename funcT to function in the standard distribution, dunno if it will happen.


(Note to self: Rewrite the comment presentation code so that the comments keep the formatting from the text box.)

I agree with you about funcT. It should really be the default.

The "blowing your mind" part was really only a joke, but I've actually had problems explaining these things to seasoned C programmers. :-)


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