OCAML: let func ax = (ax);; 之间的区别并让 func ax = ((fun a -> a) x);;?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣 -> a
是一个匿名身份函数,它总是返回其参数。您可以说:请注意,
第一个
a
和其他两个a
是不同的变量。第一个以后就不再使用了。您可以将这一行重写为更容易理解:你是,这就是问题所在。您将
x
提供给恒等函数,并获取x
返回。您想要做的是将x
提供给您的a
函数,这就是您在第一次尝试中所做的:fun a -> a
is an anonymous identity function, it will always return its parameter. You could say:Note that in your
the first
a
and the other twoa
s are different variables. The first one is never used again later. You can rewrite this line for easier understanding as:You are, and that's the problem. You're feeding your
x
to an identity function, and gettingx
back. What you want to do is feed thex
to youra
function, and that's what you did in your first attempt:对于简单情况下的函数式语言,以类似于 lambda 演算的形式编写表达式并手动进行约简(注意您正在使用哪些约简)通常很有帮助。您仍然可以使用 OCaml 语法作为 lambda 演算的简化版本
因此,在您的示例中,这将变为:
请注意,这些步骤仅用于确保我们稍后不会有具有相同名称的变量,引用不同的变量价值观。这些步骤可以省略,但我个人更喜欢使用唯一变量。
最终结果是
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:
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.
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.