Everyone_Else
Everyone_Else

Reputation: 3264

How to pattern match on input tuples ocaml

For example, I have the code

let add_next (data: int * int * int list) : int =

However,the word data is really ambiguous, and I'd like to be able to name the first two integers and then the list in the function header, while preserving the type of int * int * int list. How can this be done?

Upvotes: 1

Views: 493

Answers (1)

Marth
Marth

Reputation: 24812

OCaml version 4.01.0

#  let add_next ((first, second, l): int * int * int list) : int = first;;
val add_next : int * int * int list -> int = <fun>

If you need to pass the data tuple around without having to rebuild it, use the as construct:

# let add_next ((first, second, l) as data: int * int * int list) : int =
    ignore data;
    first;;
val add_next : int * int * int list -> int = <fun>

Upvotes: 2

Related Questions