3 ms·
In Perl 6 this is almost trivial: sub sqrt($scalar is rw) { Proxy.new( FETCH => { $scalar }, STORE => -> $, \value {
by lizmat 8y ago
In Perl 6 this is almost trivial:
sub sqrt($scalar is rw) {
Proxy.new(
FETCH => { $scalar },
STORE => -> $, \value {
$scalar = value ** 2
}
)
}
my $x;
say sqrt($x) = 12; # 144
say $x; # 144
I guess this could use some syntactic sugar, but the basic components to do this are readily available and simple to implement.
- rurban 8y agoPartially (that's the perl lvalue solution), but the problem shouldn't be solved by cheating, but by implementing a symbolic logical solver, like computional algebra sytems (Maxima, Sage, ...). To free $x on the left side square both sides. { use logic; sqrt($_) = 12; print solve($_); } => $_ = square(12) = 144; This is usually done by providing prolog-like rules for each and every operation, where many ops could provide more than 1 solution. Then feed them into a solver for more speed. Like with picat or cbmc. Lisp is perfect for that. With perl it gets hairy, but still should be possible.