Trouble with Unevaluated
- To: mathgroup at smc.vnet.net
- Subject: [mg109946] Trouble with Unevaluated
- From: Derek Yates <yatesd at mac.com>
- Date: Wed, 26 May 2010 07:07:49 -0400 (EDT)
I think I understand how Unevaluated works, but then get stuck very easily. For example, here is a simplified version of something I am trying to do. I have the following already defined: Revenue[EUR]=4; Revenue[GBP]=7; Revenue[USD]=1; I want to create the definition TotalRevenue:=Revenue[EUR]+Revenue[GBP] +Revenue[USD]. I want it to be a DownValue since Revenue[EUR] may get changed and I want TotalRevenue to automatically update. I also want to create the DownValue programmatically (since the currencies as well as the number of currencies will change and will be an input to my function). My first idea was: assuming I already have currs={EUR,GBP,USD} With[{definition=Unevaluated[Revenue[#]]&/ @currs},TotalRevenue:=definition] but this gives TotalRevenue:={Unevaluated[Revenue[EUR]] +Unevaluated[Revenue[GBP]]+Unevaluated[Revenue[USD]]}. I have to force evaluation using Evaluate/@TotalRevenue every time I wish to use it. (where I used Information[TotalRevenue] to examine the DownValue) I eventually found the following (which works) but feels overly complicated: Block[{f}, With[{vals = f /@ currs}, With[{vals2 = Hold[vals] /. f -> Revenue}, With[{vals3 = Unevaluated @@ vals2}, TotalRevenue := vals3]]]] Hopefully somebody can suggest a more elegant solution.