Re: Writing a HoldAll function that evaluates its arguments

*To*: mathgroup at smc.vnet.net*Subject*: [mg86243] Re: Writing a HoldAll function that evaluates its arguments*From*: dh <dh at metrohm.ch>*Date*: Fri, 7 Mar 2008 02:23:59 -0500 (EST)*References*: <fqo8h1$sq9$1@smc.vnet.net>

Hi here is an simple example of a function that evaluates its arguments sequentially, stopping if some condition is met: SetAttributes[myAnd,HoldAll]; myAnd[x__]:=Module[{i,par=Hold[x]}, t=par[[1]]; Do[ If[!(t=And[t,par[[i]]]),t=False;Return[]]; ,{i,2,Length[{x}]}]; t ] myAnd[1==1,2==2,3==2,3==3] hope this helps, Daniel Szabolcs Horvát wrote: > What is the simplest and most elegant way of implementing a function > that has HoldAll, but it still evaluates each of its arguments exactly > once (and does not go into infinite evaluation)? > > I can come up with some ways (explicitly checking whether evaluation > changes the arguments), but neither of them are particularly elegant. > > The function should evaluate like this: > > Attributes[f] === {HoldAll} > f[] --> f[] > f[3] --> f[3] > f[1+2] --> f[3] > f[1,2] --> f[1,2] > f[0+1,1+1] --> f[1,2] > etc. > > > Let me explain my motivation for asking this seemingly absurd question > with an example: > > One built-in function that has this behaviour is And[]. This special > behaviour was needed to make And[] a "short-circuiting" function: it > evaluates the arguments one by one, and it returns False *immediately* > when one argument evaluates to False, without evaluating any further > arguments. > > Szabolcs >