我想编写一个Haskell函数,其行为依赖于匹配其中一个参数的正则表达式模式.在像C/Python/Perl这样的语言中,我肯定会使用一个大的if/else结构,但我真的没有那个选项.什么是最惯用的Haskell处理方法?
我考虑过警卫,但他们没有工作No instance for (Data.String.IsString source0):
function arg
| arg =~ "pattern1" = dothis arg
| arg =~ "pattern2" = dothat arg
| otherwise = failwith arg
Run Code Online (Sandbox Code Playgroud)
如果处理正则表达式,在案例结构中使用的模式匹配将是完美的.
function arg = case arg of
(pattern1 match) -> dothis match
(pattern2 match) -> dothat match
_ -> failwith arg
Run Code Online (Sandbox Code Playgroud)