0

SML でセットを操作するために使用できる 3 つの関数定義を作成しようとしています。ご覧のとおり、実装はリストに基づいています。

Union は、 Set s と Set t の両方のすべての要素のセットです。(重複は不可)

交差は、要素が Set s と Set t の両方の一部であるセットです。

Set と Set t がセットの場合、Set t 内の Set の相対的補数は Set t 内の要素のセットですが、Set 内の要素のセットではありません。

現在、コードは次のようになっています。

  fun filter p [] = []
 |       filter p (h::t) =
                 if p h
                 then h :: (filter p t)
                 else (filter p t);

  fun mem [] a = false
 |        mem (h::t) a = (a=h) orelse (mem t a);


 signature SETS =
       sig
                 type ''a set
                 val union : ''a set -> ''a set -> ''a set
                 val intersection : ''a set -> ''a set -> ''a set
                 val difference : ''a set -> ''a set -> ''a set
       end;

 structure Set : SETS =
       struct
                 type ''a set = ''a list;
                 fun union s t = **(Code goes here)**
                 fun intersection s t = **(Code goes here)**
                 fun difference s t =  **(Code goes here)**
       end; 

ご覧のとおり、必要に応じて使用できる 2 つのヘルプ関数があります。mem と filter.filter はリストを調べて、ブール関数 p を満たす要素のみを保持しますが、mem はリストに値 a が含まれているかどうかを確認するだけです。 .

4

0 に答える 0