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

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

import           Solutions.P40 (goldbach)
import           System.IO

-- | Implementation of 'Problems.P74.askGoldbach'' without do notation.
askGoldbach :: Handle -> Handle -> IO ()
askGoldbach :: Handle -> Handle -> IO ()
askGoldbach Handle
hIn Handle
hOut =
  IO String -> (String -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
(>>=) (Handle -> IO String
hGetLine Handle
hIn)
        (\String
s -> let n :: Int
n = String -> Int
forall a. Read a => String -> a
read String
s :: Int in
                 let (Int
a,Int
b) = Int -> (Int, Int)
forall a. Integral a => a -> (a, a)
goldbach Int
n in
                   IO () -> (() -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
(>>=) (Handle -> String -> IO ()
hPutStr Handle
hOut (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
n)
                         (\()
_ -> IO () -> (() -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
(>>=) (Handle -> String -> IO ()
hPutStr Handle
hOut String
"=")
                                      (\()
_ -> IO () -> (() -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
(>>=) (Handle -> String -> IO ()
hPutStr Handle
hOut (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
a)
                                                   (\()
_ -> IO () -> (() -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
(>>=) (Handle -> String -> IO ()
hPutStr Handle
hOut String
"+")
                                                                (IO () -> () -> IO ()
forall a b. a -> b -> a
const (IO () -> () -> IO ()) -> IO () -> () -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle -> Int -> IO ()
forall a. Show a => Handle -> a -> IO ()
hPrint Handle
hOut Int
b)))))