【问题标题】:How to do pattern matching on string in SML?如何在 SML 中对字符串进行模式匹配?
【发布时间】:2014-11-03 17:44:02
【问题描述】:

例如,

fun f #"a"::_ = "first character is a"

但这在 sml 中不起作用。 无论如何我可以在不将其转换为字符列表的情况下对字符串进行模式匹配吗?

【问题讨论】:

    标签: sml


    【解决方案1】:

    您的代码不起作用,因为您忘记包含括号,所以它应该是这样的:

    fun f (#"a"::_) = "first character is a";
    

    如果要对字符串进行模式匹配,可以直接使用子字符串。在这种情况下,它可以是:

    fun f (str) = if substring(str, 0, 1) = "a" then "first character is a" else ""
    

    【讨论】:

      猜你喜欢
      • 2014-09-30
      • 1970-01-01
      • 2019-02-28
      • 1970-01-01
      • 1970-01-01
      • 2011-11-13
      • 2018-08-13
      • 2017-06-10
      • 1970-01-01
      相关资源
      最近更新 更多