2015-02-06 123 views

回答

2

下面是做到這一點的一種方法:

let list2 a b = [ a; b ] 

這適用於任意兩個值ab,相同類型的。它們可以是列表或其他任何東西:

$ ocaml 
     OCaml version 4.01.0 

# let list2 a b = [a; b];; 
val list2 : 'a -> 'a -> 'a list = <fun> 
# list2 [1;2] [3;4];; 
- : int list list = [[1; 2]; [3; 4]] 
# list2 "yes" "no";; 
- : string list = ["yes"; "no"] 
# 

(。如果你的兩個列表是不一樣的類型是不可能把它們放入一個列表OCaml的列表是均勻的 - 所有的元素具有相同的類型)

0

可以使用功能這樣做:

使用的append()直接將不會得到預期的result.See包含多個列表列表中的代碼段。 def foo(list1,list2): new_list =[] for i in range(0,len(list1)): new_list.append(list1[i]) for j in range(0,len(list2)): new_list.append(list2[j]) return new_list

+1

(OP要求提供OCaml解決方案。) – 2017-09-05 07:22:00

相關問題