ML。 "解链"元组列表 [英] ML. "Unzipping" list of tuples.
问题描述
这是我到目前为止所得到的:
datatype('a,'b)alterlist = nil | ::('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)= []
|提取((x,y):: xs,i)= if i = 0 then x :: extract(xs,i)
else y :: extract(xs,i);
(extract(alterlist,0))::( extract(alterlist,1))
end;
但是我收到一些错误:
stdIn:48.6-50.26错误:子句的参数或结果约束不一致[tycon mismatch]
此子句:('Z,'Y)alterlist *'X - > 'W
以前的子句:'V list *'U - > '声明中的$ W
extract =
(fn(nil,i)=> nil
|(< pat> ::< pat>,i)= >
if< exp> =< exp> then< exp> ::< exp> else< exp> ::< exp>)
stdIn:49.41-49.58错误:运算符和操作数不同意[tycon mismatch]
运算符域:'Z list *'Y
操作数:('X,'W)alterlist * int
表达式中:
提取(xs,i)
stdIn:50.9-50.26错误:运算符和操作数不同意[tycon mismatch]
运算符域:'Z list *'Y
操作数:(_ * _,'X)alterlist * int
在表达式中:
extract(xs,i)
stdIn:48.6-50.26错误:右边的子句不符合函数结果类型[tycon mismatch]
expression:(_,_)alterlist
result type:'Z list
in declaration:
extract =
(fn(nil, i)=> nil
|(< pat> ::< pat>,i)=>
如果< exp> =< exp>那么< exp> ::< exp> else< exp> ::< exp>)
而且,我是新来的,我有一点点想法是什么原因会非常感谢帮助!
我的建议首先是没有中缀运算符,因为它有点混乱。
首先解决它,然后再添加。
这是一个没有中缀的解决方案:
datatype('a,'b)alterlist = Nil
|元素'a *('b,'a)alterlist;
fun unzip(Nil:('a,'b)alterlist)=([],[])
| unzip(ablist:('a,'b)alterlist)=
let
fun extract Nil = []
| extract(element(curr,Nil))= curr :: []
|提取(元素(curr,元素(_,休息)))= curr::(提取休息)
val元素(_,balist)= ablist
(extract ablist,extract balist )
end;所以例如像这样的列表:[a,1,b,2]这样的列表:
<将由以下创建:
元素(a,元素(1,元素(b),元素(2, ))));
给你:
val it = element(a,element(1,element#)):(string,int)alterlist
*#只是表示列表比显示的时间长。
然后如果你尝试解压缩;
您将得到:
val it =([a ,b],[1,2]):string list * int list
想要。
现在尝试将元素更改为:: infix运算符。
祝你好运! p>
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!
My advice is first do it without the infix operator because it is a little bit confusing. First solve it without and add it later.
Here is a solution without the infix:
datatype ('a,'b)alterlist = Nil
| element of 'a*('b,'a)alterlist;
fun unzip (Nil : ('a,'b)alterlist ) = ([],[])
| unzip (ablist : ('a,'b)alterlist) =
let
fun extract Nil = []
| extract (element (curr, Nil)) = curr::[]
| extract (element (curr, element(_,rest))) = curr::(extract rest)
val element (_, balist) = ablist
in
(extract ablist, extract balist)
end;
So for example a list like this: ["a", 1, "b", 2] would be created by:
element ("a", element (1, element ("b", element (2, Nil))));
Giving you:
val it = element ("a",element (1,element #)) : (string,int) alterlist
* The # is just to indicate the list is longer than is displayed.
And then if you would try unzip it;
You would get:
val it = (["a","b"],[1,2]) : string list * int list
As you would like.
Now try to change element into a :: infix operator.
Good luck!
这篇关于ML。 "解链"元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!