Arc Forumnew | comments | leaders | submitlogin
Regexp name definition
6 points by applepie 5918 days ago | 6 comments
Variable bindings should be regular expressions, being functions on the matched groups.

E.g., this should generalize car, cadr, caddr, etc. for any number of "a"s and "d"s:

  (def /c([ad]*)r/ (x)
    (if
      (is $1 "")
         x
      (is (head $1) "a")
         (head (c{(tail $1)}r x))
      (is (head $1) "d")
         (tail (c{(tail $1)}r x))))
or better yet:

  (def /c([ad]*)r/ (x)
    (if
      (is $1 "")
         x
         ((if (is (head $1) "a")
              head
              tail)
          (c{(tail $1)}r x))))
and even:

  (mac /def(.*)/ (name . rest)
    `(= ,name (,$1 ,@rest)))
thus:

  (defcons x 1 2) === (= x (cons 1 2))
Of course, this should be adapted to a less Perl-like syntax.


4 points by bogomipz 5918 days ago | link

Problem is, you're adding a coupling between the function and the name. No such coupling exists in any lisp that I'm aware of. Conceptually, in Scheme and Arc

  (def foo (x) (prn "it's " x))
is the same as

  (= foo (fn (x) (prn "it's " x)))
i.e. all functions are anonymous from their own pov.

Also, variable lookup is going to be very slow. What you're saying is that the following will make lookup of any name starting with "a" return the same value;

  (= /a.*/ 42)
I'm sure lots of interesting tricks can be done with this, but I'm afraid it adds a lot of complexity only to make very confusing functions possible.

-----

1 point by bramsundar 5918 days ago | link

I think that there are some nice possibilities lurking in this idea. Perhaps there could be a macro defined that modifies def to use this functionality (kind of like pm:def).

-----

2 points by bogomipz 5918 days ago | link

You can't make a macro create an infinite set of global bindings.

-----

3 points by nex3 5918 days ago | link

This seems like it would only be workable, performance-wise, if it were done at compile time. Since Arc doesn't have a terribly rigorous distinction between compilation and interpretation, this might be hard to manage.

-----

1 point by cadaver 5918 days ago | link

Regarding speed and implementation difficulties:

This seems a little like Perl's AUTLOAD.

If an unbound symbol is evaluated, a global AUTLOAD function is called with that symbol as an argument. AUTOLOAD could then parse this symbol and add, at runtime, the necessary functions/variables to the global environment. Invoking AUTOLOAD only on a page-fault basis may not be too inefficient. I have very often seen this done in Perl.

By the way, I like the Perl-like syntax just fine.

-----

1 point by greatness 5918 days ago | link

Yeah, I had the same idea myself, but I eventually decided it wasn't entirely useful because it'd have to match the regular expression before it could make the function call which would be really slow. There are ways around that though, such as differentiating between a regex fn and a normal fn, and first looking to see if a normal definition matches the function call, then iff it couldn't find one, it checks the regular expression function definitions. Additionally, since these regular expressions aren't very large it shouldn't take a significant amount of time to match them; but it would still hurt to have that overhead for something as fundamental as a function call.

-----