1

)私は SML を始めたばかりで、L1 と L2 の 2 つのリストを取り、両方に出現する要素のリストを返す関数を作成しようとしています。これは私がこれまでに持っているものです:

fun exists x nil = false | exists x (h::t) = (x = h) orelse (exists x t);

    fun listAnd L1 nil = nil
     | listAnd nil L2 = nil
     | listAnd L1 L2 = if exists(hd(L1) L2) = true then hd(L1)::(listAnd(tl(L1) L2)) else listAnd(tl(L1) L2);

エラーがどこにあるのかよくわかりません。

4

1 に答える 1

0

existsは 2 つの引数を取る関数であるため、両方の引数を余分な括弧で囲むのは間違いです。たとえば、 のようにexists(hd(L1) L2)修正する必要がありexists (hd L1) L2ます。

いくつかの提案があります:

  • 冗長性の削除= true
  • []空のリストに使用し、未使用の値にワイルドカードを_使用
  • andL1の代わりにon のパターン マッチングを使用するhdtl

これが修正された関数です。

fun listAnd _ [] = []
  | listAnd [] _ = []
  | listAnd (x::xs) ys = if exists x ys 
                         then x::(listAnd xs ys) 
                         else listAnd xs ys
于 2012-10-17T19:24:50.490 に答える