Haskell pattern matching char in a string

前端 未结 4 1266
甜味超标
甜味超标 2021-01-13 18:08

I have a question on pattern matching:

Is it possible to somehow match a (string ++ [char] ++ anotherstring)?

I have tried something like:

f          


        
4条回答
  •  一向
    一向 (楼主)
    2021-01-13 18:56

    For completeness' sake, one could make gratuitous use of GHC's ViewPatterns extension, and rewrite Daniel Fischer's example as something like:

    {-# LANGUAGE ViewPatterns #-}
    
    f (break (== ';') -> (s, _:r)) = s ++ r
    f _ = error "No semicolon found"
    

    This is of course a purely cosmetic change, but if you prefer the usual "group of equations" syntax instead of case expressions, there it is.

    N.B. -- I don't have GHC at hand right now so I haven't actually tested the above.

提交回复
热议问题