Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell pattern matching char in a string

I have a question on pattern matching:

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

I have tried something like:

f (s++";"++r) = s++r (the rhs is trivial, but its just for testing ;))

But this results in a parse error.

like image 344
Stackd Avatar asked Dec 07 '22 11:12

Stackd


1 Answers

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"
like image 185
Daniel Fischer Avatar answered Dec 09 '22 13:12

Daniel Fischer