MathGroup Archive 2002

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

Search the Archive

Re: a question about pattern replacements

  • To: mathgroup at smc.vnet.net
  • Subject: [mg36174] Re: a question about pattern replacements
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Mon, 26 Aug 2002 04:15:51 -0400 (EDT)
  • References: <ak6ope$e9q$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Sidney Cadot" <sidney at jigsaw.nl> wrote in message
news:ak6ope$e9q$1 at smc.vnet.net...
> ............... I came across the
> following behavior which I didn't expect:
>
> z := SomeHead[{{1, 2}, {3, 4}}]
> z /. {SomeHead[q_] -> Flatten[q]}
>
> While this _does_ yield the desired result {1,2,3,4}, Mathematica
complains:
>
> Flatten::"normal": Nonatomic expression expected at position 1 in
> Flatten[q].
>............
> Could anybody explain why this happens?


Sidney,

The clue is that, before any replacing is done,
    z is evaluated to SomeHead[{{1, 2}, {3, 4}}];
    SomeHead[q_] is evaluated to SomeHead[q_]
    Flatten[q] is evaluated to Flatten[q] and the message is generated
saying essentially "nothing to flatten".
*Then* the replacement
    SomeHead[{{1, 2}, {3, 4}}] /. {SomeHead[q_] -> Flatten[q]}
is performed.

It would be better here to use RuleDelayed, :>, instead of Rule, ->,  since
Flatten[q] would not then be evaluated.

    z:=SomeHead[{{1,2},{3,4}}]
    z/.{SomeHead[q_]:>Flatten[q]}

        {1,2,3,4}
--
Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565






  • Prev by Date: Re: a question about pattern replacements
  • Next by Date: RE: Reducing a function to one argument
  • Previous by thread: Re: a question about pattern replacements
  • Next by thread: Re: a question about pattern replacements