How do I convert a string to a list of Maybe Int

haskell

Solution

If you would like all non digits to be converted to `Nothing`, you can use guards and `fmap`

import Data.Char

charToMaybeInt :: Char -> Maybe Int
charToMaybeInt  x 
 | isDigit x = Just $ digitToInt x
 | otherwise = Nothing

main = putStrLn $ show $ fmap charToMaybeInt "13.2..2"

Using guards is, from my non-expert understanding, a bit more idiomatic than using `if`/`else`.

Problem

How would I go about converting a string like this `"13.2..2"` to a list like this `[Just 1, Just 3, Nothing, Just 2, Nothing, Nothing, Just 2]` I have had a look at `digitToInt` but it does not take care of `Maybe Int`. Is there a way I could maybe modify `digitToInt` to handle `Maybe Int`?

Original source