17 January 2006
Scheme
Update
Alert reader Joshua C. wrote to correct my usage of the terms apply and map. Seems I had them backwards, but they’re fixed now.
I was thinking about Scheme and tried to create some of the more interesting functions in PHP. map came easily:
function map ($fnName, $list) {
$result = NULL;
foreach ($list as $key => $value) {
$result[$key] = $fnName ($value);
}
return $result;
}
function square ($x) { return $x * $x; }
var_dump (map("square", array (1, 2, 3, 4, 5));
Then I tried to write apply. And I very quickly realized there is no way to express it. Part of it is just the construct of the language: Scheme uses RPN and so variable numbers of arguments comes naturally. But without writing an entire list processor to handle binding between user input and the function the user is trying to call, we can’t do it. And what is Scheme? Lisp. List processing.
The point isn’t that you can only do it in Scheme. Of course you can somehow make it work in PHP. The point is that apart from the aloof-looking syntax, Scheme (even Lisp in general) has very intuitive constructs. Nearly anything that can be written in PHP (any of the C-style languages, really) can be written in the same manner in Scheme, but something originating in Scheme may take a good deal more work to port to a C-style language. It’s something that any Scheme programmer knows, but it took a long time to get, and still surprises me from time to time.

