Haskell 中“[_]”模式的含义是什么?

Con*_*nor 2 haskell pattern-matching

在 Haskell 中创建模式匹配时,是否执行以下模式匹配:

function [_] = []
Run Code Online (Sandbox Code Playgroud)

意思相同:

function (x:xs) = []
Run Code Online (Sandbox Code Playgroud)

如果不是,这个[_]图案意味着什么?

che*_*ner 8

[_]与一个列表匹配一个元素;匹配任何非空列表,具有将头部绑定到并将尾部绑定到x:xs的副作用。xxs

[_]相当于(_:[]).

给定

f x = case x of 
        [_] -> "singleton"
        [] -> "empty"
        otherwise -> "nonempty"
Run Code Online (Sandbox Code Playgroud)

然后

> print $ map f [[], [1], [1,2]]
["empty","singleton","nonempty"]
Run Code Online (Sandbox Code Playgroud)