数据类型问题中的标准 ml 函数

发布于 2024-08-20 05:15:08 字数 489 浏览 5 评论 0原文

我必须创建一个有关 peano 数的函数,定义为以下数据类型:

datatype 'a peano = P of ('a -> 'a) * 'a -> 'a
val zero = P(fn (f, x) => x)

我必须实现的函数查找 peano 参数 P(p) 的连续 peano 数。这就是我所写的:

fun suc (P(p)) = case P(p) of P(fn(f,x)=>x) => P(fn(f,x)=>f(x));

问题是我收到这些错误:

stdIn:4.33-4.36 Error: syntax error: deleting FN LPAREN
stdIn:4.43 Error: syntax error found at RPAREN

我不知道我做错了什么。请帮忙!

I have to create a function about peano numbers defined as the following datatype:

datatype 'a peano = P of ('a -> 'a) * 'a -> 'a
val zero = P(fn (f, x) => x)

The function that I have to implement finds the succesive peano number of the peano parameter P(p). This is what I have written:

fun suc (P(p)) = case P(p) of P(fn(f,x)=>x) => P(fn(f,x)=>f(x));

The problem is that i get these errors:

stdIn:4.33-4.36 Error: syntax error: deleting FN LPAREN
stdIn:4.43 Error: syntax error found at RPAREN

I don't know what Im doing wrong. Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

仅此而已 2024-08-27 05:15:08

这段代码有很多问题。编译器抱怨的问题是,您

fn (f,x) => x

case 臂的左侧有一个函数定义,其中只允许使用模式

其他一些问题:

  1. 多余的括号使代码难以阅读(可以提供删除它们的建议)。
  2. 您的 case 表达式是多余的;在函数定义中

     fun suc (P p) = ...
    

    应该可以仅使用 p 进行计算,而无需进行任何案例分析。

  3. 由于 P 带有一个函数,如果你这样写,可能会更容易

     fun suc (P f) = ...
    

    并确保在结果中,f 应用于一对(根据数据类型声明的要求)。

There are a number of problems in this code. The one the compiler is whining about is that you have a function definition

fn (f,x) => x

on the left-hand side of a case arm, where only patterns are permitted.

Some other problems:

  1. Redundant parentheses make the code hard to read (advice is available on removing them).
  2. Your case expression is redundant; in the function definition

     fun suc (P p) = ...
    

    it should be possible just to compute with p without any more case analysis.

  3. Since P carries a function, you will probably have an easier time if you write

     fun suc (P f) = ...
    

    and make sure that in the result, f is applied to a pair (as required by the datatype declarations).

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