PHP as sexy as Scheme using lambdas and recursion
newmediacampaigns.com
newmediacampaigns.com
Any sufficiently complicated C or Fortran program contains an ad hoc,
informally-specified, bug-ridden, slow implementation of half of Common Lisp.Let's remember Greenspun used TCL for his commercial work.
$fn = create_function( "$a", "return $a * 2" );
$list = array( 1, 2, 3, 4 );
$double = array_map( $fn, $list ); // array( 2, 4, 6, 8 )
or function my_filter( $a ) { return $a > 10 ? true : false; }
$list = array( 5, 10, 15, 20 );
$less = array_filter( $list, "my_filter" ); // array( 15, 20 )It's not much more than a glorified eval(). Evaluation takes place at runtime, so you don't notice syntax errors until it's too late. Bytecode-Caches can't cache your function and as the function body is a string, you won't get syntax highlighting in most editors.
Also, create_function really creates a named (with a random name in the form lambda_number) function in the global scope. Functions in general are all globally scoped and come to life once the execution reaches the function declaration. So this actually works:
function a(){
function b(){ echo "gnegg\n"; }
}
a();
b();
Whereas function a(){
function b(){ echo "gnegg\n"; }
}
b();
wouldn't.This also implies that no scope is captured and you can't access any variables of the outside body of code.
5.3 brings real anonymous functions and provides a way to conserve the enclosing scope (thus allowing you to create closures), though the syntax is a bit awkward and variables of the outer scope you want to access need to be declared one by one.
I'm going down... I know. But it just takes so much more effort to translate the above into a well thought out, persuasive argument, and in the end it would have been ignored anyway by those who like to cling to their language just because they know it well.
It's just hard sometimes for me to keep my mouth shut when I see articles titled "PHP as sexy as Scheme", even when rationally I know that the author probably didn't really mean that literally. To me, it's like someone saying "DOS like OS X, now with 32-bit graphics!"
If you say curly braces and type prefixes can ever be pretty... ;-)
Lisp is not just a collection of neat ideas - it's a cohesive whole more or less found around those neat ideas. It's damn hard to take out those ideas and to put them in a language that was not designed around them without corrupting them beyond recognition.
And mind you I label myself as a Python enthusiast, not a Lisper. But I know what my tools can and what my tools can't do.
And yes. This "my language is better than yours" "debate" is completely misguided and utterly brainless. It is also endless fun to have because just about any argument can be made.
One can't start a discussion on how the Apple II was superior to the TRS-80 these days anymore or how the MSX was a soul-less machine designed by a committee. People have to find other topics to start endless passionate discussions.
All we need to do is to keep the discussion civilized, something akin to keeping our playground clean.
2) There is still a large body of "PHP coders" that don't code this way, don't expect others to code this way, and probably won't understand code written thus. The community is the language.
Rather than giving up on the masses of "PHP coders" by labeling them incapable of functional style programming, recursion, etc, why not try to show them? If they're capable of wading through the inconsistencies of PHP they're pretty damn capable in my book. Sure they may lack the rigors of a computer science background but that doesn't mean they're uninterested.
Showing a "PHP coder" a different paradigm of programming in a different language is going to be less effective than trying to express it in the language they know. Personally, I'm really enjoying the challenge of taking the beautiful concepts taken for granted in many other programming language circles and teaching/expressing them in a language understood by the "masses".
However as a former PHP programmer I had very good reasons to give up on the community. The biggest one being the sparseness of quality code. As a PHP developer trying to learn how to write good code you find yourself jumping from island to island of good ideas floating in a sea of crap. The community has the lowest signal-to-noise ratio I've ever seen.
Does the Zend engine do TCO? When I search, Google thinks I mean "Total Cost of Ownership". All I can find is http://devzone.zend.com/article/1235.
Why use lambdas and recursion with PHP when there's tons of languages that do it better?
From the comments here you would think it is bad that writing clean PHP is possible...
The risk of this just making the codebase even uglier is enormous.
So you just generate and then run the code through that if you need it to be human readable. I assume most companies don't give a shit as long as it works ;)
Availability on the target platform (cheap web hosters?), availability of libraries, familiarity with the language (of the entire team, both current and in the future), purpose (aiming to get to the market quickly, or trying to learn a new language?), compatibility or integration with an existing code base, et cetera.
All these deliberations taken together may lead to a decision to use PHP. If so, it is nice to have lambdas and recursion. "It's better in another language" is without meaning in that case, because the feature is so small compared to the other deliberations. It quickly turns into the better question: "is it good enough?"
You can't say PHP is as pretty as scheme when ("false"==0) evaluates to true. It just can't happen.
And if you think I'm some lisp dude slagging PHP, I wrote a PHP framework and work with the garbage everyday.
It's like comparing McDonald's to a Gordon Ramsay restaurant.