How to take product of two list in OCaml?

风格不统一 提交于 2019-12-30 10:04:13

问题


I have two lists :

let a = ["a";"b"];
let b = ["c";"d"];

I want an output list c such as :

c = ["a";"c";"a";"d";"b";"c";"b";"d"];

How to do it in ocaml as lists are immutable? I am new to it.


回答1:


You would return a new list. If you really are interested in the cartesian product of the lists, then this should be enough:

let cartesian l l' = 
  List.concat (List.map (fun e -> List.map (fun e' -> (e,e')) l') l)

# cartesian ["a";"b"] ["c";"d"];;
- : (string * string) list = [("a", "c"); ("a", "d"); ("b", "c"); ("b", "d")]

If you need that strange flat structure instead, you can use an additional list concatenation.

let flat_cartesian l l' = 
  List.concat (List.concat (
    List.map (fun e -> List.map (fun e' -> [e;e']) l') l))



回答2:


If you do not want to use concatenation, because this is not a tail recursive operation, you can use the following (which should be more efficient):

let product l1 l2 =
  List.rev (
   List.fold_left
    (fun x a ->
      List.fold_left
       (fun y b ->
         b::a::y
       )
       x
       l2
   )
   []
   l1
 )
;;

For the cartesian product, just change

b::a::y

into

(a,b)::y



回答3:


I would break the problem into two sub problems:

  • Firstly consider a function appendeach with takes a value and a list and returns the result of adding that value infront of each item in the list

    let rec appendeach x lst = match lst with [] -> [] 
                                            | hd::tl -> x::hd::(appendeach x tl);;
    
  • Then consider a function product with takes two lists and calls appendeach for each item in the first list and the whole second list

    let rec product lst1 lst2 = match lst1 with [] -> [] | 
                                             hd::tl -> (appendeach hd lst2)@(product tl lst2);;
    


来源:https://stackoverflow.com/questions/10893521/how-to-take-product-of-two-list-in-ocaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!