【发布时间】:2015-08-24 03:05:26
【问题描述】:
我需要“解压缩”一个元组列表,即使我尝试使用 unzip 函数,它也没有达到我想要的效果。事实上,据我了解,unzip 函数会返回一个列表元组,但我想要的是这个。
给出这样的元组列表:
L1 = [(("s0","l0"),("l0","s1")),(("s1","l1"),("l1","s2"))]
类型:
(string*string)*(string*string) list
该函数应该返回一个由解压缩的元组组成的列表:
L2 = [("s0","l0"),("l0","s1"),("s1","l1"),("l1","s2")]
类型:
(string*string) list
我找到的唯一解决方案是:
fun f(h as (x,y,z),nil) = [(x,x^y^z)]@[(x^y^z,z)]
|f (h as (x,y,z),t::rest) =
let val i = ref 0
in if !i=0 then (i := !i+1; (x,x^y^z)::f(h,t::rest)) else (i := !i-1;(x^y^z,z)::f(t,rest)) end;
我认为它可以工作,但是当我运行该功能时,系统崩溃了。
【问题讨论】: