表达式被识别为函数
我正在尝试用OCAML语言实现一些图形算法。 我已经制作了一个具有二维数组的图形类型,该图像是指我要使用的图形
,这是我的功能,以获取链接到AV Point的所有点的列表:
let voisins graphe v =
let rec voisinsAux tableau i res =
if i = Array.length tableau then
res
else if v != i then
voisinsAux tableau (i + 1) (ajouter res (Array.get tableau i))
else
voisinsAux tableau (i + 1) res
in voisinsAux (Array.get graphe.matrice v) 0
;;
我想这不是干净,但我认为还可以。 问题是当我对其进行测试时,我会得到这个:
let listeVoisins = voisins g 3;;
val listeVoisins : int list -> int list = <fun>
如何获得有趣的类型,因为 voisins g 3
应该是 int list list
类型表达式?
为什么我的 listEvoisins
不作为表达式执行?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OCAML中的功能是咖喱的。我们的语法使我们可以更方便地与该概念一起工作,但是函数占用一个值并返回单个值。
当然,函数是值,因此函数可以返回另一个函数,而另一个函数又可以返回参数并返回某些内容。它可以返回的函数可以访问创建其创建范围的每个值。研究“关闭”。
考虑您的代码,并重写内部函数以反映这一点:
那是不是可观的,但在功能上与您所写的相同。
然后,您仅将两个参数应用于
voisinsaux
,该参数需要三个。这意味着您可以回来的是一个获取一个参数的函数(res
- 显然是int list
)和然后 计算int list
您正在寻找的结果。Functions in OCaml are curried. We have syntax that lets us work with the concept more conveniently, but a function takes a single value and returns a single value.
Of course, functions are values, so a function can return another function which in turn takes an argument and returns something. The function it can return has access to every value that was in scope when it was created. Research "closures."
Consider your code, with the inner function rewritten to reflect this:
That is not pretty to look at, but it's functionally the same as what you wrote.
You've then only applied two arguments to
voisinsAux
which takes three. This means what you've gotten back is a function that takes one argument (res
- apparently anint list
) and then calculates theint list
result you're looking for.