简体   繁体   中英

OCaml how to manipulate tuples?

Let's say I have a tuple:

let x = (1,3)

I want to add 1 to only the first value of the tuple. How would I do that?

您使用模式匹配来解构元组,然后构造更新的元组:

let (x1, x2) = x in (x1 + 1, x2)

Patterm matching is a typical idiom. Another way would be with fst and snd :

# let x = (1,3);;
val x : int * int = (1, 3)
# let y = (fst x + 1, snd x);;
val y : int * int = (2, 3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM