【问题标题】:In SML, how do I remove the first occurence of a pattern from a list and then return the removed item and the rest of the list? [closed]在 SML 中,如何从列表中删除第一次出现的模式,然后返回删除的项目和列表的其余部分? [关闭]
【发布时间】:2013-11-04 10:34:40
【问题描述】:

我正在尝试编写一个将列表和项目作为参数的 SML 函数。如果该项目存在,则该函数应返回一个包含列表的元组,而不会第一次出现该项目和刚刚删除的项目。如果列表中没有出现该项目,则该函数应返回 NONE 或类似的内容以指示此不存在。

【问题讨论】:

  • 听起来像是 Coursera 编程语言课程作业 2 中的问题 1。如果是这样,这将违反荣誉准则:“我对家庭作业、测验和考试的回答将是我自己的工作”。

标签: list recursion functional-programming sml smlnj


【解决方案1】:

试试这个:

fun same_string(str, lst) =
        case lst of
             []     =>   NONE
            |x::xs    =>  case same_string(str, xs) of
                            NONE   => if str = x  
                                      then SOME(xs)
                                      else NONE
                           |SOME xs' => SOME (x :: xs')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 2021-09-15
    • 2012-10-26
    相关资源
    最近更新 更多