Re: Re: ReplaceAll applied to List

• To: mathgroup at smc.vnet.net
• Subject: [mg72422] Re: [mg72320] Re: ReplaceAll applied to List
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Wed, 27 Dec 2006 05:47:11 -0500 (EST)
• References: <em8jdu\$pel\$1@smc.vnet.net> <200612261038.FAA09182@smc.vnet.net>

```One can also use Replace with a level specification instead of
RepalceAll, e.g.:

Replace[{1., {2}, 3},x_?AtomQ :> g,{-1}]

{g,{g},g}

Replace[{1., {2}, 3}, (x_)?( !ListQ[#1] & ) :> g, {-1}]

{g, {g}, g}

Andrzej Kozlowski

On 26 Dec 2006, at 19:38, ben wrote:

> Hi
>
> ReplaceAll using AtomQ is applied to all subexpressions, including
> Thats why a list (head=List) is changed into g[something] with head g.
>
> I do not have an elegant solution, but this should work
>
> mylist = {1, {2}};
> If[ListQ[#], #, f[#]] & /@ mylist
>
> Alternatively
>
> f[x_?NumericQ] := 2x
> f[x_?ListQ] := x
>
> f/@mylist
>
> Bye
> Ben
>
> Hannes Kessler schrieb:
>
>> Hello,
>>
>> I want to apply transformation rules to those elements of a list
>> which
>> are not lists themselves. The structure of the list is unknown in
>>
>> The following simple examples illustrate my my problems with
>> ReplaceAll
>> and List:
>>
>> Expected result:
>> {1., {2}, 3} /. x_?NumericQ :> g
>> --> {g,{g},g}
>>
>> Unexpected result - inspite of AtomQ[{1., {2}, 3}] giving False
>> {1., {2}, 3} /. x_?AtomQ :> g
>> --> g[g, g[g], g]
>>
>> Similarly unexpected result - inspite of Not[ListQ[{1., {2}, 3}]]
>> giving False
>> {1., {2}, 3} /. x_?(Not[ListQ[#]] &) :> g
>> --> g[g, g[g], g]
>>
>> Any explanations for this behavior?
>>