Headline

Use of algebraic data types in Language:Haskell

Characteristics

A data model for flat companies is defined in terms of Haskell's algebraic data types. Other than that, the contribution is a simple variation on Contribution:haskellEngineer. The systematic use of algebraic data types implies nominal type distinctions in the sense of nominal typing. For instance, arbitrary floats cannot be confused with salaries which are floats only structurally. The different kinds of names for companies, departments, and employees are not distinguished, even though this would also be possible, in principle.

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 Name [Employee]
  deriving (Eq, Show, Read)

-- | An employee consists of name, address, and salary
data Employee = Employee Name Address Salary
  deriving (Eq, Show, Read)

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

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

-- | Salaries as floats
data Salary = Salary 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
  (Name "Acme Corporation")
  [
    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 (Company _ es) = getSalaries es
  where

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

        -- Extract the salary from an employee
        getSalary :: Employee -> Float
        getSalary (Employee _ _ (Salary s)) = s

Relationships

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.