在 Ocaml 中,如果我不想使用变量,如何避免未使用的变量警告?

use*_*343 2 ocaml

假设我正在列表上定义一个递归函数,如下所示:

let rec listFunc xs =
    match xs with
    | [] -> aThing
    | h :: t -> listFunc (tailFunc t)
;;
Run Code Online (Sandbox Code Playgroud)

其中tailFunc是从列表到列表的其他函数。编译器会给我一个未使用的变量警告,因为我没有使用h,但我不能只使用通配符,因为我需要能够访问列表的尾部。如何防止编译器向我发出警告?

Rap*_*nah 5

您可以h使用下划线作为前缀。

let rec listFunc xs =
    match xs with
    | [] -> aThing
    | _h :: t -> listFunc (tailFunc t)
;;
Run Code Online (Sandbox Code Playgroud)

或者简单地:

let rec listFunc xs =
    match xs with
    | [] -> aThing
    | _ :: t -> listFunc (tailFunc t)
;;
Run Code Online (Sandbox Code Playgroud)

任何以 开头的绑定都_不会在范围内,并且不会向您提供未使用的变量警告。