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


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

Creating a recursive function which returns a sequence

Started byWei-Hsun Lin <wei.xun.lin@gmail.com>
First post2012-10-24 07:52 +0000
Last post2012-10-24 07:52 +0000
Articles 1 — 1 participant

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


Contents

  Creating a recursive function which returns a sequence Wei-Hsun Lin <wei.xun.lin@gmail.com> - 2012-10-24 07:52 +0000

#12167 — Creating a recursive function which returns a sequence

FromWei-Hsun Lin <wei.xun.lin@gmail.com>
Date2012-10-24 07:52 +0000
SubjectCreating a recursive function which returns a sequence
Message-ID<k686n0$mf6$1@smc.vnet.net>
Hello all,

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.
Could anyone please advise me as to what's is the usual way to achieve
this? Thank you very much!

The result I want is

f[2]
=Sequence[g[2],f[1]]
=Sequence[g[2],Sequence[g[1],f[0]]]
=Sequence[g[2],g[1],f[0]]
=Sequence[g[2],g[1],Sequence[g[0],f[-1]]]
=Sequence[g[2],g[1],g[0],f[-1]]
=Sequence[g[2],g[1],g[0],Sequence[]]
=Sequence[g[2],g[1],g[0]]

Best regards,
Wei-Hsun

[toc] | [standalone]


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


csiph-web