{- |
Description: Maybe monad
Copyright: Copyright (C) 2023 Yoo Chung
License: GPL-3.0-or-later
Maintainer: dev@chungyc.org

Some solutions to "Problems.P75" of Ninety-Nine Haskell "Problems".
-}
module Solutions.P75 (maybeGoldbach) where

import           Control.Monad (guard)
import           Solutions.P40 (goldbach)
import           Text.Read     (readMaybe)

-- | Implementation of 'Problems.P75.maybeGoldbach'' with do notation.
maybeGoldbach :: String -> Maybe (Integer, (Integer, Integer))
maybeGoldbach :: String -> Maybe (Integer, (Integer, Integer))
maybeGoldbach String
s = do
  Integer
n <- String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe String
s
  Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Maybe ()) -> Bool -> Maybe ()
forall a b. (a -> b) -> a -> b
$ Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
2
  Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Maybe ()) -> Bool -> Maybe ()
forall a b. (a -> b) -> a -> b
$ Integer -> Bool
forall a. Integral a => a -> Bool
even Integer
n
  (Integer, (Integer, Integer))
-> Maybe (Integer, (Integer, Integer))
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer
n, Integer -> (Integer, Integer)
forall a. Integral a => a -> (a, a)
goldbach Integer
n)