Match strings of SML / NJ string

Is there something like pattern matching in SML / NJ, but for strings? In the end, I want to remove the first character of the string if it is specific and this solution first came to mind, so I would appreciate it if I could do this without further mess, eg. by turning the string into a list form, etc.

+3


source to share


2 answers


Not directly. The most common way to solve this problem would be to force the caller of your function to strip the first character in such a way that you can match it to the pattern.



In SML'97, the line is the CharVector.vector file (which has the signature MONO_VECTOR). SML / NJ allows pattern matching on vectors (which is a non-standard extension), but unfortunately not on monomorphic vectors as far as I can tell.

+3


source


String.explode

will print a list of characters from a string. Matching the heading of this list will give the desired functionality:

fun f s =
   let val c = hd(String.explode s)
   in
    case c
    of #"a" => "The character is a!"
    |  #"b" => "The character is b!"
    |  #"c" => "The character is c!"
    | _     => "Not a b or c!"
   end

      



The symbol list suggests parsing the states of the states, rather than following a Perl-like regex pattern.

+3


source







All Articles