Re: conditionnal rule
- To: mathgroup at smc.vnet.net
- Subject: [mg51536] Re: conditionnal rule
- From: Curt Fischer <crf3 at po.cwru.edu>
- Date: Thu, 21 Oct 2004 22:21:01 -0400 (EDT)
- References: <cl4t4i$8ee$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Wishmaster7 wrote:
> hi all !!
>
> I want to apply a rule under a certain condition. here is the example :
>
> myList = {{a, b}, {c, d}};
>
> myList //. {a,x_}->{a,d}
>
> apply this rule if Length[x]===1 (this means that x can not be a list)
Here is one solution to your present example. You will also want to
investigate the Condition operator /; and the pattern test operator ?.
Note that in Mathematica, a list can have length one, or zero, so your
Length criterion is not the same as testing whether something is a list
or not.
In[1]:=
?ListQ
ListQ[expr] gives True if expr is a list, and False otherwise.
In[2]:=
?Not
!expr is the logical NOT function. It gives False if expr is \
True, and True if it is False.
In[3]:=
myList={{a,b},{c,d}}
Out[3]=
{{a,b},{c,d}}
In[4]:=
myRule={a,x_?(Not[ListQ[#]]&)}\[Rule] {a,d}
Out[4]=
{a,x_?(!ListQ[#1]&)}\[Rule]{a,d}
In[5]:=
myList//.myRule
Out[5]=
{{a,d},{c,d}}
> I tried something like :
>
> myList //. {a_,x_}->{c,d}/;Length[x]===1
>
> or
>
> myList //. {a_,x_?Length[x]===1}->{c,d}/
>
> but it does not work.
>
> can someone help me ?
>