Dave Whipp wrote:
I do agree that a prelude.pm should be written atas higher level as
possible, but I would not that Perl6 is not a "declarative" language.
Using the most powerful operators available (I'd like to see more of
them) is about the best you can do: as soon at you start using
codeblocks to define things, you get beyond the realm where compile-time
analysis is possible in a dynamic language.
Lets imagine I want to define a "sqrt($x)" function in a declarative
style in perl6 (and lets image we've defined a Real type with
Real::Epsilon being the precision of the representation). The
declarative version of sqrt must say to find a value within the set of
Real numbers that, when squared, is closest to $x:
sub sqrt(Num where { 0 <= $_ <= Real::Max } $x) {
(0..$x/2 :by(Real::Epsilon)).min: { abs $x - $^candidate ** 2 }
}
So do you really mean "as declarative a manner as possible"? Or would
you consider this example to go beyond "possible"?
I would declare sqrt this way instead (the body is the important change):
sub sqrt(Num where { 0 <= $_ <= Real::Max } $x) {
$x ** (1/2)
}
My point with this example is that any time there exists 2 operators for which
one is a more specialized case of the other, or the other is a more generalized
case of the first, then the specialized one is defined in terms of the
generalized one.
So defining sqrt as just a special case of exponentiation makes for a simple
declarative solution.
I think when I said "define in terms of higher level", I also meant to say
"define in terms of the most generalized operators applicable".
Note also that, as I've defined it, sqrt is still defined in exact terms rather
than approximate / precision-varying terms, and so each underlying
implementation can easily interpret this as an exact math operation if it is
itself capable of exact math, and otherwise it still has enough information to
know how to do it in approximate math.
-- Darren Duncan