How many ways are there to describe the Fibonacci sequence in Perl 6?

后端 未结 2 1745
旧巷少年郎
旧巷少年郎 2021-02-01 05:12

I\'ve been looking at the various ways of constructing lazy lists in Perl 6 and I would like to collect all of the concise ways of describing the Fibonacci sequence.

I w

相关标签:
2条回答
  • 2021-02-01 06:02

    The shortest seems to be

    my @fibs := ^2,*+*...*;
    
    0 讨论(0)
  • 2021-02-01 06:15

    You can use the magic of the golden ratio: let φ=(sqrt(5)+1)/2, and define fib(n)=(φn+(1-φ)n)/sqrt(5).

    You can convert such a function into a lazy list in the obvious way: In Haskell the following works:

    fibs=genfibs 0 where genfibs n=(round (fib n)):genfibs (n+1)
    

    I'm afraid my Perl 6 knowledge isn't up to translating this, sorry! Anyone who edits this answer to edit in the codes will earn my gratitude.

    A more testing question would be to list ways of generating the lazy list of Hamming numbers.

    0 讨论(0)
提交回复
热议问题