使用 OCaml 实现以下内容
我正在尝试使用 OCaml 实现以下家庭作业:
g(n) = y if n = x else f(n)
所以,例如
if f(n) = n+1, x = 7, y=42, then g(1) = 2, but g(7) = 42
我有:
# let update f x y = f(x) = y;;
val update : ('a -> 'b) -> 'a -> 'b -> bool = < fun>
但我只是不知道应该使用什么语法才能使其返回函数而不是布尔值。
我并不是要求某人解决问题,但如果有人可以发布一些接受函数、操作它并返回它的代码,我将不胜感激。
I'm trying to implement the following with OCaml for a homework assignment:
g(n) = y if n = x else f(n)
so, for example
if f(n) = n+1, x = 7, y=42, then g(1) = 2, but g(7) = 42
I've got:
# let update f x y = f(x) = y;;
val update : ('a -> 'b) -> 'a -> 'b -> bool = < fun>
but I just don't know the syntax I should use in order to make it return a function instead of a boolean value.
I'm not asking for someone to solve the problem, but if someone could post some code that takes in a function, manipulates it, and returns it, I'd appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定我是否理解你的作业——你确定你只是不想调用 f(n) 而不是返回它吗?
但是,一些示例 - 返回另一个函数的函数
请注意,它返回的类型是一个函数:
基本上,要返回一个函数,您可以定义该函数并返回它。
假设您想要接受一个函数并返回一个返回双倍值的函数:
Not sure I understand your homework -- are you sure you just don't want to call f(n) instead of returning it?
But, some examples -- a function that returns another function
Note that the type it returns is a function:
Basically, to return a function, you define the function and just return it.
Let's say you want to take a function and return a function that returns double the value: