![]() |
Quote:
* PHPv3 did indeed suck by current standards, but it did what it was designed to do. V4 was like a teenager learning to drive. Lots of functions are not a bad thing and minor inconsistencies like hyphens and underscores in function names are typical of community-developed anything. |
Quote:
|
Quote:
PHP is perfectly fine once you get used to its oddities, and in some ways its easier to use than a more sophisticated language like Ruby or Perl (both of which are brilliant at making clean, concise code, but which require a lot more mental effort to create or understand). |
Quote:
Quote:
Code:
class StuffPHP Code:
|
Quote:
sorry, that should be Human==Weird. logical equivalence, not assignment... :D |
Quote:
Human === Weird Logical equivalence, both value AND type. :p |
Quote:
For the Ruby version, how about: Code:
class Stuff |
well, the PHP version could be reduced to
PHP Code:
|
Quote:
|
Quote:
But it's not bad enough to make me want to learn Perl :rolleyes: |
Quote:
You're also using indexing in the PHP version (as you did in the C version), so the coefficients have to be an array. The Ruby version only requires the coefficients to be enumerable. (In C++ terminology, you're requiring a random access iterator where an input iterator would have sufficed.) This example is so tiny that it's hard to appreciate the significance of these distinctions. The thing is, the C/PHP version is constructed from smaller mental "chunks" than the Ruby version, so you need more of them. That means it takes more mental effort when reading the program to figure out what it does. It's like the difference between a 1000-piece jigsaw puzzle and a 500-piece jigsaw puzzle. The number of pieces makes a huge difference in the time it takes to assemble the puzzle. |
Quote:
|
Or in other words: yes, you can evaluate a polynomial with only addition/multiplication, but exponents are faster, even if the code looks slicker without them.
|
Quote:
|
On the positive side the giant perl-coded bomb would destroy perl too.
|
Quote:
|
Quote:
|
I was trying not to. :D
In the interest of accuracy, I need to correct myself and point out that php doesn't do exponents as: $a^$b It does them this way: pow($a,$b); One more thing for its detractors to gripe about. :eek: |
Quote:
If you have the sequence a, b, c, d, you can form the sum by injecting addition: a + b + c + d you can form the product by injecting multiplication: a * b * c * d you can form the longitudinal checksum by injecting xor: a ^ b ^ c ^ d or in general, inject an arbitrary binary operator op to get a op b op c op d In each case, these operators associate left-to-right, so the expressions are really evaluated as (((a + b) + c) + d) (((a * b) * c) * d) (((a | b) | c) | d) (((a op b) op c) op d) All you have to do in ruby is tell it which binary operation to inject. The polynomial ax^3+bx^2+cx+d can be more efficiently written as (((ax + b)x + c)x + d) which we get by injecting the operator op defined by v op c == v*x+c. And that's exactly how it's written in ruby: coefficients.inject {|v,c| v*x+c} or in words "given the sequence of coefficients, inject the operator which, on arguments v and c, computes v*x+c. Return the value of that expression." Notice that there's no need to create a loop. We're just building an expression. (Sure, there's a loop going on under the hood, but we don't need to think about that, any more than we need to think about all the dictionary lookups and memory management and subroutine calls that are also going on under the hood, in both languages.) Since we don't introduce a loop, we don't need to grok C's baroque for loop syntax, nor do we need to introduce the auxiliary variable $i whose sole purpose is to keep track of our progress through the loop. No need to count how many coefficients there are. No need to treat the coefficients as an indexable array (as in the expression $coefficient[$i]). In ruby, it's just a sequence, and we're just injecting an operator into it to form an expression. One simple (albeit powerful) concept. Well, OK, two concepts. We're using the fact that lambda expressions are easy to write and easy to use. In ruby they're called blocks; {|v,c| v*x+c} is an example of one. You don't write much ruby without using lots of blocks. Oh, and BTW, multiplication is a LOT faster than exponentiation. |
Sure, one multiplication is faster than exponentiation, but that's not what you have:
a0 + a1x + a2x^2 + a3x^3 + ... + anx^n can be written in your (((ax + b)x + c)x + d) form, but only by using a loop, which is exactly what inject does. The result is many multiplications and many additions. Remember, we don't know how many items exist in the array: coefficient. |
| All times are GMT -5. The time now is 05:34 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.