Haskell Bytestrings:如何模式匹配? [英] Haskell Bytestrings: How to pattern match?
问题描述
我是Haskell的新手,并且在找出如何匹配 ByteString
时遇到了一些麻烦。我的函数的 [Char]
版本看起来像:
dropAB: :字符串 - >字符串
dropAB [] = []
dropAB(x:[])= x:[]
dropAB(x:y:xs)= if x =='a'&& amp ; y =='b'
然后dropAB xs
else x:(dropAB $ y:xs)
正如所料,这会从字符串中过滤所有出现的ab。然而,我试图将它应用于 ByteString
。
天真版本
dropR :: BS.ByteString - > BS.ByteString
dropR [] = []
dropR(x:[])= [x]
< ...> b
$ b $ p code>无法匹配预期的类型'BS.ByteString'
与推断的类型'[a]'
在模式:[]
'dropR'的定义中:dropR [] = []
[]
是显然是罪魁祸首,因为它是一个普通的字符串
不是 ByteString
。在 BS.empty
中的Subbing似乎是正确的,但它给出了绑定位置中的限定名:BS.empty。离开我们去尝试
dropR :: BS.ByteString - > BS.ByteString
dropR empty = empty
dropR(x cons empty)= x cons empty
< ...>
这给出了<模式解析错误> (x cons empty)
。我真的不知道我还能在这里做什么。
作为一个附注,我试图用这个函数做的是从某些文本中过滤掉一个特定的UTF16字符。如果有一个干净的方法来实现这一点,我很乐意听到它,但这种模式匹配错误看起来像是一个新手哈克勒应该真正理解的东西。
您可以使用查看模式 for this things
{ - #LANGUAGE ViewPatterns# - }
进口Data.ByteString(字节串,利弊,uncons,单,空)
进口Data.ByteString.Internal(C2W)
dropR ::字节字符串 - > ;字节字符串
dropR(uncons - >为Nothing)=空
dropR(uncons - >仅有(X,uncons - >为Nothing))=单X
dropR(uncons - >只要(x,uncons - > Just(y,xs)))=
if x == c2w'a'&& y == c2w'b'
然后dropR xs
else x(dropR $ cons y xs)
I'm a Haskell newbie, and having a bit of trouble figuring out how to pattern match a ByteString
. The [Char]
version of my function looks like:
dropAB :: String -> String
dropAB [] = []
dropAB (x:[]) = x:[]
dropAB (x:y:xs) = if x=='a' && y=='b'
then dropAB xs
else x:(dropAB $ y:xs)
As expected, this filters out all occurrences of "ab" from a string. However, I have problems trying to apply this to a ByteString
.
The naive version
dropR :: BS.ByteString -> BS.ByteString
dropR [] = []
dropR (x:[]) = [x]
<...>
yields
Couldn't match expected type `BS.ByteString'
against inferred type `[a]'
In the pattern: []
In the definition of `dropR': dropR [] = []
[]
is clearly the culprit, as it is for a regular String
not a ByteString
. Subbing in BS.empty
seems like the right thing but gives "Qualified name in the binding position: BS.empty." Leaving us to try
dropR :: BS.ByteString -> BS.ByteString
dropR empty = empty
dropR (x cons empty) = x cons empty
<...>
this gives "parse error in pattern" for (x cons empty)
. I don't really know what else I can do here.
As a side note, what I'm trying to do with this function is to filter out a specific UTF16 character from some text. If there's a clean way to accomplish that, I'd love to hear it, but this pattern matching error seems like something that a newbie haskeller should really understand.
You can use view patterns for such things
{-# LANGUAGE ViewPatterns #-}
import Data.ByteString (ByteString, cons, uncons, singleton, empty)
import Data.ByteString.Internal (c2w)
dropR :: ByteString -> ByteString
dropR (uncons -> Nothing) = empty
dropR (uncons -> Just (x,uncons -> Nothing)) = singleton x
dropR (uncons -> Just (x,uncons -> Just(y,xs))) =
if x == c2w 'a' && y == c2w 'b'
then dropR xs
else cons x (dropR $ cons y xs)
这篇关于Haskell Bytestrings:如何模式匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!