我是一个Haskell新手,并且在弄清楚如何模式匹配时遇到了一些麻烦ByteString.在[Char]我的函数的版本是这样的:
dropAB :: String -> String
dropAB [] = []
dropAB (x:[]) = x:[]
dropAB (x:y:xs) = if x=='a' && y=='b'
then dropAB xs
else x:(dropAB $ y:xs)
Run Code Online (Sandbox Code Playgroud)
正如所料,这会过滤掉字符串中出现的所有"ab".但是,我在尝试将其应用于a时遇到问题ByteString.
天真的版本
dropR :: BS.ByteString -> BS.ByteString
dropR [] = []
dropR (x:[]) = [x]
<...>
Run Code Online (Sandbox Code Playgroud)
产量
Couldn't match expected type `BS.ByteString'
against inferred type `[a]'
In the pattern: []
In the definition of `dropR': dropR [] = []
Run Code Online (Sandbox Code Playgroud)
[]显然是罪魁祸首,因为它是一个常规String而非一个ByteString.Subbing in …
我正在开发一个Haskell项目,我开始通过这样组织它:
blah.hs 拥有大部分代码blah_main.hs 有主程序blah_test.hs有测试用例.这样做的问题是通过blah.hs限制可以测试的功能来限制导出的功能blah_test.hs.这个问题有好办法吗?因为我真的想为某些未被导出的"内部"函数编写测试代码blah.hs.
谢谢,李