2014-04-08 70 views
0
let add_information info = match info with 
    (int * int * int) -> int + int + int 
;; 

let result = add_information (1, 2, 3);; 

print_int result;; (* should print 6 here *) 

我相信你可以像列表一樣匹配元組。只是不確定確切的格式。OCaml匹配元組

回答

4

獨立與, S:

let add_info = function 
    | a, b, c -> a + b + c 

因爲元組匹配是無可辯駁的 - 也就是說,沒有辦法進行匹配失敗 - 你也可以綁定元組let或函數的參數:

let a, b, c = calc_tuple() in a + b + c 
+2

或在函數參數中匹配它們,如'let add_information(a,b,c)= ...' – nlucaroni

+0

感謝您的答案 – user3509086

1

你也可以聲明一個新變量說法是這樣的:

let d,e,f = info in 
    d+e+f;;