2013-11-01 8 views
0

연습은 이진 검색 트리에서 요소를 삭제하는 ML에 함수를 코딩하는 것입니다.ML은 'a를 int와 통합 할 수 없습니다.

datatype 'a tree = Lf | Br of 'a * 'a tree * 'a tree; 

fun deleteTop (Br(_, Lf, t2)) = t2 
    | deleteTop (Br(_, t1, Lf)) = t1 
    | deleteTop (Br(_, Br(v, u1, u2), t2)) = 
    Br(v, deleteTop (Br(v, u1, u2)), t2); 

fun delete (Lf, k : string) = Lf 
    | delete (Br((a,b),t1,t2), k) = 
    if a=k then deleteTop(Br((a,b),t1,t2)) 
    else if k<a then Br((a,b),delete(t1,k),t2) 
      else Br((a,b),t1,delete(t2,k)); 

나는 폴리/ML에이를로드 할 때이 deleteTop에 일치하는 불완전 패턴의 저를 경고하지만 그 때문에 오직 삭제 deleteTop에게 지점을 통과 문제가되지 않습니다 여기에 코드입니다.

val deleteTop = fn: 'a tree -> 'a tree 
val delete = fn: (string * 'a) tree * string -> (string * 'a) tree 

은 내가 (문자열 * INT) 트리를 만들고
> delete(a,"they"); 
Error-Type error in function application. 
    Function: delete : (string * 'a) tree * string -> (string * 'a) tree 
    Argument: (a, "they") : (string * int) tree * string 
    Reason: 
     Can't unify (string * 'a) tree with (string * int) tree 
     (Different type constructors) 
Found near delete (a, "they") 
Static Errors 

을 실행 나를 다시 반복 처리하자 그 라인 중 하나를

Can't unify (string * 'a) tree with (string * int) tree 

하는 이유의 Unify ML 수 없다 '는 int로? 당신이을 정의하기 때문에는 최상위 수준에을 삭제 트리를 재정의 한 경우

답변

1

당신은 그런 메시지를 얻을 수 있습니다. 트리트리과 동일하지 않습니다. 예를

> datatype 'a t = T of 'a; 
datatype 'a t = T of 'a 
> val x = T 1; 
val x = T 1: int t 
> datatype 'a t = T of 'a; 
datatype 'a t = T of 'a 
> val T y = x; 
Pattern and expression have incompatible types. 
    Pattern: T y : 'a t 
    Expression: x : int t 
    Reason: Can't unify 'a t with int t (Different type constructors) 
Found near val T y = x 
Static Errors 
> 
+0

감사에 대한

이 나를 당황하게 남아있는 것입니다. 이제는 올바른 출력을 제공하도록 삭제를 다시 작성하십시오 ... –