MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Creating a recursive function which returns a sequence

  • To: mathgroup at smc.vnet.net
  • Subject: [mg128502] Re: Creating a recursive function which returns a sequence
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Thu, 25 Oct 2012 01:44:15 -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
  • References: <20121024073207.54EC56881@smc.vnet.net>

Clear[f]

f[n_?Negative] := Sequence[];

f[n_?NonNegative] := Sequence @@ {g[n], f[n - 1]};

f[2]

Sequence[g[2], g[1], g[0]]


Bob Hanlon


On Wed, Oct 24, 2012 at 3:32 AM, Wei-Hsun Lin <wei.xun.lin at gmail.com> wrote:
> 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
>



  • Prev by Date: Re: How accurate is the solution for high degree algebraic equation?
  • Next by Date: Re: CDF Security
  • Previous by thread: Re: Creating a recursive function which returns a sequence
  • Next by thread: Re: Creating a recursive function which returns a sequence