Hacker News new | comments | show | ask | jobs | submit login
Why Lisp macros are cool, a Perl perspective (2005) (warhead.org.uk)
24 points by btilly 64 days ago | hide | past | web | favorite | 6 comments

>Maybe you would like this to work in Perl:

> 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/

In Perl 6 this is almost trivial:

    sub sqrt($scalar is rw) {
          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.

Partially (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.

Pretty good read, but it's from a mailinglist, and is littered with quoted-printable residue. If someone wants to preserve this, it would be worth converting it to a readable form.


Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact