[BEGINNER] reccurence! and sequence!
helxi via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jul 5 16:38:24 PDT 2017
On Monday, 26 June 2017 at 10:34:22 UTC, ag0aep6g wrote:
> On 06/26/2017 11:51 AM, helxi wrote:
>> [...]
>
> `a` is a tuple of the run-time arguments you pass to
> `sequence`. In this example, no arguments are passed (empty
> parens at the end of the call), so `a` is empty.
>
>> [...]
>
> a[0] = 1
> a[1] = 2
>
>> [...]
>
> `a` isn't used in the string lambda, and it's not considered an
> element of the range. So n starts at 0 and this just prints
> 0+2, 1+2, 2+2, etc.
>
>> [...]
>
> `a` is still not used in the string lambda, but `recurrence`
> uses the values in `a` as the first elements of the range. `n`
> is incremented accordingly (to 1), so this prints:
>
> 1 = a[0],
> 3 = (n = 1) + 2,
> 4 = (n = 2) + 2,
> etc.
>
> Another difference between `sequence` and `recurrence` is that
> `a` always refers to the same initial value(s) in `sequence`,
> while in `recurrence` it gets updated and refers to the
> previous element(s) of the range:
>
> ----
> sequence!((a, n) => a[0] + 1)(1).take(10).writeln;
> // [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
> // because a[0] is always 1
>
> recurrence!((a, n) => a[0] + 1)(1).take(10).writeln;
> // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> // because a[0] refers to the previous value
> ----
Oh thank you. Just 2 follow-up questions:
> recurrence!((a, n) => a[0] + 1)(1).take(10).writeln;
1. In the last example of reccurence, what does n in (a,n) refer
to?
2. How would you chain until! with reccurence? For example I want
to compute 1, 10, 100, ..., (until the value remains smaller than
1000_000)?
More information about the Digitalmars-d-learn
mailing list