Google News
logo
C# - Interview Questions
What is Discriminated Union in F#?
It is a useful data structure. It helps to store heterogeneous data. The Union is used to represent tree data structures. It provides cases, and each case consists of heterogeneous data.
 
Example : 
type Calcualte =    
    | Add of val1 : int * val2 : int    
    | Multiply of val1 : int * val2 : int    
    
let compute vall =    
   match vall with    
     | Add (val1, val2) -> val1+val2    
     | Multiply (val1, val2)->val1*val2    
    
let addition = compute (Add(10,10))    
let multiplication = compute (Multiply(2,5))    
    
printf "Addition = %d\nMultiplication = %d" addition multiplication
Advertisement