Contribution:
haskellList
Headline
List processing with map and friends in Language:Haskell
Characteristics
The contribution demonstrates functional programming in Language:Haskell. List processing with map and friends is put to work.
Illustration
Higher-order functions for list processing enable the concise implementation of list processing functionality in a regular manner, eliminating the need for problem-specific functions to be recursive or to perform pattern matching on lists. For comparison, consider the following code for totaling salaries without the use of map and friends, as part of Contribution:haskellEngineer:
-- | Total all salaries in a company
total :: Company -> Float
total = sum . salaries
where
-- Extract all salaries in a company
salaries :: Company -> [Salary]
salaries (_, es) = getSalaries es
where
-- Extract all salaries of lists of employees
getSalaries :: [Employee] -> [Salary]
getSalaries [] = []
getSalaries (e:es) = getSalary e : getSalaries es
where
-- Extract the salary from an employee
getSalary :: Employee -> Salary
getSalary (_, _, s) = s
Specifically, the helper function getSalaries should be regarded as "boilerplate code" because it duplicates a routine scheme of recursion which is readily generalized by the map function. Thus, Contribution:haskellList puts to work the map function and revises the code for totaling salaries as follows:
-- | Total all salaries in a company
total :: Company -> Float
total = sum . salaries
where
-- Extract all salaries in a company
salaries :: Company -> [Salary]
salaries (n, es) = map getSalary es
where
-- Extract the salary from an employee
getSalary :: Employee -> Float
getSalary (_, _, s) = s
Thus, the map function is used to iterate the getSalary function over lists of employees. Since map takes a function as argument, it is clearly a higher-order function. The result of mapping is a list which is summed up with the sum function, which in itself, is not a higher-order function. However, it is worth noticing that the sum function is actually defined (or definable) in terms of another higher-order function for list processing, i.e., the fold function, more specifically Haskell's foldr function.
Relationships
- The present contribution revises Contribution:haskellEngineer to put to work map and friends.
- See Contribution:haskellLambda for a variation which exercises anonymous functions.
Architecture
See Contribution:haskellEngineer.
Usage
User contributions
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.