Bercovici Adrian
Bercovici Adrian

Reputation: 9370

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.

P.S 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.

Upvotes: 1

Views: 2951

Answers (1)

Mark Seemann
Mark Seemann

Reputation: 233347

In this particular example, you could use a guard:

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

Upvotes: 6

Related Questions