[Date Index]
[Thread Index]
[Author Index]
Re: Creating a recursive function which returns a sequence
*To*: mathgroup at smc.vnet.net
*Subject*: [mg128492] Re: Creating a recursive function which returns a sequence
*From*: Bill Rowe <readnews at sbcglobal.net>
*Date*: Thu, 25 Oct 2012 01:40:53 -0400 (EDT)
*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com
*Delivered-to*: l-mathgroup@wolfram.com
*Delivered-to*: mathgroup-newout@smc.vnet.net
*Delivered-to*: mathgroup-newsend@smc.vnet.net
On 10/24/12 at 3:32 AM, wei.xun.lin at gmail.com (Wei-Hsun Lin) wrote:
>I would like to create a recursive function of which the returning
>value is a sequence. For example,
>f[n_]:=Module[{}, If[n<0,Return[Sequence[]]]; Sequence[g[n],f[n-1]]
>]
>The above code doesn't work because the Sequence function was
>evaluated before it's returned.
Have you read the documentation for Sequence? My guess is this
function simply isn't designed to do what you want or perhaps
think it does. The purpose of Sequence in Mathematica is to
convert some expression into a sequence of arguments to be used
by something else. For example, suppose I had
Clear[f];
f[a_, b_, c_] := a b^c
y = c + b;
and I wanted the result
a b^c
then I could use Sequence as follows:
In[4]:= f[a, Sequence @@ y]
Out[4]= a b^c
Assuming the sequence you want returned is a mathematical
sequence, i.e., a list of values, then Nest is the function to
use. For example, here is something that returns the Fibonnaci sequence
In[5]:= g[n_] := Nest[Flatten@{#, Plus @@ #[[-2 ;;]]} &, {1, 1}, n]
In[6]:= g[4]
Out[6]= {1,1,2,3,5,8}
Prev by Date:
**Re: Eigenvalues works very slow**
Next by Date:
**Re: Eigenvalues works very slow**
Previous by thread:
**Re: Creating a recursive function which returns a sequence**
Next by thread:
**How accurate is the solution for high degree algebraic equation?**
| |