Ber*_*ian 1 haskell pattern-matching
您好,有人可以解释一下我如何在haskell中执行以下操作:
f :: Char -> Bool
f 'a' = someMethod
f 'b' = someMethod
f 'c' = someMethod
f _ = someOtherMethod
Run Code Online (Sandbox Code Playgroud)
我可以做某种类似于@模式的事情:
f :: Char -> Bool
f x@(pattern1 || pattern2 ...pattern n) = sameMethod x
Run Code Online (Sandbox Code Playgroud)
基本上我想对多个模式应用相同的方法.这可能吗?我不想写N个模式匹配线,基本上做同样的事情.
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
Run Code Online (Sandbox Code Playgroud)
我正在解析一个String可以分隔的},{和,.对于}和{我想应用相同的方法.
在这个特定的例子中,你可以使用一个警卫:
f :: Char -> Bool
f x | x `elem` "abc" = someMethod
f _ = someOtherMethod
Run Code Online (Sandbox Code Playgroud)