表达式被识别为函数

发布于 2025-01-29 04:46:51 字数 706 浏览 5 评论 0 原文

我正在尝试用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 不作为表达式执行?

I am trying to implement some graph algorithms with the OCaml language.
I've made a graph type with a two dimension array which refer to the graph I want to use

And here is my function to get the list of all the point linked to a v 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
;;

I guess it's not clean but I think it's OK.
The problem is when I test it, I'm getting this:

let listeVoisins = voisins g 3;;
val listeVoisins : int list -> int list = <fun>

How can I get a fun type since voisins g 3 should be an int list type expression?
Why my listeVoisins is not executed as an expression?

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

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

发布评论

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

评论(1

久而酒知 2025-02-05 04:46:51

OCAML中的功能是咖喱的。我们的语法使我们可以更方便地与该概念一起工作,但是函数占用一个值并返回单个值。

当然,函数是值,因此函数可以返回另一个函数,而另一个函数又可以返回参数并返回某些内容。它可以返回的函数可以访问创建其创建范围的每个值。研究“关闭”。

考虑您的代码,并重写内部函数以反映这一点:

let voisins graphe v =
  let rec voisinsAux =
    fun tableau ->
      fun i ->
        fun 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

那是不是可观的,但在功能上与您所写的相同。

然后,您仅将两个参数应用于 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:

let voisins graphe v =
  let rec voisinsAux =
    fun tableau ->
      fun i ->
        fun 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

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 an int list) and then calculates the int list result you're looking for.

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