How to do pattern matching on string in SML?

此生再无相见时 提交于 2019-12-11 20:42:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!