[英]Printing int -> int list in ocaml
The function 'succ_di_nodo' prints the successors of a given node in a given graph:函数 'succ_di_nodo' 打印给定图中给定节点的后继节点:
type peso = int;;
type 'a grafo = Gr of (int * peso * int) list;;
let g1 = Gr [(1,3,2);(1,9,5);(2,2,3);(5,4,6);(3,1,6);(3,7,4);(6,2,7);(4,4,6)];;
let rec succ_di_nodo (Gr grafo) nodo =
let rec f_ausiliaria = function
[] -> []
| (x,y,z)::coda ->
if x = nodo then z::f_ausiliaria coda
else if z = nodo then x::f_ausiliaria coda
else f_ausiliaria coda in f_ausiliaria grafo;;
g1 is the Graph itself and the value are (node1,weight,node2). g1 是图本身,值为 (node1,weight,node2)。 When i pass to the function succ_di_nodo the value for example (succ_di_nodo g1 4) it returns correctly: int list = [3;6].当我将值传递给函数 succ_di_nodo 时,例如 (succ_di_nodo g1 4) 它会正确返回:int list = [3;6]。
The question is, when i call the function (succ_di_nodo g1) without giving a specific node it return the type int -> int list;问题是,当我调用函数 (succ_di_nodo g1) 而不给出特定节点时,它返回类型 int -> int list; so it is not an empty list and i want to see the exact return value of this function call.所以它不是一个空列表,我想看到这个函数调用的确切返回值。 How can i print the type int -> int list in Ocaml?如何在 Ocaml 中打印类型 int -> int list?
When you call succ_di_nodo
with only one argument it returns a function that accepts the second argument and returns the list of successors.当您仅使用一个参数调用succ_di_nodo
,它会返回一个函数,该函数接受第二个参数并返回后继列表。 This is called partial application .这称为部分应用。
The int -> int list
type denotes a function that takes one argument of type int
and returns a value of type int list
. int -> int list
类型表示一个函数,它接受一个int
类型的参数并返回一个int list
类型的值。 You can't print a function value (the default printer will just print <fun>
).您无法打印函数值(默认打印机只会打印<fun>
)。 In order to print a function, you would need to enumerate all values in the input domain, which is usually infinite (or close to that).为了打印函数,您需要枚举输入域中的所有值,这通常是无限的(或接近于无限)。 For testing purposes, you can, of course, apply you function to a set of input values, to see what it outputs, eg,出于测试目的,您当然可以将函数应用于一组输入值,以查看它输出的内容,例如,
List.init 10 (succ_di_nodo g1);;
will apply (succ_di_nodo g1)
to 0
, 1
, and so on up to 9
and return the result in a list, ie, the output will be将(succ_di_nodo g1)
到0
、 1
,等等直到9
并在列表中返回结果,即输出将是
[[]; [2; 5]; [1; 3]; [2; 6; 4]; [3; 6]; [1; 6]; [5; 3; 7; 4]; [6]; []; []]
You can also trace your function in a toplevel (ie, when you are running your function in the interpreter), by using the #trace
directive.您还可以使用#trace
指令在顶层(即,当您在解释器中运行您的函数时)跟踪您的函数。 It will show the arguments that go into the function and values that go out.它将显示进入函数的参数和退出的值。 For the sake of experiment try this in the toplevel:为了实验,在顶层尝试这个:
#trace succ_di_nodo;;
(you need to type the leading #
it is a part of the directive name). (您需要输入前导#
它是指令名称的一部分)。 When you will apply succ_di_nodo
to two arguments, you will notice that the function first returns a new function, codenamed succ_di_nodo*
, which is the result of the application of the first argument, and, finally, the second argument is passed to the returned succ_di_nodo*
function.当您将succ_di_nodo
应用于两个参数时,您会注意到该函数首先返回一个新函数,代号为succ_di_nodo*
,这是应用第一个参数的结果,最后将第二个参数传递给返回的succ_di_nodo*
功能。 This process is called currying , which is an essential concept in functional programming.这个过程叫做柯里化,这是函数式编程中的一个基本概念。
# #trace succ_di_nodo;;
succ_di_nodo is now traced.
# succ_di_nodo g1 0;;
succ_di_nodo <--
Gr
[(1, 3, 2); (1, 9, 5); (2, 2, 3); (5, 4, 6); (3, 1, 6); (3, 7, 4);
(6, 2, 7); (4, 4, 6)]
succ_di_nodo --> <fun>
succ_di_nodo* <-- 0
succ_di_nodo* --> []
- : int list = []
#
When you are done, you can untrace a function with the #untrace
directive.完成后,您可以使用#untrace
指令取消跟踪函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.