Take the 2-minute tour ×
type A =
    id: int;

I wish i could do this

let Add (x:A) (y:A) =
     match x,y with
      | {x.id=0,y.id=1} -> ...

And is there any trick to define the function if i don't care about the order of x and y (so that the function is symmetric) also i don't mind whether the parameter is a tuple (x,y) or a higher order function x,y

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted
let add (x: A) (y: A) =
     match x.id, y.id with
     | 0, 1 | 1, 0 -> (* do some thing *)
     | _ -> (* do some thing else *)

If you only care about a field, do pattern matching directly on it. And you can use Or pattern to have a symmetric function.

share|improve this answer
add comment

Another syntax for this is:

let add x y =
    match x, y with
    | {id = 0}, {id = 1} | {id = 1}, {id = 0} -> ..
    | _ -> ..

See Record Pattern section at http://msdn.microsoft.com/en-us/library/dd547125.aspx

share|improve this answer
This should be the correct answer as it's matching on the records and not on integers. –  gradbot Sep 3 '12 at 21:46
@gradbot: I'm hesistant to use this because according to OP's question, there are more fields for which we don't know. –  pad Sep 3 '12 at 22:04
@pad, quoting the manual: "The record pattern is used to decompose records to extract the values of fields. The pattern does not have to reference all fields of the record; any omitted fields just do not participate in matching and are not extracted." –  toyvo Sep 4 '12 at 15:53
@toyvo: Thanks, that's what I didn't know. –  pad Sep 4 '12 at 16:09
@gradbot is there any difference whether i match on records or on integers? Why is this answer better than pad's ones? –  colinfang Sep 7 '12 at 21:58
show 2 more comments

Your Answer


