MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Controlling evaluation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg83750] Re: Controlling evaluation
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Thu, 29 Nov 2007 06:38:23 -0500 (EST)
  • References: <fiji6s$ikm$1@smc.vnet.net>

magma wrote:
> I am building a package and I need to programmatically construct
> expressions .
> At a certain point I have
> 
> arg1 = {a, b, c}; (* some list *)
> arg2 = {d, e, f}; (* some other list *)
> samelistQ[list1_List, list2_List] :=
>  list1 === list2  (* some logic function *)
> 
> Now I want to construct an expression like
> 
> Condition[expr, samelistQ[arg1, arg2]]
> 
> with arg1 and arg2 evaluated, but with samelist[...] not evaluated.
> In other words, I want to obtain exactly this
> 
> Condition[expr, samelistQ[{a, b, c}, {d, e, f}]]
> 
> where the 2 args have been evaluated, but samelistQ[...] appears
> always unevaluated
> What is expr is not important, just leave it unassigned.
> The problem is that Condition blocks the evaluation of arg1 and arg2
> and of samelistQ.
> 
> Now, doing
> 
> Condition[expr, samelistQ[arg1 // Evaluate, arg2 // Evaluate]]
> 
> does not work, because Evaluate does not disappear.
> So what to do? I tried Hold, ReleaseHold, ect, but it didn't work.
> 
> This is how I solved it
> 
> Condition @@ List[expr, pp[arg1, arg2]] /. pp -> samelistQ
> 
> where pp is an unassigned symbol.
> My question: isn't there a better way, using the functions which are
> supposed to control evaluation (Hold, Evaluate, ect) to achieve this
> result?
> 
> Perhaps a related more general question:
> how do we block
> f[x,y]
> while allowing evaluation of x, and y ?
> Thank you in advance for any comment
> 

Two alternatives:

With[{a = arg1, b = arg2}, Condition[expr, samelistQ[a, b]]]

Block[{samelistQ}, Condition[expr, Evaluate@samelistQ[arg1, arg2]]]

(I prefer the first one, because, unlike the second one, it can be used 
in all similar situations.  Evaluate[] works only if it is at the first 
level in a function with Hold* attributes.)

-- 
Szabolcs


  • Prev by Date: Re: Convert nxn matrix to a column vector with (n^2) elements
  • Next by Date: Re: Controlling evaluation
  • Previous by thread: Re: Controlling evaluation
  • Next by thread: Re: Controlling evaluation