[Nickle]Array comprehension syntax
Keith Packard
nickle@nickle.org
Sat, 03 Aug 2002 20:18:33 -0700
Array comprehensions are lambdas evaluated to initialize an array; they're
passed the index of the desired element and compute that element.
Essentially, they're a shorthand notation for:
int[*,*] a = [2, 2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
a[i,j] = lambda (i,j);
I've got the underlying code all built for compiling and executing these,
but I'm a bit stuck on the syntax. My current (lame) syntax looks like:
int[*,*] a = [2, 2] { = int func (int i, int j) { return i*j; } };
Actually, any expression which evaluates to a function can be used inside
the initializer:
int c (int i, int j) { return i * j; }
int[*,*] a = [2, 2] { = c };
I'm really not excited about this syntax and am looking for suggestions.
However, even with this weak syntax, compose looks a lot nicer:
poly (poly...) compose (poly f, poly g) {
return poly func (poly args...) {
poly(int) pick(int start) {
return func (int i) { return args[start+i]; };
}
return f (g ((poly[func_args(g)]) { = pick(0) } ...),
(poly[func_args(f)-1]) { = pick(func_args(g)) }...);
};
}
The only problem is that the arguments 'f' and 'g' aren't statically
typechecked to accept only functions. We don't have any syntax for
a function which takes an arbitrary number of arguments, all we have
is syntax for a function which takes a variable number of arguments;
the problem is that:
poly(poly...) a = poly func (poly g) { return g; }
is currently a type error (Incompatible types 'poly(poly ...)', 'poly(g)')
We need a formal spec for our current type system...
-keith