OCAML: let func ax = (ax);; 之间的区别并让 func ax = ((fun a -> a) x);;?

发布于 2024-12-11 12:10:18 字数 378 浏览 0 评论 0原文

我对 OCAML 的这项作业有点困惑。我试图将一个函数和一个值作为参数传递到另一个函数中。例如,我有一个名为 test 的函数,它接受 (fun x -> x+x) 和 3 作为参数。函数测试应该输出 6,因为 3 + 3 = 6。我知道我可以通过完成来实现类似的效果:

let func x = x + x;;

let a = func;;

let test a x = (a x);;

这样当我输入时,测试 5,我将得到 10。

但是当我将语句更改为这样时,我只获取我为 x 设置的值。如何让 (fun a -> a) 接受值 x?

让测试 ax = ((fun a -> a) x);;

I'm a little stuck on this assignment for OCAML. I'm trying to pass in a function and a value as a parameter into another function. For example, I have function called test that takes in (fun x -> x+x) and 3 as parameters. The function test should output 6, since 3 + 3 = 6. I know I can achieve something similar this by completing:

let func x = x + x;;

let a = func;;

let test a x = (a x);;

This way when I input, test a 5, I will get 10.

But when I change the statement to this, I get only the value I placed in for x. How do I get the (fun a -> a) to take in the value x?

let test a x = ((fun a -> a) x);;

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

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

发布评论

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

评论(2

浮光之海 2024-12-18 12:10:18

有趣 -> a 是一个匿名身份函数,它总是返回其参数。您可以说:

let id = fun a -> a;;
id 3;;
  => 3
id (fun x -> x + x);;
  => (fun x -> x + x)

请注意,

let test a x = ((fun a -> a) x);;

第一个 a 和其他两个 a 是不同的变量。第一个以后就不再使用了。您可以将这一行重写为更容易理解:

let test a x = ((fun b -> b) x);;

如何让 (fun a -> a) 接受值 x?

你是,这就是问题所在。您将 x 提供给恒等函数,并获取 x 返回。您想要做的是将 x 提供给您的 a 函数,这就是您在第一次尝试中所做的:

let func x = x + x;;
let test a x = a x;;
test func 3;;
  => 6

fun a -> a is an anonymous identity function, it will always return its parameter. You could say:

let id = fun a -> a;;
id 3;;
  => 3
id (fun x -> x + x);;
  => (fun x -> x + x)

Note that in your

let test a x = ((fun a -> a) x);;

the first a and the other two as are different variables. The first one is never used again later. You can rewrite this line for easier understanding as:

let test a x = ((fun b -> b) x);;

How do I get the (fun a -> a) to take in the value x?

You are, and that's the problem. You're feeding your x to an identity function, and getting x back. What you want to do is feed the x to your a function, and that's what you did in your first attempt:

let func x = x + x;;
let test a x = a x;;
test func 3;;
  => 6
断爱 2024-12-18 12:10:18

对于简单情况下的函数式语言,以类似于 lambda 演算的形式编写表达式并手动进行约简(注意您正在使用哪些约简)通常很有帮助。您仍然可以使用 OCaml 语法作为 lambda 演算的简化版本

因此,在您的示例中,这将变为:

   let test a x = ((fun a -> a ) x);;
=> let test a x = ((fun b -> b ) x);; (* Variable renamed (alpha equivalence) *)
=> let test a y = ((fun b -> b ) y);; (* Variable renamed (alpha equivalence) *)

   let func x = x + x;;

请注意,这些步骤仅用于确保我们稍后不会有具有相同名称的变量,引用不同的变量价值观。这些步骤可以省略,但我个人更喜欢使用唯一变量。

   test func 5
=> test (fun x -> x + x) 5 (* Variable func inserted (beta reduction) *)
=> (fun a y -> ((fun b -> b) y) (fun x -> x + x) 5 (* Variable test inserted *)
=> (fun y -> (fun b -> b) y) 5 (* Variable a inserted *)
=> ((fun b -> b) 5 (* Variable y inserted *)
=> 5 (* Variable b inserted *)

最终结果是5。一开始尝试这样做会显得非常不寻常和困难,但很快就会变得容易。如果您多次执行类似的操作,您将更好地理解常见的功能模式并推理程序结构。

请参阅本文了解更多相关示例。

另请注意,只要多一点努力,这也可以向后进行。尽管这通常不如按照与编译器相同的方向进行那么有帮助。

With functional languages in simple cases it is often helpful to write the expression in a form similar to lambda calculus and do the reductions manually (noting which reductions you are using). You can still use OCaml syntax as a simplified version of lambda calculus

So in the case of your example this would become:

   let test a x = ((fun a -> a ) x);;
=> let test a x = ((fun b -> b ) x);; (* Variable renamed (alpha equivalence) *)
=> let test a y = ((fun b -> b ) y);; (* Variable renamed (alpha equivalence) *)

   let func x = x + x;;

Note that these steps only serve to make sure, that we will later on have no variables with the same name, referring to different values. These steps can be left out, but I personally like working with unique variables much better.

   test func 5
=> test (fun x -> x + x) 5 (* Variable func inserted (beta reduction) *)
=> (fun a y -> ((fun b -> b) y) (fun x -> x + x) 5 (* Variable test inserted *)
=> (fun y -> (fun b -> b) y) 5 (* Variable a inserted *)
=> ((fun b -> b) 5 (* Variable y inserted *)
=> 5 (* Variable b inserted *)

The final result is 5. Attempting this at first will seem very unusual and hard, but get's easier very fast. If you do something like this a couple of time you will get much better at understanding common functional patterns and reasoning about your program structure.

Have a look at this article for more examples on this.

Also note, that with a little more effort, this works backward as well. Although this usually is not as helpful as doing it in the same direction as the compiler.

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