'if' as a _function_, in Scheme and Python

Barry Margolin barmar at bbnplanet.com
Fri Aug 20 13:01:31 EDT 1999


In article <37beeec3.31761662 at 90.0.0.40>,
Steve Schafer <pandeng at telepath.com> wrote:
>On Fri, 20 Aug 1999 00:24:25 GMT, Barry Margolin
><barmar at bbnplanet.com> wrote:
>
>>No.  Remember that then-branch and else-branch are thunks.  It doesn't
>>matter whether you evaluate them, they don't do anything until you *call*
>>them.
>
>Yes, but that doesn't negate what I said--it's not equivalent to IF
>(since IF doesn't require that you wrap the expressions in thunks). 

It's not syntactically equivalent, it's another way of accomplishing the
same thing.

>Essentially, you're changing the problem to fit your solution. I guess
>I don't really see any point in doing that.

This is a discussion about language design: does "if" need to be a special
form, or can it be constructed out of other, existing primitives?  The
answer is that it can, with the only special form required being "lambda".
Yes, it means that it has to be called a little differently in the
theoretical variant of Scheme that doesn't have an "if" primitive; this can
be hidden using a macro, though.

A note about the original poster's implementation, though.  IIRC, it used
"assq" to select the appropriate thunk to call.  Can "assq" be written
without using "if"?  Although "assq" is a required language primitive, it's
a pretty high-level function and would usually be expected to be
implemented in the high-level language.  My version of "if" that used the
"if-true" and "if-false" functions really doesn't depend on anything other
than "lambda" and function calling.

-- 
Barry Margolin, barmar at bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.




More information about the Python-list mailing list