       Re: Creating a recursive function which returns a sequence

• To: mathgroup at smc.vnet.net
• Subject: [mg128498] Re: Creating a recursive function which returns a sequence
• From: Sseziwa Mukasa <mukasa at gmail.com>
• Date: Thu, 25 Oct 2012 01:42:54 -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>

```Flatten does what you need.  Also there's no need to use Return:

(Debug) In:= f[n_] := Module[{}, If[n < 0, {},
Flatten[{g[n], f[n - 1]}]]]
(Debug) In:= f
(Debug) Out= {g, g, g}

Sequence makes no sense outside of a function, so you can just do Sequence@@f[n] when you want to insert the result as the argument of another function.

Regards,
Sseziwa

On Oct 24, 2012, at 3:32 AM, Wei-Hsun Lin 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
> =Sequence[g,f]
> =Sequence[g,Sequence[g,f]]
> =Sequence[g,g,f]
> =Sequence[g,g,Sequence[g,f[-1]]]
> =Sequence[g,g,g,f[-1]]
> =Sequence[g,g,g,Sequence[]]
> =Sequence[g,g,g]
>
> Best regards,
> Wei-Hsun
>

```

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