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}