Headline

A type of records

Illustration

Record types are available or conveniently expressible in many programming languages.

Record types in Haskell

Language:Haskell provides syntactic sugar for algebraic data types such that constructor components can be be labeled so that they can be accessed in a record-like manner. Consider, for example, the following algebraic data type for points:

data Point = Point Float Float

The data constructor can be defined using record notation instead:

data Point = Point { getX :: Float, getY :: Float }

Here is an example of constructing a record:

myPoint :: Point
myPoint = Point { getX = 42, getY = 88 }

Here is an example of accessing record components:

-- Compute the distance between two points
distance :: Point -> Point -> Float
distance p1 p2 = sqrt (deltax^2+deltay^2)
  where
    deltax = abs (getX p1 - getX p2) 
    deltay = abs (getY p1 - getY p2) 

The constructors and component selectors are of these types:

> :t Point
Point :: Float -> Float -> Point
> :t getX
getX :: Point -> Float
> :t getY
getY :: Point -> Float

We can also update records in the sense that we can construct new records from existing records by updating specific components. For instance:

myPoint' :: Point
myPoint' = myPoint { getY = 77 }

For what it matters, the position-based approach to construction, as with normal algebraic data types, can also be used. Thus, component selectors can be omitted at will. This is demonstrated with constructing the same point as above:

myPoint :: Point
myPoint = Point 42 88

Component selectors are also omitted during pattern matching:

-- Represent point as pair
toPair :: Point -> (Float, Float)
toPair (Point x y) = (x, y)

The record notation can also be used in algebraic data types with multiple constructors, e.g.:

data Shape = Circle { getRadius :: Float }
           | Rectangle { getWidth :: Float, getHeight :: Float }


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.