有多少种方法可以描述Perl 6中的斐波那契数列? [英] How many ways are there to describe the Fibonacci sequence in Perl 6?

查看:185
本文介绍了有多少种方法可以描述Perl 6中的斐波那契数列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Perl 6中构造懒惰列表的各种方法,我想收集所有描述斐波那契序列的简洁方法.

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.

我将从 masak 的日记中的三个开始:

I will start this off with the three from masak's journal:

my @fibs := (0, 1, -> $a, $b { $a + $b } ... *);

my @fibs := (0, 1, { $^a + $^b } ... *);  

my @fibs := (0, 1, *+* ... *);

我在想类似的方法也可以,但是我认为我的语法有误:

I was thinking something like this would also work, but I think I have the syntax wrong:

my @fibs := (0, 1, (@fibs Z+ @fibs[1..*]));

急切的东西(切片?)使Rakudo进入无限循环.这是Haskell定义的翻译:

Something there is eager (the slice?) and causes Rakudo to enter an infinite loop. It's a translation of the Haskell definition:

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

更新:

好像zipWith示例的问题是@fibs[1..*] slice.如果tail被定义为sub tail (@x) {my $i = 1; {@x[$i++]}...*},则它可以正常工作.我很想知道为什么熟悉乐天(Rakudo)内部原理的人都不会对这种切片感到懒惰.

Seems like the problem with the zipWith example is the @fibs[1..*] slice. if tail is defined as sub tail (@x) {my $i = 1; {@x[$i++]}...*} then it works properly. I would be interested to know why the slice isn't lazy from anyone familiar with Rakudo's internals.

另一个不错的是:

my @fibs := (0, [\+] 1, @fibs);

推荐答案

最短的似乎是

my @fibs := ^2,*+*...*;

这篇关于有多少种方法可以描述Perl 6中的斐波那契数列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆