내가 합성 목록 주어진 두 개의 목록을 반환하는 기능 구축 :멀티 타입 목록
let l1 = [('a', 1, 'a'); ('b', 2, 'b'); ('c', 3, 'c'); ('d', 4, 'd')]
기능 : 이제
let rec split2 l =
match l with
[] -> ([], [])
| (x, y)::ls -> let (xs, ys) =
split ls in (x::xs, y::ys);;
val split2 : l:('a * 'b) list -> 'a list * 'b list
lsts = [('a', 1); ('b', 2); ('c', 3); ('d', 4)]
split2 lsts;;
val it : int list * char list = ([1; 2; 3; 4], ['a'; 'b'; 'c'; 'd'])
을, 나는 더 복잡한 목록 개념을 적용 나는 타입에 문제를 주었다. 그래서 나는 두 번째 타입을 만든다. 이 경우 형식을 신중하게 정의했지만 컴파일해도 l1
에 적용하면 오류가 반환됩니다.
let rec split3 (l:(char * int * char) list) =
match l with
[] -> ([], [], [])
| (x, y, z)::ls ->
let (xs, ys, zs) =
split3 ls in (xs, ys, zs);;
val split3 : l:(char * int * char) list -> 'a list * 'b list * 'c list
split3 l1;;
error FS0030: Value restriction. The value 'it' has been inferred to
have generic type val it : '_a list * '_b list * '_c list
Either define 'it' as a simple data term, make it a function with explicit
arguments or, if you do not intend for it to be generic, add a type annotation.
왜 형식이 선언 되더라도 추가 형식 주석이 필요합니까?
예. 그래서 두 번째 질문을 생각해 보았습니다. 첫 번째 예제에서는 '두 요소'목록과 두 번째 예제에서는 '세 요소'목록과의 차이점은 무엇입니까? 둘 다 문자와 정수로 구성됩니다. – Worice
첫 번째 코멘트는 무의미했습니다. 컴파일러는 함수의 반환 유형을 유추 할 수 없습니다.'let rec split3 (l : (char * int * char) list) : (char list * int list * char list) = ' – Petr
때때로 값의 이해가 어렵 기 때문에 값 제한 오류가 더 많습니다. https : //blogs.msdn.microsoft.com/mulambda/2010/05/01/finer-points-of-f-value-restriction/ – Petr