Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Lazy and infinite sequences / Exercises / Powers of three
Solution: Powers of three
Here is a possible solution to the task.
Code
say (1, * * 3 ... *).head(5);🦋 You can find the source code in the file powers-of-two.raku.
Output
(1 3 9 27 81)Comments
The closure
* * 3is the rule for the next term: take the current one and multiply by three. Giving the rule explicitly is more reliable than letting the operator guess from the first few terms.The
*endpoint makes the series infinite, and.head(5)takes the first five.