So I have this list of tuples(n=2), witch I'm supposed to "unzip" and by that create a new list like so: for list of tuples like this val it = (1,"one") :: (2,"two") :: nil : (int,string) alterlist, the unzip function will create a list like so [(1,2),("one", "two")]. This is what I got so far:
datatype ('a, 'b) alterlist = nil | :: of ('a*'b) * ('a, 'b) alterlist;
infixr 5 ::
fun build4(x, one, y, two) = (x,one)::((y,two)::nil);
fun unzip(alterlist) =
let
fun extract([], i) = []
| extract((x,y)::xs, i) = if i=0 then x::extract(xs, i)
else y::extract(xs, i);
in
(extract(alterlist, 0))::(extract(alterlist, 1))
end;
But I get a bunch of errors:
stdIn:48.6-50.26 Error: parameter or result constraints of clauses don't agree [tycon mismatch]
this clause: ('Z,'Y) alterlist * 'X -> 'W
previous clauses: 'V list * 'U -> 'W
in declaration:
extract =
(fn (nil,i) => nil
| (<pat> :: <pat>,i) =>
if <exp> = <exp> then <exp> :: <exp> else <exp> :: <exp>)
stdIn:49.41-49.58 Error: operator and operand don't agree [tycon mismatch]
operator domain: 'Z list * 'Y
operand: ('X,'W) alterlist * int
in expression:
extract (xs,i)
stdIn:50.9-50.26 Error: operator and operand don't agree [tycon mismatch]
operator domain: 'Z list * 'Y
operand: (_ * _,'X) alterlist * int
in expression:
extract (xs,i)
stdIn:48.6-50.26 Error: right-hand-side of clause doesn't agree with function result type [tycon mismatch]
expression: (_,_) alterlist
result type: 'Z list
in declaration:
extract =
(fn (nil,i) => nil
| (<pat> :: <pat>,i) =>
if <exp> = <exp> then <exp> :: <exp> else <exp> :: <exp>)
And being that I'm new to ml I have very little idea what causes it. Would greatly appreciate help!