Prev: Elephant experiences?
Next: [ANN] ECL 10.3.1
From: Barry Margolin on 5 Mar 2010 21:05 In article <hmr7kq$5tc$1(a)news.eternal-september.org>, Joshua Taylor <tayloj(a)cs.rpi.edu> wrote: > This is getting a little bit away from the OP's OQ, but no matter. One > of the techniques I've started using over the past year or two is > writing function-passing versions of things first, and then writing a > macro wrapper that expands into the function-passing version. E.g., I learned this technique 20 years ago from Symbolics's table and presentation macros. One of the nice things about it is that you don't have to use GENSYM to get hygiene or ONCE-ONLY to ensure that expressions are only evaluated once. -- Barry Margolin, barmar(a)alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group ***
From: Mirko on 6 Mar 2010 07:22 On Mar 4, 1:25 pm, "Alex Mizrahi" <udode...(a)users.sourceforge.net> wrote: > M> I would like to use a macro to expand into a function body, but it > M> needs to have a declare statement at the top. Here is the macro: > > M> (defmacro dist (fun) > M> `(progn > M> (declare (number lo hi)) > M> (coerce lo 'double-float) > M> (coerce hi 'double-float) > M> (make-marray 'double-float > M> :initial-contents > M> (coerce (,fun lo hi count) 'list)))) > > Version which could actually make sense: > > (defmacro defdist (name paramlist fun) > `(defun ,name paramlist > (declare (number lo hi)) > (make-array 'double-float > :initial-contents (coerce > (,fun (coerce lo 'double-float) > (coerce hi 'double-float) > count) > 'list)))) > > M> and one example of its use: > > M> (defun useq (lo hi &optional (count 51)) > M> (dist my-utils:rseq)) > > (defdist useq (lo hi &optional (count 51)) my:utils:rseq) > Aaah, perfect. You actually answered a question that has been nagging me for some time now. Thank you, Mirko
From: Mirko on 6 Mar 2010 07:26 On Mar 4, 12:49 pm, Tamas K Papp <tkp...(a)gmail.com> wrote: > On Thu, 04 Mar 2010 09:40:39 -0800, Mirko wrote: > > Hello, > > > I would like to use a macro to expand into a function body, but it needs > > to have a declare statement at the top. Here is the macro: > > > (defmacro dist (fun) > > `(progn > > (declare (number lo hi)) > > (coerce lo 'double-float) > > (coerce hi 'double-float) > > (make-marray 'double-float > > :initial-contents > > (coerce (,fun lo hi count) 'list)))) > > > and one example of its use: > > > (defun useq (lo hi &optional (count 51)) > > (dist my-utils:rseq)) > > > The function compilation fails, because the `declare' form is not the > > first form in the function body. It is nested behind `progn' > > > 1) how to accomplish this with a macro? 2) Or should I use a function > > instead, passing `fun' as an argument? > > stuff deleted > > Finally, declaring something to be NUMBER is unlikely to be a > significant source of improvement in most implementations. I would > just drop that declaration altogether. You are right. The coerce forms would catch any non-numbers, anyway (I think that touches on Graham's discussion on thin utilities that do not need their own error catching mechanism. > > You tried to trade a concise and understandable function for an > obfuscated macro that doesn't buy you anything. There is a lesson in > this. I guess that you are right in case of a single macro invocation. But I was defining multiple functions on the same boilerplate, and I was trying to eliminate cutting & pasting, and thus possibilities to introduced editorial errors. Alex in the following post showed nicely how to do it. Thanks, Mirko
From: Tim Bradshaw on 6 Mar 2010 12:59
On 2010-03-06 12:26:59 +0000, Mirko said: > You are right. The coerce forms would catch any non-numbers, anyway > (I think that touches on Graham's discussion on thin utilities that do > not need their own error catching mechanism. Declaring things can be for people as well as the machine: just because a declaration does not make it faster is not a reason for not having it (it may not be useful either to human or machine in this case in particular, of course). |