PureScript Edition
A set of challenges for jump starting your understanding of monads.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Now that you have a Maybe
type, you need to use it. So now use it to build safe versions of several common functions specified by the Prelude.
headMay :: forall a. Array a -> Maybe a
tailMay :: forall a. Array a -> Maybe (Array a)
lookupMay :: forall a b. Eq a => a -> Array (Tuple a b) -> Maybe b
divMay :: forall a. Eq a => EuclideanRing a => a -> a -> Maybe a
maximumMay :: forall a. Ord a => Array a -> Maybe a
minimumMay :: forall a. Ord a => Array a -> Maybe a
The functions headMay
and tailMay
are “safe” versions of the well known head
and tail
functions. The former returns the first element of a list or Nothing
if the list is empty. The latter returns a list containing all but the first element of a list, or Nothing
if the list is empty. The lookupMay
function is kind of like the lookup
for a map. Find the first tuple in the list where the first element is the equal to the passed in value and return the second element. If there is no matching a
, then return Nothing
. The divMay
function should return Nothing
if you’re dividing by 0
and the result of the division otherwise. (You might want to review the documentation on EuclideanRing.) maximumMay
and minimumMay
calculate the maximum and minimum respectively of all the numbers in the list, but if the list is empty they return Nothing
.