Headline

Data variation in Language:Haskell with algebraic data types

Characteristics

The data model leverages data variation for companies with departmental nesting. Thus, an algebraic data type is used for subunits of departments (i.e., employees and departments) so that recursive nesting can be expressed. The algebraic data type needs indeed two data constructors. Thus, data variation is exercised, but see Contribution:haskellComposition for an alternative without data variation.

Illustration

The data model leverages an algebraic data type for subunits of departments; in this manner recursion is enabled:

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

module Company.Data where

-- | A company consists of name and top-level departments
type Company = (Name, [Department])

-- | A department consists of name, manager, and sub-units
type Department = (Name, Manager, [SubUnit])

-- | An employee consists of name, address, and salary
type Employee = (Name, Address, Salary)

-- | A sub-unit is either an employee or a sub-department
data SubUnit = EUnit Employee | DUnit Department
 deriving (Eq, Read, Show)

-- | Managers as employees
type Manager = Employee

-- | Names of companies, departments, and employees
type Name = String

-- | Addresses as strings
type Address = String

-- | Salaries as floats
type Salary = Float

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 =
  ( "Acme Corporation",
    [
      ( "Research",
        ("Craig", "Redmond", 123456),
        [
          EUnit ("Erik", "Utrecht", 12345),
          EUnit ("Ralf", "Koblenz", 1234)
        ]
      ),
      ( "Development",
        ("Ray", "Redmond", 234567),
        [
          DUnit (
            "Dev1",
            ("Klaus", "Boston", 23456),
            [
              DUnit (
                "Dev1.1",
                ("Karl", "Riga", 2345),
                [EUnit ("Joe", "Wifi City", 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 (_, ds) = sum (map totalDepartment ds)
  where
    -- Total salaries in a department
    totalDepartment :: Department -> Float
    totalDepartment (_, m, sus)
        = getSalary m
        + sum (map totalSubunit sus)
      where
        -- Total salaries in a subunit
        totalSubunit :: SubUnit -> Float
        totalSubunit (EUnit e) = getSalary e
        totalSubunit (DUnit d) = totalDepartment d

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

The following salary total is computed for the sample company:

399747.0

Relationships

See Contribution:haskellComposition for a contribution with a similar data model such that data variation is not exercised, but only data composition.

Architecture

See Contribution:haskellComposition.

Usage

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


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.