Re: Re-evaluation of Conditional expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg3494] Re: [mg3460] Re-evaluation of Conditional expressions
- From: wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner)
- Date: Mon, 18 Mar 1996 00:56:52 -0500
- Organization: University of Colorado, Boulder
- Sender: owner-wri-mathgroup at wolfram.com
In article <4ibb4f$fep at ralph.vnet.net>, Richard Mercer <richard at seuss.math.wright.edu> wrote: > >Apparently "If" is willing to evaluate one step in search of a True or >False result in spite of the HoldAll attribute. Indeed "If" would be >pretty useless without such a capability. This *appears* to be a >special exception to HoldAll. > >I don't have an explanation for why the HoldAll attribute is there >in the first place. Probably it is like "Plot": it works well in some >cases and not well in others (like yours). It would be interesting to >hear a good case for the use of HoldAll for "If". The HoldAll is necessary for the semantics of If. If "If" didn't have it, then both the "then" and "else" parts would evaluate before the boolean condition was even tested! This is not only inefficient, it is incorrect when the code contains side-effects such as incrementing a counter, printing something, etc. It is also incorrect if the If condition is checking for some sort of erroneous situation, e.g. If[x != 0, 1/x, "help!"] For similar reasons, And, Or, For, While, Switch, Which, etc. all have held arguments. These are not "special exceptions" to HoldAll. HoldAll just means that the arguments don't evaluate *before* the function is called. What happens to them *during* the function call is completely the business of that function. The real question is why If doesn't have the HoldRest attribute instead of HoldAll. Obviously, the first argument must be evaluated in all cases. Dave Wagner Principia Consulting (303) 786-8371 dbwagner at princon.com http://www.princon.com/princon ==== [MESSAGE SEPARATOR] ====