取无限结构的有限部分
我必须定义一个无限循环者,
enumInts::Cyclist Integer
其中包含按自然顺序排列的所有整数,其中零为当前元素。
我所做的是:
data Cyclist a=Elem (Cyclist a) a (Cyclist a)
enumInts:: Cyclist Integer
enumInts=Elem prev 0 next
where
prev=help2 enumInts 0
next=help1 enumInts 0
-- Create positive part
help1::Cyclist Integer -> Integer -> Cyclist Integer
help1 prev n=present
where present=Elem prev (n+1) next
where next=help1 present (n+1)
-- Create negative part
help2::Cyclist Integer -> Integer -> Cyclist Integer
help2 next n=present
where present=Elem prev (n-1) next
where prev=help2 present (n-1)
它正在编译自己。但我不确定它是否能正常工作......所以我想看看它的结果,例如。 11 个单位。它应该是:-5 -4 -3 -2 -1 0 1 2 3 4 5 值。 可以看到吗? (我知道它是无限的)但是例如。在斐波那契数列中,我们可以使用“取 11 个斐波那契数列”,它给出了它们。这里的选项“take n..”不起作用(嗯或者它起作用,但我不知道如何使用它)。 我会很感激你的帮助..
I have to define an infinite cyclist
enumInts::Cyclist Integer
containing all integers in the natural order with zero being the current element.
What I did is:
data Cyclist a=Elem (Cyclist a) a (Cyclist a)
enumInts:: Cyclist Integer
enumInts=Elem prev 0 next
where
prev=help2 enumInts 0
next=help1 enumInts 0
-- Create positive part
help1::Cyclist Integer -> Integer -> Cyclist Integer
help1 prev n=present
where present=Elem prev (n+1) next
where next=help1 present (n+1)
-- Create negative part
help2::Cyclist Integer -> Integer -> Cyclist Integer
help2 next n=present
where present=Elem prev (n-1) next
where prev=help2 present (n-1)
It is compiling itself. But I'm not sure if it works as it should... so I'd like to see its result for for eg. 11 units. It should be :-5 -4 -3 -2 -1 0 1 2 3 4 5 values.
Is it possible to see it? (I know its an infinite) but for eg. in fibonacci sequence we could use 'take 11 fibs' and it gave them. Here option 'take n..' doesn't work (hmm or it works but i dont know how to use it).
I'd be grateful for your help..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我确信现在已经超过了你的截止日期,所以我很高兴处理双无限整数:
允许有限部分
要制作一个 take 函数,我必须编辑你的类型,以便它可以< /em> 是有限的:
但是现在我们可以看到您的数据类型中有一个错误:
并且
到目前为止看起来还不错,但是:
这不是我们想要的结构 - 它有三个零!
最后有两个
0
,每个数字都有两个。如果我们更深入,情况会更糟,我们不需要中间的所有东西。我们想要的更像是
That's good,但是有太多
Empty
让人困惑。创建一种可以实现您想要的功能的数据类型。
我们真正需要的是一个当前元素,比如向右延伸的列表,以及向后向左延伸的列表。编译器没有方向感,因此我们将为两者使用相同的结构,但请记住将左侧向后打印到右侧。
首先,我们需要一个绝对无限的列表:
现在我们可以使其在两个方向上都是无限的:
看起来像这样:
只有无限多个元素,而不仅仅是 9。
让我们制定一种移动方式。使用
reverse
、toList
和fromList
可以提高效率,但这样你就可以看到如何弄乱各个部分其中:现在,每次我们想要有限时,我们都可以转换为另一种数据类型。
哪个给出,
但您可能想打印它,所以它看起来像您的意思:
哪个给出
当然,我们可以转换为列表并显示它,但我们已经失去了指示我们所在位置的能力。
附录:我如何漂亮地打印树木
有几种不同类型的树木等,所以我给自己一个类将它们转换为树:
我们这样使用:
首先是你的骑自行车者:
接下来是双无限列表:
然后是左右列表:
It's way past your deadline now, I'm sure, so I had fun dealing with the doubly-infinite Integers:
Allowing a finite part
To make a take function, I'll have to edit your type so that it can be finite:
But now we can see a mistake in your data type:
and
It's looking OK so far, but:
That's not the structure we want - it has three zeros in it!
There are two
0
s and two of every number in the end. It's even worse if we go deeperWe don't need all that stuff in the middle. What we want is more like
That's good, but there are so many
Empty
s it's confusing.Making a data type that does what you intend.
What we really need is a current element, somthing like a list stretching out to the right, and something like a list stretching out backwards to the left. The compiler doesn't have a sense of direction, so we'll use the same structure for both but remember to print the left hand side backwards on to the right hand side.
First we need a definitely-infinite list:
Now we can make it infinite in both directions:
Which looks like this:
only with infinitely many elements, not just 9.
Let's make a way of moving about. This could be made more efficient with use of
reverse
,toList
andfromList
, but this way you get to see how you can mess with the parts of it:We can now convert to another datatype every time we want to be finite.
Which gives
but you probably want to print that so it looks like what you mean:
Which gives
Of course we could have just converted to a list and shown that, but we'd have lost the ability to indicate where we were.
Appendix: How I pretty-printed the trees
There are a few different types of trees etc lying about so I gave myself a class to convert them each to Tree:
Which we use like this:
First your Cyclist:
Next the doubly-infinite list:
And then the left-right-list:
当您想要确切的数字时,为什么不直接使用
When you want exactly those numbers, why not just use
虽然它是内置的,但您可以想象列表类型被定义为
take
可以这样定义:您应该尝试看看如何调整
take
的定义适合您自己的类型。Although it's built-in, you can imagine the list type as being defined as
take
can be defined like this:You should try to see how you can tweak the definition of
take
for your own type.我认为这可以解决:
我们的无限数据
从这个列表中获取特定数量的元素:
I think this can be solved as:
Our infinite data
getting specific number of elements from this list:
这是我的解决方案:
您可以这样使用它:
其中:
This is my solution:
and you can use it that way:
where: