by pg · 17y ago · 0 comments
http://ycombinator.com/arc/arc3.tar
If you want a single, self-contained example, here's one:
(mac zap (op place . args)
(with (gop (uniq)
gargs (map [uniq] args)
mix (afn seqs
(if (some no seqs)
nil
(+ (map car seqs)
(apply self (map cdr seqs))))))
(let (binds val setter) (setforms place)
`(atwiths ,(+ binds (list gop op) (mix gargs args))
(,setter (,gop ,val ,@gargs))))))
How do you implement that in a language without macros? template<typename T>
void zap (T (*f)(T x), T &x) { x = f(x); }
Example: int square (int x) { return x*x; }
int foo[5] = {1,2,3,4,5};
zap<int>(square, foo[3]); // foo is now {1,2,3,16,5}
Admittely, the <int> is a little ugly, but that can probably be fixed with Hindley-Milner (or just plain dynamic typing).You can do the same thing without macros. Granted, the compiler supports by generating code, so it is macro-esque.
arc> (= flag t)
t
arc> (zap no flag)
nil
arc> flag
nil
arc> (= foo '(1 2 3 4 5))
(1 2 3 4 5)
arc> (zap (fn (x) (* x 10))
(foo 0))
10
arc> foo
(10 2 3 4 5)
BTW, in more idiomatic Arc you'd say (zap [* _ 10] foo.0)
Here's a line in HN that uses it: (zap [firstn votewindow* _] (uvar user votes))
This expands into roughly the equivalent of: (= ((profs* user) 'votes)
(firstn votewindow* ((profs* user) 'votes)))
Not quite, though, because you only want to evaluate the code that gets you to the location once, and the operation needs to be atomic. The actual macroexpansion is much more complicated. void zap(void *object, void* (*selector)(void *), void (*action)(void*)) {
action(selector(object));
}
Given your explanation: "It lets you apply an arbitrary function[1] to (a variable or a position anywhere within some structure)[2]."Where object is the structure you're operating on, `selector` is a function taking an object and returning a pointer to the position [2]. The `action` is a pointer to an arbitrary function [1].
Your first example would look like this:
int foo[5] = {1, 2, 3, 4, 5};
void *select_first(void *x) {return x;}
void act_times_10(void *x) {int *i = (int *)x; (*i)*=10;}
zap(foo, select_first, act_times_10);
Or am I missing something you didn't include in the description? void zap(*action, *selector, *object) { ... }
compared to (mac zap (op place . args) ... )
But I also think what the c code really represents is an abstraction. You still need to write out the definitions of the functions each time you call zap. With the lisp code you write one line, which is the whole point of macros: write a single line of code which expands to several lines. (zap [firstn votewindow* _] (uvar user votes))
In Python you wouldn't need a macro for this. You could just write del uvar(user, votes)[votewindow:]
or uvar(user, votes)[votewindow:] = []