[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: expressions vs. statements




> Date: Tue, 18 Dec 2001 15:45:15 -0800 (PST)
> From: Paul Graham <paulgraham@yahoo.com>
> 
> This is actually a question people on this list might
> have interesting opinions about.  Does distinguishing 
> between expressions and statements buy you anything in 
> expressive power?  I have often wondered about what
> the point of this distinction was...
> 

I had an eye-opening experience not long ago writing some scheme code when
I found a reason to use the return value of a cond expression from within a
function (as opposed to e.g. just returning it from a function); something
like:

  (set! foo (cond ...

This would be impossible if cond was a statement, like "if" in C.  Of
course in C you can use ?:, but then the code becomes unreadable IMO.  I
think the reason for separating statements and expressions (e.g. in Python)
is mainly to simplify the syntax.  Python actually has a glitch whereby "="
doesn't return a value normally but you are allowed to do

  a = b = c = 0

as a special case.  Periodically there is an outcry for a new assignment
operator that will return the value assigned on comp.lang.python, but Guido
is dead-set against it because he (rightfully) hates code like this:

  if (a = getchar())
      # do something

To me, the problem with the above is that there is no explicit boolean type
in python, but now I'm getting way off topic.

Of course, as long as you have a void type, you can always program your own
"statements" just by returning void.  Mzscheme and python both have this,
but the scheme standard doesn't.

Mike