小编Ama*_*eur的帖子

Haskell 模式不匹配 [_]

我一直在尝试使以下代码工作:

{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
import Data.List
format :: String -> String
format [] = []
format (a:b:xs)
 | a == 'W' && b == 'U' = " " ++ format (drop 1 xs) 
 | otherwise = a : format (b:xs)

songDecoder :: String -> String
songDecoder xs = unwords. words . format $ xs
Run Code Online (Sandbox Code Playgroud)

当我测试时:

歌曲解码器“AWUBBWUBC”

我希望“ABC”作为输出。但是,我收到了一个不寻常的模式匹配警告:

Pattern match(es) are non-exhaustive
In an equation for ‘format’: Patterns not matched: [_]
Run Code Online (Sandbox Code Playgroud)

我不确定为什么我需要匹配 [_]

format (a:b:xs)
Run Code Online (Sandbox Code Playgroud)

请帮忙。

string haskell list pattern-matching

2
推荐指数
1
解决办法
1387
查看次数

标签 统计

haskell ×1

list ×1

pattern-matching ×1

string ×1