如何调用两个函数并将它们的结果用作彼此的参数? [英] How to call two functions and use their results as arguments for each other?

查看:185
本文介绍了如何调用两个函数并将它们的结果用作彼此的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码:

let join a ~with':b ~by:key = 
  let rec a' = link a ~to':b' ~by:key
      and b' = link b ~to':a' ~by:(Key.opposite key) in
  a'

及其编译结果是:

错误:这种表达形式不能作为的右侧使用 `let rec'构建完成

Error: This kind of expression is not allowed as right-hand side of `let rec' build complete

我可以将其重写为:

let join a ~with':b ~by:key = 
  let rec a'() = link a ~to':(b'()) ~by:key
      and b'() = link b ~to':(a'()) ~by:(Key.opposite key) in
  a'()

这是可编译的变体,但是实现的功能是无限递归的,这不是我所需要的.

It is compilable variant, but implemented function is infinitely recursive and it is not what I need.

我的问题:为什么第一次实施无效?如何调用两个函数并将它们的结果用作彼此的自变量?

My questions: Why is first implementation invalid? How to call two functions and use their results as arguments for each other?

我的编译器版本= 4.01.0

My compiler version = 4.01.0

推荐答案

第一个问题的答案在

The answer to your first question is given in Section 7.3 of the OCaml manual. Here's what it says:

非正式地,可接受的定义的类包括那些定义的名称仅在函数体内或作为数据构造函数的参数出现的定义.

Informally, the class of accepted definitions consists of those definitions where the defined names occur only inside function bodies or as argument to a data constructor.

您的名称显示为函数参数,不支持.

Your names appear as function arguments, which isn't supported.

我怀疑原因是您不能指定语义.在我看来,您看到的无限计算通常是无法避免的.

I suspect the reason is that you can't assign a semantics otherwise. It seems to me the infinite computation that you see is impossible to avoid in general.

这篇关于如何调用两个函数并将它们的结果用作彼此的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆