Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!apple!bloom-beacon!bu-cs!buengc!art From: art@buengc.BU.EDU (A. R. Thompson) Newsgroups: comp.lang.pascal Subject: Re: C Ternary Conditional Expression? Message-ID: <2962@buengc.BU.EDU> Date: 27 May 89 21:20:04 GMT References: <950025@hpclcdb.HP.COM> <6490002@hplsla.HP.COM> <1344@ruuinf.cs.ruu.nl> <15926@paris.ics.uci.edu> Reply-To: art@buengc.bu.edu (A. R. Thompson) Followup-To: comp.lang.pascal Distribution: na Organization: Boston Univ. Col. of Eng. Lines: 51 In article <15926@paris.ics.uci.edu> Alastair Milne writes: >>... Personally, I would like to write the above as >> >> a[ IF index = last_index THEN last_val ELSE f(index) ] := a[ y ] ; >> >>This would extend the IF-statement to conditional expressions, which in my >>view is a higher-level construct than the conditional statement in Pascal, >>because it allows one to write better readable code. > > Even putting complicated indexing expressions into []'s is enough > seriously to compromise readability. Having an entire statement lodged > in them seems to me even worse. The contention that this code is more > readable seems to me quite unsupported. But it's not a statement it's an expression. It just happens to look like a statement. > > Years ago, ALGOL allowed IF's to return a value, and one could construct > just this sort of thing. Wirth, apparently deliberately, left it out of > Pascal. Before we start wishing to have it back, let's find out why it > has been avoided. If's would not "return" values. What you are discussing is a thing called a "conditional expression". It has the form: "if be then e1 else e2". Where be must be an expression of type boolean and e1 and e2 must be expressions of compatible type and the else part must not be empty. More generally this extends to a "case" expression. The conditional expression appeared in SAIL, a re-worked ALGOL from the Stanford AI lab (SAIL=Stanford Artificial Intelligence Language). It was great fun to program in (lotsa dirty tricks). I don't know why Wirth left them out of Pascal. Probably in keeping with the "simple for teaching" philosophy that was at once Pascal's greatest strength and greatest weakness. I sorely missed them, but since I've discovered Lisp I don't feel quite so lonely. I believe conditional expressions should indeed be part of the language because they are a natural thing to do and reflect quite directly the intention of the programmer. e.g. Doesn't i:= if x<0 then 1 else i+x seem to play better than: if x<0 then i:=1 else i:=i+x ? It does to me. Clearly the net effect is identical (codewise too probably), but the conditional expression says what I mean more directly.