Contribution:
haskellWriter
Headline
Logging in Haskell with the Writer monad
Characteristics
Salary changes are logged in a Language:Haskell-based implementation with the help of a writer monad. Compared to a non-monadic implementation, the code is more concise. Details of logging are localized such that they only surface in the context of code that actually changes salaries.
Illustration
See Contribution:haskellLogging for a simpler, non-monadic implementation.
The present, monadic implementation differs only with regard to the cut function:
cut :: Company -> Writer Log Company
cut (Company n ds) =
do
ds' <- mapM cutD ds
return (Company n ds')
where
cutD :: Department -> Writer Log Department
cutD (Department n m ds es) =
do
m' <- cutE m
ds' <- mapM cutD ds
es' <- mapM cutE es
return (Department n m' ds' es')
where
cutE :: Employee -> Writer Log Employee
cutE (Employee n a s) =
do
let s' = s/2
let log = [ LogEntry {
name = n,
oldSalary = s,
newSalary = s'
} ]
tell log
return (Employee n a s')
Thus, the family of functions uses a writer monad in the result types. The sub-traversals are all composed by monadic bind (possibly expressed in do-notation). The function for processing departments totally abstracts from the fact that logging is involved. In fact, that function could be defined to be parametrically polymorphic in the monad at hand.
Relationships
- See Contribution:haskellComposition for the corresponding contribution that does not yet involve logging. The data model is preserved in the present contribution, but the functions for cutting salaries had to be rewritten since the logging concern crosscuts the function.
- See Contribution:haskellLogging for a variation on the present contribution which does not yet use monadic style.
Architecture
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.