【发布时间】:2017-11-13 07:00:57
【问题描述】:
我一直在尝试使以下代码工作:
{-# 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
当我测试时:
歌曲解码器“AWUBBWUBC”
我希望“ABC”作为输出。但是,我收到了一个不寻常的模式匹配警告:
Pattern match(es) are non-exhaustive
In an equation for ‘format’: Patterns not matched: [_]
我不确定为什么我需要匹配 [_]
format (a:b:xs)
请帮忙。
【问题讨论】:
-
如果您使用
-Wall打开警告,GHC 会在编译时对此发出警告,并指出要匹配的缺失大小写。推荐。
标签: string list haskell pattern-matching