[plt-scheme] Something like $_ in Perl

From: Eduardo Cavazos (proteus at theworld.com)
Date: Sat Mar 1 08:39:03 EST 2003

So, predicate functions are often called in the test portion of a cond
clause. Predicate functions often return a "useful" value instead of just
#t. The problem I see is that when you call a useful-value-returning
predicate directly in the test portion of a cond clause, you cannot use
this returned value in the <expression> section associated with the test.

Take this template for example:

 ((member needle haystack metal-detector)
 ((find fish ocean :key species)

It is natural to place the calls to the predicate functions directly in
the test slots. However, to use the results of member or find, I must
either re-evaluate the expressions in the ... or save the value outside of
the cond via let.

In these situations, I admire and miss the $_ variable in Perl. It would
be nice if the value of <test> in a cond were bound to some variable in
the <expression> portion, such as $_ for example.

The template shown above is simple, and the let-ified version is not a
nightmare to look at. But I sometimes have cond's with many branches, and
complicated predicates, and having a huge set of let bindings preceding
the cond, spacially separates things in a weird, and in my opinion,
arbitrary way. (the previous comment should probably be qualified with a
hairy example... feel free to not understand it).

I don't have much experience with macros, but I imagine that a special
version of cond could be made to do what I am thinking about. But, this
sort of thing isn't in the standard and isn't mentioned in many places, so
I wonder if (the-right-thing? $_) -> #f.

Are my instincts not of the Lambda way? Are my urges impure?

The Puzzled Grasshopper

Posted on the users mailing list.