I need to compare a string to different regexps, which each give a different result.
In ruby I would do something like
case str
when regexp1 then result1
when regexp2 then result2
when regexp3 then result3
...
My first attempt in Haskell is
if str =~ regexp1
then result 1
else
let (_,_,_,groups) = str =~ regexp2
in if (length groups > 0)
then result2 groups
else ...
I'm sure there is a much nicer way to do so in Haskell using Alternative or Monad etc ...
Update
My second solution (using alternavite)
fun str =
regexp1 ?~ (\_ -> "result1")
<|> regexp2 ?~ (\[capture] -> "capture"
<|> regexp3 ?~ (\[c1,c2\ ] -> c1 ++ ":" c2
where r ?~ f = do
match <- r =~~ str
(_,_,_,groups) = match :: (String, String, String, [String])
Just (f groups)
(