Haskell pattern matching char in a string

前端 未结 4 1287
甜味超标
甜味超标 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:48

    No, it's not possible. Pattern matching deconstructs values according to the constructors they were built with, so you can only use constructor applications in pattern matching to describe which values match the pattern and which don't.

    For something like your example, a case works well,

    f str = case break (== ';') str of
              (s, _:r) -> s ++ r
              _        -> error "No semicolon found"
    

提交回复
热议问题