Challenge 2: Writer Monad
Test your knowledge by solving a problem requiring the Writer monad.
Problem statement
In a Writer monad environment, sequential chains are log-oriented. In addition to the computed values, the Writer monad also generates useful side data.
For this challenge, some string operations are given below in the table. Your task is to log all operations in sequential chains with a Writer monad and generate side data. The encoded representations of the operations are given below in sequential order:
Order | Operation | Encoded representation |
---|---|---|
1 | Placing a given string ($str ) in an environment. |
place "$str" in environment |
2 | Computing the letter count of the string ($str ). |
compute "$str" letter count |
3 | Squaring the letter count of the string ($str ). |
compute square of "$str" letter count |
Requirements
Write all your logic inside a function named WriterChallenge()
. The function must be able to take a string as a parameter and return the result of the sequential chain, along with side data.
Use the function defined below in your composition for all three operations. It returns a function that computes the value of the function f
and logs it along with the side data msg
.
Get hands-on with 1400+ tech skills courses.