简体   繁体   English

多模式匹配

[英]Multiple pattern match

Hello can someone please explain me how can i do the following in haskell: 您好,有人可以解释一下我如何在Haskell中执行以下操作:

f :: Char -> Bool
f 'a' = someMethod
f 'b' = someMethod
f 'c' = someMethod
f _ = someOtherMethod

Can i do somehow something similar to the @ pattern : 我可以做某种类似于@模式的事情吗:

f :: Char -> Bool
f x@(pattern1 || pattern2 ...pattern n) = sameMethod x

Basically I want to apply the same method for multiple patterns .Is this possible? 基本上我想对多个模式应用相同的方法,这可能吗? I do not want to write N pattern match lines that basically do the same thing. 我不想编写基本上执行相同操作的N个模式匹配行。

PS My method that i want to implement is the following : PS我要实现的方法如下:

readContent::String->Maybe [Double]
readContent (x:xs)=go [] [](x:xs) where
    go _ ls [] =if length length ls > 0  then Just ls else Nothing
    go small big (x:xs)= case x of
                           '}' -> Just (small:big) -- i want some other pattern here too
                           ',' -> go [] small:big  xs 
                            t@(_) -> go  t:small big xs

I am parsing a String that can be delimited by } , { and , . 我正在解析一个可以用}{,分隔的String For } and { i want to apply the same method. 对于}{我想应用相同的方法。

In this particular example, you could use a guard : 在这个特定的示例中,您可以使用防护

f :: Char -> Bool
f x | x `elem` "abc" = someMethod
f _ = someOtherMethod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM