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


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

Re: Creating a recursive function which returns a sequence

Started byBill Rowe <readnews@sbcglobal.net>
First post2012-10-25 05:40 +0000
Last post2012-10-25 05:40 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

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

#12170 — Re: Creating a recursive function which returns a sequence

FromBill Rowe <readnews@sbcglobal.net>
Date2012-10-25 05:40 +0000
SubjectRe: Creating a recursive function which returns a sequence
Message-ID<k6ajct$r2a$1@smc.vnet.net>
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}

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web