Writing a HoldAll function that evaluates its arguments
- To: mathgroup at smc.vnet.net
- Subject: [mg86198] Writing a HoldAll function that evaluates its arguments
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Thu, 6 Mar 2008 02:54:56 -0500 (EST)
- Organization: University of Bergen
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