2016-02-29 72 views
-1

我想在ocaml中创建一个对列表,但问题是当列表的长度不同时,我不热点做出对(a,b),当其中一个元素不存在时。如何合并Lucene索引文件?

+3

请添加示例输入和期望输出,也请分享您已经编写的代码。 – Istvan

+0

亚历克斯,假装你是我们,你给了我们什么除了“请为我写这个代码” –

回答

0

最有可能的,你必须创建一个类来封装不同长度的情况下,

type ('a,'b) combined = Both of 'a * 'b | Left of 'a | Right of 'b 

let rec zipwith xs ys = match xs,ys with 
    | x::xs,y::ys -> Both (x,y) :: (zipwith xs ys) 
    | x::xs, [] -> Left x  :: (zipwith xs ys) 
    | [], y::ys -> Right y :: (zipwith xs ys) 
    | [], []  -> [] 

# zipwith [1;2;3] [1;2];; 
- : (int, int) combined list = [Both (1, 1); Both (2, 2); Left 3] 

你必须做出关于尾呼吁这对长列表工作的优化,但这是如何解决问题的开始。