ML 表达式,逐行帮助

发布于 2024-09-28 13:08:25 字数 178 浏览 1 评论 0原文

val y=2;
fun f(x) = x*y;
fun g(h) = let val y=5 in 3+h(y) end;
let val y=3 in g(f) end;

我正在寻找逐行解释。我是机器学习新手,正在尝试破译一些在线代码。另外,对“let/in”命令的描述也会非常有帮助。

val y=2;
fun f(x) = x*y;
fun g(h) = let val y=5 in 3+h(y) end;
let val y=3 in g(f) end;

I'm looking for a line by line explanation. I'm new to ML and trying to decipher some online code. Also, a description of the "let/in" commands would be very helpful.

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

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

发布评论

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

评论(1

蓝天白云 2024-10-05 13:08:25

我对 ocaml 更熟悉,但对我来说看起来都一样。

val y=2;
fun f(x) = x*y;

前两行绑定变量 yfy 为整数 2f 为一个函数,该函数接受整数 x 并将其乘以所绑定的值y2。因此,您可以认为函数 f 接受一些整数并将其乘以 2。 (f(x) = x*2)

fun g(h) = let val y=5
           in
               3+h(y)
           end;

下一行定义了一个函数g,它需要一些h(结果是一个函数)它接受一个整数并返回一个整数)并执行以下操作:

  1. 将整数 5 绑定到临时变量 y
    • 您可以将 let/in/end 语法视为声明可在表达式中使用的临时变量的方法紧随end 只是结束表达式。 (这与省略 end 的 ocaml 形成对比)
  2. 返回 3 加上应用参数 y< 的函数 h 之和/code> 或 5

在较高层次上,函数 g 接受某个函数,将 5 应用于该函数,并将 3 添加到结果中。 (g(h) = 3+h(5))

此时,环境中绑定了三个变量:y = 2f = function< /code> 和 g = 函数

let val y=3
in
    g(f)
end;

现在,3 绑定到临时变量 y 并以函数 f 作为参数调用函数 g。您需要记住,定义函数时,它会保留其环境,因此此处 y 的临时绑定不会影响函数 gf。他们的行为没有改变。

g (g(h) = 3+h(5)),使用参数 f 调用 (f(x) = x*2)。执行参数 h 的替换,g 变为 3+((5)*2),其计算结果为 13

我希望你清楚这一点。

I'm more familiar with ocaml but it all looks the same to me.

val y=2;
fun f(x) = x*y;

The first two lines bind variables y and f. y to an integer 2 and f to a function which takes an integer x and multiplies it by what's bound to y, 2. So you can think of the function f takes some integer and multiplies it by 2. (f(x) = x*2)

fun g(h) = let val y=5
           in
               3+h(y)
           end;

The next line defines a function g which takes some h (which turns out to be a function which takes an integer and returns an integer) and does the following:

  1. Binds the integer 5 to a temporary variable y.
    • You can think of the let/in/end syntax as a way to declare a temporary variable which could be used in the expression following in. end just ends the expression. (this is in contrast to ocaml where end is omitted)
  2. Returns the sum of 3 plus the function h applying the argument y, or 5.

At a high level, the function g takes some function, applies 5 to that function and adds 3 to the result. (g(h) = 3+h(5))

At this point, three variables are bound in the environment: y = 2, f = function and g = function.

let val y=3
in
    g(f)
end;

Now 3 is bound to a temporary variable y and calls function g with the function f as the argument. You need to remember that when a function is defined, it keeps it's environment along with it so the temporary binding of y here has no affect on the functions g and f. Their behavior does not change.

g (g(h) = 3+h(5)), is called with argument f (f(x) = x*2). Performing the substitutions for parameter h, g becomes 3+((5)*2) which evaluates to 13.

I hope this is clear to you.

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