Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

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

Search the Archive

Re: Pattern matching

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116218] Re: Pattern matching
  • From: Roland Franzius <roland.franzius at uos.de>
  • Date: Mon, 7 Feb 2011 06:05:07 -0500 (EST)
  • References: <iilth1$2mk$1@smc.vnet.net>

Am 06.02.2011 11:35, schrieb StatsMath:
> Have a question regarding the applicaiton of pattern rules:
>
> fact[4] //. {fact[n_] :>  n fact[n-1], fact[0] ->  1}
>
> This is a bug since fact[0] needs to be defined 1st, but I expected
> the above to grind away idefnitely but it returned a a value 0,
> instead of an infininte computation.
>
> Can you help me understand why the above returns 0?

Using

Trace[fact[4] //. {fact[n_] :> (Print["a:", n]; n fact[n - 1]),
    fact[0] :> (Print["b:", n]; 1)}]

you see the second rule is never used. The first rule at

fact[0] -> 0 fact[-1]

evaluates the expression to 0 and there is no further rule for "0".

-- 

Roland Franzius


  • Prev by Date: Re: Pattern matching
  • Next by Date: Re: Pattern matching
  • Previous by thread: Re: Pattern matching
  • Next by thread: Re: Pattern matching