MathGroup Archive 1995

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

Search the Archive

Re: Re: Puzzle

  • To: mathgroup at smc.vnet.net
  • Subject: [mg2432] Re: [mg2419] Re: [mg2258] Puzzle
  • From: Allan Hayes <hay at haystack.demon.co.uk>
  • Date: Wed, 8 Nov 1995 23:42:14 -0500

Patrick Jemmer <paddy at sun4.bham.ac.uk>
>Subject: [mg2419] Re: [mg2258] Puzzle
Asks about the following

lst={6, 44, 30, 15, 24, 12, 33, 23, 18};
chk={a___, b_, c___, d_, e___, f_, g___, h_, i___};
cnd= b + d + f + h == 100;
sln={b,d,f,h};

lst/.chk/;cnd->sln
   Condition::obscf:
   Warning: Obsolete use of Condition (chk /; cnd) evaluated to Fail.
{6, 44, 30, 15, 24, 12, 33, 23, 18}

(instead of {44, 15, 23, 18})

Patrick:

Here is a version that works.

   lst/.Evaluate[chk]/;Evaluate[cnd]->sln

      {44, 15, 23, 18}

Explanation:

Consider the full form,
   HoldForm[FullForm[lst/.chk/;cnd->sln]]//Print

     ReplaceAll[lst, Rule[Condition[chk, cnd], sln]]

Condition has attribute HoldAll; so in evaluating
      ReplaceAll[lst, Rule[Condition[chk, cnd], sln]],
the literal Condition[chk, cnd] is looked at before chk and cnd are  
evaluated.
Since chk is not a pattern Condition[chk,cnd] is replaced by Fail (  
had cnd been True it would have evaluated to chk).
This results in ReplaceAll[...] returning lst.

However, it is not sufficient to evaluate only chk: for
ReplaceAll[lst, Rule[Condition[Evaluate[chk], cnd], sln]]
becomes

   ReplaceAll[lst,
      Rule[
         Condition[
            {a___,b_,c___,d_,e___,f_,g___,h_,i___},
            cnd
         ],
         sln
      ]
   ]

But then, when ReplaceAll looks at a match for
 {a___, b_, c___, d_, e___, f_, g___, h_, i___},
literal cnd has no variables a,b,c,d,e,f,g,h,i to receive the  
matches and so cnd remains just cnd and evaluates to b + d + f + h  
== 100, which is never True.
Consequently no replacement is ever made and lst is returned unchanged.
Thus,
   lst/.Evaluate[chk]/;cnd->sln
      {6, 44, 30, 15, 24, 12, 33, 23, 18}

The evaluation can be followed using Trace or TracePrint.

Allan Hayes,
hay at haystack.demon.co.uk



  • Prev by Date: Re: Complex Matrices
  • Next by Date: I don't know....(2)
  • Previous by thread: Re: Puzzle
  • Next by thread: Re: Re: Puzzle