首页 > 解决方案 > 如何返回一些列表而不是普通列表?

问题描述

我正在尝试实现一个程序,该程序接受一个字符串和一个列表,NONE如果找不到任何匹配项则返回,如果找到则返回没有元素的列表。

fun all_except_option ("string",["he","she","string"]) = SOME["he","she"]

我设法让它工作,但没有选项类型,我不知道如何让它返回SOME list一个普通列表。

fun all_except_option(str,lst)=
    case lst of
         [] => []
      | x::lst' => if same_string(x,str) = false
                   then let fun append (word, list) = word::list
                        in append(x,[]) :: all_except_option(str,lst')
                        end
                   else all_except_option(str,lst')

标签: smlsmlnj

解决方案


谢谢你。我设法让它工作,但我仍然不明白“其他情况”以及我的程序如何处理它。这是工作代码。如果你能解释一下“else case all_except_option(str,list') of”,我会很高兴。

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

推荐阅读