For Programmers: Free Programming Magazines  


Home > Archive > Mathematica > December 2006 > Re: ReplaceAll applied to List









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: ReplaceAll applied to List
dimitris

2006-12-20, 8:16 am

Everything is normal. Look at the following example

In[18]:=
{2.3, 4, {7/8}, a, b, I, {{I^2}}} /. x_ /; NumberQ[x] -> x^2

Out[18]=
{5.289999999999999, 16, {49/64}, a, b, -1, {{1}}}

The rule is applied to all the levels of the expression. But

In[19]:=
Cases[{2.3, 4, {7/8}, a, b, I, {{I^2}}}, x_ /; NumberQ[x] -> x^2]

Out[19]=
{5.289999999999999, 16, -1}

Only at level one here.

Regards
Dimitris



Hannes Kessler wrote:
> 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
> advance.
>
> 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?
>
> Thanks in advance,
> Hannes Kessler


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com