0

私は Ocaml を初めて使用し、再帰データ型で定義された再帰関数のヘルプが必要です。次のようにデータ型を定義しました

    type 'a slist = S of 'a sexp list
    and
    'a sexp = Symbol of 'a | L of 'a slist

私が書いている関数 (subst) は、定義された slist 内のシンボル a をチェックし、それを b に置き換えます。ex subst 1 10 S[ 1; の場合 4; S[L[3; 1;]; 3];] は S[10; を返します。4; S[L[S[3; 10;]]; 3;] . 私のコードは次のとおりです

    let rec subst a b sl = match sl with
    S s -> match s with
          [] -> []
          | p :: q -> match p with
                Symbol sym -> if sym = a then S[b] :: (**subst** a b S[q]) else
                                 S[p] :: (subst a b S[q])
                L l -> (subst a b l) :: (subst a b S[q])
    | L lis -> subst a b lis;;

エラーが発生しています:

この関数が適用される引数が多すぎます。「;」を忘れたのかもしれません。

助けてください

4

1 に答える 1

1

あなたのタイプはより簡単な方法で定義できます.必要はありませんslist:

type 'a sexp = Symbol of 'a | L of 'a sexp list

あなたの問題は、それsubst a b S[q]が として読み取られるsubst a b S [q]ことです。つまり、関数 subst が 4 つの引数に適用されます。subst a b (S[q])代わりに書く必要があります。

于 2013-06-05T12:11:36.787 に答える