问题
for example,
fun f #"a"::_ = "first character is a"
But this does not work in sml. Is there anyway I can do pattern matching on string without turning it to a char list?
回答1:
Your code doesn't work because you forget to include the bracket, so it should be something like this:
fun f (#"a"::_) = "first character is a";
If you want to do a pattern matching on string, you can use substring directly. In this case, it can be:
fun f (str) = if substring(str, 0, 1) = "a" then "first character is a" else ""
来源:https://stackoverflow.com/questions/26695111/how-to-do-pattern-matching-on-string-in-sml