Functional programming 使用选项和模式匹配的SML列表和

Functional programming 使用选项和模式匹配的SML列表和,functional-programming,pattern-matching,sml,smlnj,ml,Functional Programming,Pattern Matching,Sml,Smlnj,Ml,我正在创建列表的总和,并在其中使用选项。当我传递一个空列表时,我应该得到NONE或者一些值 我可以通过以下方式做到这一点: fun sum_list xs = case xs of [] => NONE | x => let fun slist x = case x of [] => 0 | x

我正在创建列表的总和,并在其中使用
选项
。当我传递一个空列表时,我应该得到
NONE
或者
一些值

我可以通过以下方式做到这一点:

fun sum_list xs =
    case xs of
        [] => NONE
      | x => 
        let
            fun slist x =
                case x of
                    [] => 0
                  | x::xs' => x + slist xs'
        in 
            SOME (slist x)
        end
但是我想用另一种方法使用模式匹配,在模式匹配中,我想评估
sum\u list
的结果,看看它是
NONE
还是包含其他值

我已经尝试过各种方法,但我不知道如何用这种方法。我认为你目前所掌握的非常清楚,也很容易理解

如果要避免使用
slist
,必须递归调用列表尾部的
sum\u list
,对该
选项的值进行模式匹配,并返回适当的结果:

fun sum_list xs =
    case xs of
       [] => NONE
     | x::xs' => (case (sum_list xs') of
                     NONE => SOME x
                   | SOME y => SOME (x+y))
我认为你现在所拥有的是非常清楚和容易理解的

如果要避免使用
slist
,必须递归调用列表尾部的
sum\u list
,对该
选项的值进行模式匹配,并返回适当的结果:

fun sum_list xs =
    case xs of
       [] => NONE
     | x::xs' => (case (sum_list xs') of
                     NONE => SOME x
                   | SOME y => SOME (x+y))