       Re: Q: Recursion on a list

• To: mathgroup at smc.vnet.net
• Subject: [mg28924] Re: [mg28886] Q: Recursion on a list
• From: BobHanlon at aol.com
• Date: Fri, 18 May 2001 01:13:31 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```After some additional thought, it is more efficient to write the function as

f[r_List] := FoldList[Times, 1, 1+r];

Bob Hanlon

In a message dated 2001/5/17 7:11:43 AM,  writes:

>The elements of a list are designated by r[[n]], i.e., with Part[r, n],
>
>not r[n], which is a function r evaluated for the argument n.
>
>To generate the complete new list
>
>f[r_List] := FoldList[#1*(1+#2)&, 1, r];
>
>However, to calculate an element of the new list without
>calculating and storing the whole new list
>
>g[r_List, n_Integer?NonNegative] :=
>    Times @@ (1+Take[r, n]);
>
>r = {r1, r2, r3, r4, r5};
>
>f[r]
>
>{1, r1 + 1, (r1 + 1)*(r2 + 1),
>  (r1 + 1)*(r2 + 1)*(r3 + 1),
>  (r1 + 1)*(r2 + 1)*(r3 + 1)*
>   (r4 + 1), (r1 + 1)*(r2 + 1)*
>   (r3 + 1)*(r4 + 1)*(r5 + 1)}
>
>g[r, 4]
>
>(r1 + 1)*(r2 + 1)*(r3 + 1)*(r4 + 1)
>
>f[r] == Table[g[r, k], {k, 0, Length[r]}]
>
>True
>
>For a large list
>
>r = Table[2Random[]-1, {10000}];
>
>Last[f[r]] == g[r, Length[r]]//Timing
>
>{0.9166666666665151*Second, True}
>

```

• Prev by Date: flat box under RealTime3D
• Next by Date: Re: OOP in Mathematica
• Previous by thread: Re: Q: Recursion on a list
• Next by thread: Re: Q: Recursion on a list