OCaml:没有定义值的柯里化

发布于 2024-08-30 07:43:11 字数 211 浏览 5 评论 0原文

我有两个函数 f 和 g,我试图返回 f(g(x)) 但我不知道 x 的值,而且我不太确定如何处理这个问题。

更具体的例子:如果我有函数 f = x + 1g = x * 2 并且我试图返回 f(g(x)) 我应该得到一个等于 (x*2) + 1 的函数

I have two functions f and g and I am trying to return f(g(x)) but I do not know the value of x and I am not really sure how to go about this.

A more concrete example: if I have functions f = x + 1 and g = x * 2 and I am trying to return f(g(x)) I should get a function equal to (x*2) + 1

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

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

发布评论

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

评论(1

满地尘埃落定 2024-09-06 07:43:11

看起来你是对的,f(g(x)) 应该可以正常工作。我不知道为什么你有一个 return 关键字(它不是 ocaml 中的关键字)。这是一个正确的版本,

let compose f g x = f (g x)

其类型定义是,

val compose : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c = <fun>

每个,'a,'b,'c 都是抽象类型;我们不关心它们是什么,只需要在定义上保持一致即可(因此,g 的定义域必须在 f 的范围内)。

let x_plus_x_plus_1 = compose (fun x -> x + 1) (fun x -> x * 2) 

It looks like you have it right, f(g(x)) should work fine. I'm not sure why you have a return keyword there (it's not a keyword in ocaml). Here is a correct version,

let compose f g x = f (g x)

The type definition for this is,

val compose : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c = <fun>

Each, 'a,'b,'c are abstract types; we don't care what they are, they just need to be consistent in the definition (so, the domain of g must be in the range of f).

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