0

私はこのコードを持っています:

fun all_except_option2(str : string, strlst : string list) =
    let fun all_except_option([], result) = NONE
    | all_except_option(str::T, result) = SOME(List.rev(result) @ T)
    | all_except_option(H::T, result) =all_except_option(T, H::result)
    in
    all_except_option(strlst, [])
    end

そしてコンパイラは言う:

hw2provided.sml:22.13-24.70エラー:冗長一致(nil、result)=> ...(str :: T、result)=> ...->(H :: T、result)=>..。

私はすでに「caseof」ステートメントでこれを処理していますが、私の質問は、言語が(上から)strとパターンマッチングを行わない方法ですか?コンパイラがstrをHのように考える理由。

4

1 に答える 1

2

あなたが書くとき

| all_except_option(str::T, result) =

strこれは、古いパラメータをシャドウする新しいバインディングstrです。したがって、2番目のパターンはh::t最後のパターンと同じ形式になります。

構成strを使用して、値をパラメーターと比較できます。if/else

fun all_except_option2(str: string, strlst: string list) =
  let 
     fun all_except_option([], result) = NONE
      | all_except_option(h::t, result) = if h = str 
                                          then SOME(List.rev(result) @ t)
                                          else all_except_option(t, h::result)
  in
     all_except_option(strlst, [])
  end
于 2013-01-26T22:51:43.517 に答える