Re: Problems with recursive function
- To: mathgroup at smc.vnet.net
- Subject: [mg87375] Re: Problems with recursive function
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 9 Apr 2008 05:53:13 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <ftfela$bv1$1@smc.vnet.net>
Frank Hechtner wrote: > I'm in trouble with a recursive function. the functios looks similar to > test[a_, b_] := test[a - 1, b]*b. A is the index b should just be an > argument of test. > for any calculation i need to tell mathematica the first value of the > function, e.g. test[0, 5]=5. The problem is the following: the first > object is independent of b. does anyone know a way to tell mathematica > the following thing: > > test[0,b]=5 => test[0]=5 regardless to b. Hi Franck, You were almost there: just use the blank pattern (single underscore) in place of b. In the example below, I have added a test to prevent infinite recursion and also memoization to improve performances (in case of). Clear[test] test[a_Integer?Positive, b_] := test[a, b] = test[a - 1, b]*b test[0, _] = test[0] = 5; test[0] (* returns 5 *) test[0, 3] (* returns 5 *) test[2, 3] (* returns 45 *) test[0, 1, 2, 3] (* returns unevaluated test[0, 1, 2, 3] *) test[3] (* returns unevaluated test[3] *) test[-1, 3] (* returns unevaluated test[-1, 3] *) Best regards, -- Jean-Marc