標準 ML で新しいデータ型の関数を作成したいのですが、データ型は intnest と呼ばれ、次のように定義されます。
datatype intnest =
INT of int
| LIST of intnest list;
intlist のすべての整数を追加する関数を作成したいので、次のコードを試しました。
fun addup (INT n) = n
| addup (LIST x::xs) = x + addup(xs);
私は何を間違っていますか?
編集:
私も次のことを試しました:
fun addup (INT n) = n
| addup (LIST x::xs) = addup(x) + addup(xs);
x は INT 型であるため、最初のオプションはその int 値を返し、addup(xs) は同じ 2 番目のオプションを返すための再帰呼び出しです。
次のことも試しました:
fun addup (INT n) = n
| addup (LIST []) = 0
| addup (LIST x::xs) = addup(x) + addup(LIST xs);
しかし、次のエラーが表示されます。
stdIn:146.4-151.50 Error: parameter or result constraints of clauses don't agree [tycon mismatch]
this clause: intnest list -> 'Z
previous clauses: intnest -> 'Z
in declaration:
addup =
(fn INT n => n
| LIST nil => 0
| :: (<pat>,<pat>) => addup <exp> + addup <exp>)
stdIn:151.25-151.50 Error: operator and operand don't agree [tycon mismatch]
operator domain: intnest
operand: intnest list
in expression:
addup x