Meaning of Discriminated Union in F# -


i understand meaning of "discriminated" , "union" in standalone contexts, @ loss when comes f#'s "discriminated union".

fyi, english not first language , not @ math either. hope out there can shed light on feature of f#. please.

what need know is:

  • the use case discriminated union. used for?
  • it's equivalent other oop feature/terms. if there's any.
  • is set operation use venn diagrams represent data?

or can me pointing links.

a discriminated union union of 2 sets can tell set item belonged to; if same thing, can discriminate between them, i.e. tell them apart.

for instance, if have discriminated union of 2 sets of integers, both containing number 2, can discriminate between 2s because know original set came from.

as example, consider points in 2-dimensional plane.

these can expressed pair of reals in 2 ways, either using rectangular (or cartesian) coordinates (x coordinate, y coordinate) or using polar coordinates (angle of rotation, distance).

but if gives pair of numbers, wouldn't know meant.

we can form discriminated union, though:

type point2d =      | rectangular of real * real     | polar of real * real 

now point2d value makes intended interpretation clear, , compiler can make sure don't try mix representations or fail handle case.

in oo setting, build class hierarchy abstract base class, or have "kind" member inspect.

it's more common form unions of different types, though - if wrote interpreter programming language might have looks like

type expression =      | integer of int     | string of string     | identifier of string     | operator of string     | conditional of expression * expression * expression     | definition of string * expression 

and on.

discriminated unions called "sum types", , tuples called "product types".
these terms come discipline of type algebra, , resultant types called "algebraic data types".
(when functional programmers mention "adt", "a" "algebraic", not "abstract".)


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -