admin管理员组

文章数量:1122832

(I am learning the OCaml and this may be a naive question.)

I use below code to define a named function a:

utop # let a = fun x -> x+1;;
val a : int -> int = <fun>

Note that the leading val a clearly shows up as the name of the function.

Then I tried to see the type info of the function a:

utop # a;;
- : int -> int = <fun>

There's a leading -, which means anonymous.

But I have given it a name a.

Why is it not displayed?

(I am learning the OCaml and this may be a naive question.)

I use below code to define a named function a:

utop # let a = fun x -> x+1;;
val a : int -> int = <fun>

Note that the leading val a clearly shows up as the name of the function.

Then I tried to see the type info of the function a:

utop # a;;
- : int -> int = <fun>

There's a leading -, which means anonymous.

But I have given it a name a.

Why is it not displayed?

Share Improve this question edited yesterday smwikipedia asked yesterday smwikipediasmwikipedia 64k96 gold badges326 silver badges503 bronze badges 2
  • Would you expect the REPL to repeat whatever expression you gave it on the left of the :? That would be redundant. – Naïm Favier Commented yesterday
  • While the variable a has a name, the value that a is bound to (which is what gets printed) does not; it is just a function. This is not specific to functions – if you let a = 1;;, then a;; shows - : int = 1. – molbdnilo Commented 11 hours ago
Add a comment  | 

1 Answer 1

Reset to default 1

The REPL reads the line

a;;

as an anonymous toplevel expression, which returns a value of type int->int. This is the same behaviour as you see with

Array.iteri (fun n x -> a.(n) <- x + 1) a
- : unit = ()

with a slightly unusual return type for the expression.

If you want to have information on a value, you can use the #show toplevel directive:

#show a;;
val a: int -> int

本文标签: ocamlWhy a named function is still displayed as anonymous in utopStack Overflow