外卡模式匹配如何在案例表达式中起作用?

Ite*_*tor 2 haskell

我想了解一个使用WebSocket库的示例服务器代码.

我发现了一个奇怪的案例表达,我无法理解:

case msg of
    _   | not (prefix `T.isPrefixOf` msg) ->
            WS.sendTextData conn ("Wrong announcement" :: Text)

        | any ($ fst client)
            [T.null, T.any isPunctuation, T.any isSpace] ->
                WS.sendTextData conn ("Name cannot " `mappend`
                    "contain punctuation or whitespace, and " `mappend`
                    "cannot be empty" :: Text)

        | clientExists client clients ->
            WS.sendTextData conn ("User already exists" :: Text)

        | otherwise -> flip finally disconnect $ do

        -- ...
Run Code Online (Sandbox Code Playgroud)

这张外卡在这里意味着什么?case表达式的语法是这样的:

case expression of pattern -> result  
                   pattern -> result  
                   pattern -> result  
                   ...  
Run Code Online (Sandbox Code Playgroud)

为什么_必要的,为什么作者能够在其中使用警卫?

chi*_*chi 7

case irrelevant of
   _ | condition1 -> e1
     | condition2 -> e2
     ...
     | otherwise  -> eO
Run Code Online (Sandbox Code Playgroud)

是一种编写链条的奇特方式if then else.

if condition1 
then e1
else if condition2
then e2
...
else eO
Run Code Online (Sandbox Code Playgroud)

irrelevant表达是无关紧要的.它的值匹配_,它总是成功并丢弃该值.

你编码混乱使用case msg of ...msg被忽略.通常有人写case () of ...这么强调它的价值是无关紧要的.