【问题标题】:ML. "Unzipping" list of tuples.毫升。 “解压缩”元组列表。
【发布时间】:2014-07-13 08:29:47
【问题描述】:

所以我有这个元组列表(n = 2),女巫我应该“解压缩”并创建一个像这样的新列表:对于像这样的元组列表 val it = (1,"one" ) :: (2,"two") :: nil : (int,string) alterlist,解压缩函数将创建一个类似 [(1,2),("one", "two")] 的列表。 这是我到目前为止得到的:

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;

但是我得到了一堆错误:

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>)

由于我是 ml 新手,所以我几乎不知道是什么原因造成的。非常感谢帮助!

【问题讨论】:

    标签: error-handling tuples ml operand


    【解决方案1】:

    我的建议是首先不要使用中缀运算符,因为它有点令人困惑。 不用先解决,以后再添加。

    这是一个没有中缀的解决方案:

      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;
    

    例如,这样的列表:["a", 1, "b", 2] 将由以下人员创建:

    element ("a", element (1, element ("b", element (2, Nil))));
    

    给你:

    val it = element ("a",element (1,element #)) : (string,int) alterlist
    

    * # 只是表示列表比显示的长。

    如果你想试试unzip it; 你会得到:

    val it = (["a","b"],[1,2]) : string list * int list
    

    如你所愿。

    现在尝试将元素更改为 :: 中缀运算符。

    祝你好运!

    【讨论】:

    • 欢迎您@BobSacamano,但如果答案对您有帮助,请将其标记为答案(复选标记)。
    • @BobSacamano ,答案附近有一个复选标记。单击它,然后也单击它附近的向上箭头。
    • 我勾选了它,但我没有足够的街头信誉来投票给你的答案。
    猜你喜欢
    • 2014-08-26
    • 1970-01-01
    • 2011-11-25
    • 2015-08-24
    • 2015-08-24
    • 2020-12-14
    • 2018-07-04
    • 2020-12-11
    相关资源
    最近更新 更多