The Monad Challenges

A set of challenges for jump starting your understanding of monads.

Outline

Set 1: Random Numbers

Set 2: Failing Computations

Set 3: Combinations

Set 4: Common Abstraction

Set 5: Do Notation

This project is maintained by mightybyte

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Do Notation – Set 2

As with the previous set, you will want to create a Monad instance for your Maybe data type.

Once that is accomplished, rewrite the following functions using do syntax:

queryGreek :: GreekData -> String -> Maybe Double
addSalaries :: [(String, Integer)] -> String -> String -> Maybe Integer
tailProd :: Num a => [a] -> Maybe a
tailSum :: Num a => [a] -> Maybe a
tailMax :: Ord a => [a] -> Maybe a

You should import Set2 so that you can use the helper functions you wrote in that set.

You can test queryGreek with the following assertions:

queryGreek greekDataA "alpha" == Just 2.0
queryGreek greekDataA "beta" == Nothing
queryGreek greekDataA "gamma" == Just 3.3333333333333335
queryGreek greekDataA "delta" == Nothing
queryGreek greekDataA "zeta" == Nothing

queryGreek greekDataB "rho" == Nothing
queryGreek greekDataB "phi" == Just 0.24528301886792453
queryGreek greekDataB "chi" == Just 9.095238095238095
queryGreek greekDataB "psi" == Nothing
queryGreek greekDataB "omega" == Just 24.0

Previous Page - Next Page