简体   繁体   中英

Multiple pattern match

Hello can someone please explain me how can i do the following in 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.

PS My method that i want to implement is the following :

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 , . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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