ML 中的序列(有限和无限)
好的,
我已经有了序列的下一个定义:
datatype 'a seq = Nil | Cons of 'a * (unit-> 'a seq);
我需要实现下一个函数:
filterq_n:('a -> bool) -> int -> 'a seq -> 'a seq
该函数获取一个返回 true 或 false、n(整数)和序列的谓词函数。
功能:
- 如果 n<=0 返回相同的 seq。
- else 返回一个 seq,其前 n 个元素是谓词为它们返回 true 的原始 seq 中的前 n 个元素,其余的将相同。
例如,如果谓词是 (x mod 2) 并且 seq 是 1,2,3,4,5... 并且 n 是 3,那么新的 seq 是 2,4,6,7,8,...
另外,我应该检查另外 2 个选项:
2.1)如果 seq 是有限的并且少于 n 个元素,谓词为它们返回 true,则新的 seq将仅包含谓词为其返回 true 的元素。
2.2) 如果 seq 是无限的,并且谓词返回 true 的元素少于 n 个,那么新的 seq 将包含谓词返回 true 的所有元素,并且在尝试获取下一个元素时,它将进入无限循环。
我当前的代码从逻辑上讲是暂时规划的,没有考虑 2.1 和 2.2(尽管我遇到错误并且可以找到原因?)
fun filter_n (predicate: 'a -> bool ) (n: int) Nil = Nil
| filter_n (predicate: 'a -> bool ) (n: int) (Cons(x, xf)) =
if(n <= 0) then Cons(x, xf)
else
if predicate x then Cons(x, fn() => filter_n predicate n-1 (xf()) )
else filter_n predicate n-1 (xf())
;
语法错误或巨大的变化..我不确定?
(另外,对于 2.1 和 2.2,我只需要检查是否得到(Nil 和 n>0)然后返回 Nil?)
提前感谢您的帮助。
Okay,
I've got the next definition of sequence:
datatype 'a seq = Nil | Cons of 'a * (unit-> 'a seq);
I need to implement the next function:
filterq_n:('a -> bool) -> int -> 'a seq -> 'a seq
The function gets a predicate function which returns true or false, n (integer) and sequence.
The functionality:
- if n<=0 return the same seq.
- else return a seq that its first n elements are the first n elements in the original seq that predicate returns true for them and the rest will be the same.
For example, if the predicate is (x mod 2) and the seq is 1,2,3,4,5... and n is 3 so the new seq is
2,4,6,7,8,...
In addition, I should check another 2 options:
2.1) if the seq is finite and has less than n elements that predicate returns true for them then the new seq will contain only the elements that predicate returns true for them.
2.2) if the seq is infinite and has less than n elements that predicate returns true for them then the new seq will contain all the elements that predicate returns true for them and when trying to get the next element it will enter to an infinite loop.
My current code logically was planned for now without considering 2.1 and 2.2 (although I get errors and can find why?)
fun filter_n (predicate: 'a -> bool ) (n: int) Nil = Nil
| filter_n (predicate: 'a -> bool ) (n: int) (Cons(x, xf)) =
if(n <= 0) then Cons(x, xf)
else
if predicate x then Cons(x, fn() => filter_n predicate n-1 (xf()) )
else filter_n predicate n-1 (xf())
;
syntax errors or drastic change..I am not sure?
(also, for 2.1 ans 2.2 I just need to check that if I got (Nil and n>0) then return Nil? )
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
n-1
括在括号中,否则它会被解释为多个参数。完成后,它将编译。但是,您的代码中仍然存在逻辑错误:无论谓词是否匹配,您都会减少
n
。然而,规范说您应该选择谓词匹配的n
元素,而不是您应该检查谓词中的n
元素。因此,如果谓词匹配,您应该只减少n
,否则保持相同的n
。一旦解决了这个问题,您的代码应该符合规范(包括 2.1 和 2.2)。
You need to wrap
n-1
in parens, otherwise it's interpreted as multiple arguments. After you did that, it will compile.However there's still a logic error in your code: You're decreasing
n
whether the predicate matches or not. However the specification says that you should selectn
elements where the predicate matches, not that you should check the predicate forn
elements. So you should only decreasen
if the predicate matches and keep the samen
otherwise.Once you've fixed that, your code should meet the specification (including 2.1 and 2.2).