Re: macros

From: Pascal Costanza (costanza_at_web.de)
Date: 08/14/04


Date: Sat, 14 Aug 2004 20:22:40 +0200


Frank Buss wrote:

> Peter Seibel <peter@javamonkey.com> wrote:
>
>>But you could argue that this is really a lisp-1 vs lisp-2 problem. A
>>better example, I think, would be something like with_open_file. I
>>want to be able to write:
>>
>> with_open_file(in, "filename") {
>> /* Do stuff with 'in' */
>> }
>>
>>and have it mean, open "filename" and make 'in' the variable holding
>>the stream and automatically close it when we reach the end of the
>>block.
>
> This is not a better example, at least not for C++, because every C++
> programmer would write it this way, with a local variable:
>
> {
> ifstream in("filename");
> /* Do stuff with 'in' */
> }
>
> At the end of the block, or even at a return within the block, "in" is
> destroyed and the close method is called on it.

I don't think this is a valid counter example, because you are just
reusing a feature that someone else has implemented for you. The
question is how to create that feature in the first place. However, I
think there are again some counter arguments to that position, so I
don't want to go into too much detail here.

Instead, I propose another challenge. How do you implement the following
in C#++@!&^%?

aif int test_result = some_test() {
   ...
} else { ...
}

If the test succeeds (resulting in something other than 0), the first
block is executed. In that block, the variable test_result (or whatever
name the programmer wants) is visible and bound to the test result.
Otherwise, the second block is executed, and that variable is not
visible. Bonus points if you don't need to surround those blocks by
curly braces, but instead can also use single statements. I don't care
if you have to add parentheses or semi-colons at random places to make
this work. ;)

(I don't say this is impossible or only possible in inelegant ways. I
don't know about all the various preprocessing tools for C-based
languages, so it's hard to make such a statement. I'm rather just curious.)

Another challenge has been posted by Guy Steele to the ll1 mailing list
some time ago:

"The goal is to have something that looks like a function that takes a
(numerical) function as an argument and finds its roots, or a root.
Something like this:

  (findroots (lambda (a) (* (sin a) (+ a (* 3 a)))))"

See http://www.ai.mit.edu/~gregs/ll1-discuss-archive-html/msg02088.html
for the full description.

He concludes with "By the way: to fans of Lisp macros, any macro
facility incapable of this particular application is not the true macro
facility."

Pascal

P.S.: Please always keep in mind that these questions are _not_ about
whether something is possible at all. This is always the case due to
Turing equivalence. The question is always: How convenient is it to make
certain features work, and what drawbacks do you add to the rest of the
language by adding some specific feature?

-- 
Tyler: "How's that working out for you?"
Jack: "Great."
Tyler: "Keep it up, then."


Relevant Pages

  • How to insert an existing macro into "Additional Include Directories"?
    ... I am a C++ programmer using VS 2005. ... I encountered a strange proplem ... I can insert an existing macro such as ... I think this is a very convenient feature. ...
    (microsoft.public.vsnet.general)
  • Re: Help! A word feature is trying to drive me insane!
    ... you this behaviour and turn that feature off. ... you can run that macro on a batch of files. ... Rianna wrote: ... Dim myFile As String ...
    (microsoft.public.word.docmanagement)
  • Re: Recent Criticism about Ruby (Scalability, etc.)
    ... can get roughly linear scaling up to 100 times as much scaling needs, ... But you'll also have a pretty limited feature ... programmer productivity will be a better investment for scalability than ... that it was realistic for "many purposes" to assume linear scalability. ...
    (comp.lang.ruby)
  • Re: D40x Mirror Lockup
    ... little less and still get the feature with other manufacturers. ... Anyone that takes macro or astro shots will benefit from MLU. ...
    (rec.photo.digital)
  • Re: Indirect referencing in VB6
    ... > literal variable names and somehow use them to point to the value. ... > programmer before I was a VB programmer so I must have been thinking of ... >> indirection and procedure calls in traditional languages. ...
    (microsoft.public.vb.general.discussion)