Why Lisp macros are cool, a Perl perspective (2005)
lists.warhead.org.uk
lists.warhead.org.uk
> sqrt($x) = 12;
>and now $x contains 144. No, too bad. That is impossible, or at least extremely difficult. (Maybe you could do something ridiculous, like replacing the built-in sqrt() with a user-defined "lvalue" sub that tied its argument or something. Oy.)
Adding variables to the lhs of an equation, logic programming, is something I'm toying with the last years. I wanted to name it cps, but now use logic sounds better to me. Every primitive op knows about its inverse function, and assignment is a matcher, as in functional programming. perl unfortunately has no matcher, their smartmatch op cannot match structures, it really is a dumbmatch. but that's easily fixable with a few lines of recursive code. there do exist nice prolog systems for lisp, and even a slow one for perl. Currently I'm using picat for such tasks, mixing declarative logic with normal forward expressions. http://www.picat-lang.org/
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. { 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.