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("%d\n", 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!
Comments are subject to review and will not be shown until they have been approved, for the purpose of keeping spammers and morons away.
var say_number = reversefunc {
document.writeln(number);
return number;
} (number)
reversefunc: func [body args] [
func args body
] ; **that's all it takes!!!**
number-say: reversefunc [
print number
number
] [number]
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...