SML 中的无限序列
我必须编写一个接收序列(有限或无限)并返回相同序列的函数,唯一的区别是,如果在序列期间发生异常,则该函数将序列返回到其开头。
换句话说,该函数必须返回一个循环序列,该序列在结束时会重复自身。 我必须用句柄捕获异常。
以下示例必须有效。
- listToSeq [1,2];
val it = Cons (1,fn) : int seq - restartOnError it;
val it = Cons (1,fn) : int seq - tail it;
val it = Cons (2,fn) : int seq - tail it;
val it = Cons (1,fn) : int seq - tail it;
val it = Cons (2,fn) : int seq
有人可以帮助我吗?
I have to code a function that receives a sequence (finite or infinite) and returns an identical sequence with the only difference that if an exception occurs during the sequence then the function returns the sequence to its beginning.
In other words, the function must return a cyclic sequence which repeated itself when it comes to an end.
I have to catch the exception with handle.
The following example must work.
- listToSeq [1,2];
val it = Cons (1,fn) : int seq - restartOnError it;
val it = Cons (1,fn) : int seq - tail it;
val it = Cons (2,fn) : int seq - tail it;
val it = Cons (1,fn) : int seq - tail it;
val it = Cons (2,fn) : int seq
Can someone help me ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的。你有一堆 Cons(int, ->Cons(int, ...)) 的东西(看起来像),并且你想向下递归。观察、学习、思考。当您调用 fn 来生成列表中的下一个 elt 时,您不想直接调用它,而是要处理每次,并在必要时返回到开头。所以,你先写那个 fn 。然后,您需要一个能够将任何 elt 转换为新列表中的内容的小伙子,并提供经过调整的下一个乐趣,让您重新开始。所以,你接下来写那个人(下面第三行)。最后,返回答案即可。简单,代码应该易于理解(伪代码;无法完全编译并且可能有问题)。
Simple. You have a load of Cons(int, ->Cons(int, ...)) things (it looks like), and you want to recurse down. Watch and learn, and think it through. When you call the fn which makes the next elt in the list, you don't want to call it straight, but to handle each time, and go back to the start if you have to. So, you write that fn first. Then, you want a chap that will will tranform any elt into the one in the new list, with the tweaked next fun that sends you back to start. So, you write that guy next (third line below). Finally, just return the answer. Simple, code should be easy to follow (pseudo-code; won't quite compile and may have haskelisms).