Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!cmcl2!lanl!jlg From: jlg@lanl.gov (Jim Giles) Newsgroups: comp.lang.fortran Subject: Re: Message-ID: <58379@lanl.gov> Date: 28 Jul 90 00:01:45 GMT References: <3250.26b00265@mccall.com> Organization: Los Alamos Natl Lab, Los Alamos, N.M. Lines: 45 From article <3250.26b00265@mccall.com>, by tp@mccall.com: > In article <58237@lanl.gov>, jlg@lanl.gov (Jim Giles) writes: >>> [...] >>> X .GT. Y .OR. L(Z) >> [...] >> Since this started as a comparison to C, let's remember that C is just >> as bad here. You can't predict a-priori whether the function L() will >> be executed in either language. In pretty much any language, it's a >> bad idea to have functions with side-effects in a context like this. > > Not true. In the expression "x > y || l(z)", l will only be called if x is > not greater than y. The definition of the || (or) and && (and) operators > explicitly state that evaluation is left to right, and the right hand side > will only be evaluated if necessary to determine the value of the > expression. [...] Please _read_ people's postings before responding. When I say "you can't predict a-priori whether the function L() will be executed in either language", that's what I mean. If I could predict whether "X>Y" or not _before_ (ie. "a-priori") I do the if-test, I wouldn't bother to test it! > [... lots of garbage about C's 'advantages' because it short-circuits > logical expressions ...] There are two good reasons _not_ to short-circuit logical operators: 1) Mathematically, 'and' and 'or' ('/\' and '\/') are both commutative. They are also both associative. It is important to preserve these properties in a procgamming language. It allows maximum parallel evaluation of conditionals. It allows the use of mathematical principles in program verification. 2) Short-circuiting is a _separate_ operation from logical 'and' or 'or'. Your programming language _should_ implement separate features in distinct ways. The only argument _in_favor_ of having logical operators short-circuit is that you think a terse notation is better than an explicitly written- out notation. Unfortunately, both C and Fortran allow logical operators to short-circuit. This just means that program verification is slightly harder and code doesn't parallelize well (actually, Fortran _allows_ you not to short-circuit, but it's not portable that way). J. Giles