2016-02-28 6 views
6

Diciamo che abbiamo un uniontype in F #:Come fare una funzione che controlla, se le etichette dei sindacati discriminati corrispondono?

type Example = 
    |FirstLabel of int 
    |SecondLabel of int 
    |ThirdLabel of int 

Come è possibile creare una funzione, che prende 2 parametri del tipo "Esempio" e restituisce true, se i due parametri condividono la stessa etichetta e altrimenti restituisce falso? Voglio che questa funzione restituisca questi risultati indipendentemente dal valore degli interi.

Quindi, se abbiamo parametro1 e parametro2 con

val parameter1 : Example = SecondLabel 2 

e

val parameter2 : Example = Secondlabel 5 

la funzione sarebbe tornato true

non riuscivo a trovare una risposta per questa domanda, anche con la ricerca accuratamente. Forse ho cercato male. Quindi potresti darmi una fonte per risolvere questi problemi?

risposta

7
let sameLabels x y = 
    match x, y with 
    | FirstLabel _ , FirstLabel _ 
    | SecondLabel _, SecondLabel _ 
    | ThirdLabel _ , ThirdLabel _ -> true 
    | _ -> false 
Problemi correlati