Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #12170

Re: Creating a recursive function which returns a sequence

From Bill Rowe <readnews@sbcglobal.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Creating a recursive function which returns a sequence
Date 2012-10-25 05:40 +0000
Message-ID <k6ajct$r2a$1@smc.vnet.net> (permalink)
Organization Time-Warner Telecom

Show all headers | View raw


On 10/24/12 at 3:32 AM, wei.xun.lin@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}

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Creating a recursive function which returns a sequence Bill Rowe <readnews@sbcglobal.net> - 2012-10-25 05:40 +0000

csiph-web