Headline

Use of record types in Language:Haskell

Characteristics

A data model for flat companies is defined in terms of Haskell's record types. Such record types are essentially algebraic data types. We only use record types for compound data. Otherwise, we use Haskell's newtypes, which is a special form of algebraic data type. Other than that, the contribution is a simple variation on Contribution:haskellData which uses plain algebraic data types for all types.

Illustration

The data model looks like this:

{-| A data model for the 101companies System -}

module Company.Data where

-- | A company consists of name and employee list
data Company = Company { 
    getCompanyName :: Name,
    getEmployees :: [Employee]
  }
    deriving (Eq, Show, Read)

-- | An employee consists of name, address, and salary
data Employee = Employee {
    getEmployeeName :: Name,
    getAddress :: Address,
    getSalary :: Salary
  }
    deriving (Eq, Show, Read)

-- | Names as strings
newtype Name = Name String
  deriving (Eq, Show, Read)

-- | Addresses as strings
newtype Address = Address String
  deriving (Eq, Show, Read)

-- | Salaries as floats
newtype Salary = Salary { getFloat :: Float }
  deriving (Eq, Show, Read)

A sample company looks like this:

{- | Sample data of the 101companies System -}

module Company.Sample where

import Company.Data

-- | A sample company useful for basic tests
sampleCompany :: Company
sampleCompany = Company {
  getCompanyName = Name "Acme Corporation",
  getEmployees = [
    Employee (Name "Craig") (Address "Redmond") (Salary 123456),
    Employee (Name "Erik") (Address "Utrecht") (Salary 12345),
    Employee (Name "Ralf") (Address "Koblenz") (Salary 1234),
    Employee (Name "Ray") (Address "Redmond") (Salary 234567),
    Employee (Name "Klaus") (Address "Boston") (Salary 23456),
    Employee (Name "Karl") (Address "Riga") (Salary 2345),
    Employee (Name "Joe") (Address "Wifi City") (Salary 2344)
  ]
}

Feature:Total is implemented as follows:

{-| The operation of totaling all salaries of all employees in a company -}

module Company.Total where

import Company.Data

-- | Total all salaries in a company
total :: Company -> Float
total = sum . salaries

-- | Extract all salaries in a company
salaries :: Company -> [Float]
salaries = getSalaries . getEmployees
  where

    -- Extract all salaries of lists of employees
    getSalaries :: [Employee] -> [Float]
    getSalaries [] = []
    getSalaries (e:es) = getFloat (getSalary e) : getSalaries es

Relationships

  • The present contribution is a slightly more complex variation on Contribution:haskellEngineer in that it uses data types (in fact, record types) as opposed to type synonyms.
  • See also Contribution:haskellData, which uses plain data types instead of record types.

Architecture

See Contribution:haskellEngineer.

Usage

See https://github.com/101companies/101haskell/blob/master/README.md.

Metadata


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.