ML 中的序列(有限和无限)

发布于 2024-10-10 19:50:20 字数 1283 浏览 8 评论 0原文

好的,

我已经有了序列的下一个定义:

datatype 'a seq = Nil | Cons of 'a * (unit-> 'a seq);

我需要实现下一个函数:

filterq_n:('a -> bool) -> int -> 'a seq -> 'a seq

该函数获取一个返回 true 或 false、n(整数)和序列的谓词函数。

功能:

  1. 如果 n<=0 返回相同的 seq。
  2. 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:

  1. if n<=0 return the same seq.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春花秋月 2024-10-17 19:50:21

您需要将 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 select n elements where the predicate matches, not that you should check the predicate for n elements. So you should only decrease n if the predicate matches and keep the same n otherwise.

Once you've fixed that, your code should meet the specification (including 2.1 and 2.2).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文