Should I unpack a Maybe with maybe or pass value around I'm beginning (for the third time in 5 years) to learn Haskell and decided to do a &quot;More or less&quot; console game (game pick a random number, you then have to guess which).
First the code, then my question :
import System.Random  
import Text.Read(readMaybe)

main :: IO ()
main = do 
  gen &lt;- getStdGen
  let maxSecret = 1000
  let (secret, newGen) = randomR (1,maxSecret) gen :: (Int, StdGen)

  -- putStrLn $ show secret
  let oracle = compareToSecret secret
  askForNumber oracle 1 (1, maxSecret)


data Guess = SecretIsLess | SecretIsMore | Win deriving (Show)

askForNumber :: (Int -&gt; Guess) -&gt; Int -&gt; (Int, Int) -&gt; IO ()
askForNumber oracle tries range = do
  let (min, max) = range
  putStrLn $ &quot;Guess the number between &quot; ++ (show min) ++ &quot; and &quot; ++ (show max) ++ &quot; !&quot;
  guessInput &lt;- getLine
  let guess = readMaybe guessInput :: Maybe Int

  case (oracle) &lt;$&gt; guess of 
            Just SecretIsMore -&gt; do
                  putStrLn &quot;Guess more !&quot;
                  askForNumber oracle (tries+1) (maybe min id guess, max)
            Just SecretIsLess -&gt; do
                  putStrLn &quot;Guess less !&quot;
                  askForNumber oracle (tries+1) (min, maybe max id guess)
            Just Win -&gt; do
                  putStrLn &quot;You win !&quot;
                  putStrLn $ &quot;It took you &quot; ++ (show tries) ++ &quot; tries.&quot;
                  return ()
            Nothing -&gt; do
                  putStrLn &quot;That is not a number&quot;
                  askForNumber oracle tries range
    
compareToSecret :: Int -&gt; Int -&gt; Guess
compareToSecret secret guess
  | secret &gt; guess = SecretIsMore
  | secret &lt; guess = SecretIsLess
  | secret == guess = Win


I'd like to improve that code to make it idiomatic and learn in the process.
I made a choice : at first I had
compareToSecret :: Int -&gt; Int -&gt; (Guess, Int)
and in the case of I had something like
Just (SecretIsMore, newmin) -&gt; do askForNumber oracle (tries+1) (newmin, max)
Do you think it's better to kind of unpack with maybe there or to pass the value around and keep it pure ? Or is there another solution ?
Prelude
I think your code (sans bug) is fine. Your question is already a little nit-picky so my comments will mostly be nit-picks. I am not a Haskell expert either, so don't place too much authority in my comments. Evaluate them for yourself.
My comments are in a somewhat arbitrary order, but I've organized them with headers. The section pertaining specifically to your question is titled &quot;What to do with the case&quot;.
The revised code is included at the end.
The Guess datatype
A datatype that is used precisely like this already exists, it's called Ordering and can be found in Data.Ord (although I believe it's included by default). No need to reinvent the wheel here. Especially because you'll see later that you can use already-made functions that work with Ordering.
Input validation
You correctly check that the input is a well-formed number, but you don't check that it's between the min and max value of the range. Here's an example of how this bug manifests itself.
Guess the number between 1 and 1000 !
50
Guess more !
Guess the number between 50 and 1000 !
20
Guess more !
Guess the number between 20 and 1000 !

Notice how the value goes down. The fix, which I will put in the section addressing your questions about Maybe, is to add another case that deals with a well-formed, but invalid guess.
Syntax and other small details
min and max are built-in functions, it's best not to shadow them. I would call them minValue and maxValue.
You can pattern match in function definitions so that you don't need to unpack range – you can instead have
askForNumber oracle tries (minValue, maxValue) = do
...

Function application has the highest precedence in Haskell, so you don't need parens around (show min). (oracle) &lt;$&gt; guess doesn't need the parens either.
maybe value id = fromMaybe value, so use that instead. You can get fromMaybe from Data.Maybe, but it might be included by default.
oracle
If you switch to using Ordering, you can nix your compareToSecret function and define oracle = compare secret.
What to do with the case
I think it's kind of clunky to extract a value from a Maybe after you've cased on it, which is what you're doing right now. A big utility of case is knowing that your value must be of a certain form in each branch. In that branch, guess must be of the form Just _. While you as a programmer know this to be true, it's much more helpful to have the type system be able to prove this.
As I see it, there are two ways to handle your case statement.
The first is to have two nested case statements. The first branches on guess. If guess is Nothing, it prompts the user for another input. If guess is Just g, it cases on oracle g.
The second is to do a slight modification of what you're doing. Instead of fmapping oracle, you can fmap the function \g -&gt; (oracle g, g). This allows you to extract the guess safely without having to use Maybe. I believe this is what you were doing originally.
I prefer to avoid nesting cases when I can, so I opted for the second one.
If you would also like to validate the input (I chose to), then you will want to add a case at the beginning matching Just (_, guess) – i.e. one that ignores the comparison – to deal with invalid guesses. This pattern, however, will match both invalid and valid guesses. We need to add a guard that asserts that guess &gt; max or guess &lt; min. This looks like:
    ...
    Just (_, guess) | guess &lt; min || guess &gt; max -&gt; do
      putStrLn &quot;Guess out of range&quot;
      askForNumber oracle tries range
    ...

Small note on using oracle
I use the function \g -&gt; (oracle g, g) which might feel slightly clunky. Indeed, there are a few alternatives, but I warn you that they are somewhat arcane. In general, I would avoid using &quot;cute&quot; or &quot;slick&quot; code like this unless you know everyone who sets eyes on the code will understand it.
All of the below are equivalent (for your use case):
\g -&gt; (oracle g, g)    -- plain definition
oracle &amp;&amp;&amp; id          -- (&amp;&amp;&amp;) needs to be imported from Control.Arrow
(,) =&lt;&lt; oracle         -- using the Reader Monad
(,) &lt;$&gt; oracle &lt;*&gt; id  -- using the Reader Applicative

I would be most inclined to use oracle &amp;&amp;&amp; id because it'd be the most recognizable, but even it is a little arcane. I pretty much only included the last example because you'll see code that looks like f &lt;$&gt; g &lt;*&gt; h &lt;*&gt; i … a lot; it's a common idiom for other Applicatives (but I would advise against using it in this case). No matter what people say or do, there's nothing wrong with writing &quot;plain&quot; or &quot;simple&quot; Haskell.
Revised code
You'll find that not too much is different.
import System.Random  
import Text.Read (readMaybe)

main :: IO ()
main = do 
  gen &lt;- getStdGen
  let maxSecret = 1000
  let (secret, newGen) = randomR (1,maxSecret) gen :: (Int, StdGen)

  -- putStrLn $ show secret
  let oracle = compare secret
  askForNumber oracle 1 (1, maxSecret)

askForNumber :: (Int -&gt; Ordering) -&gt; Int -&gt; (Int, Int) -&gt; IO ()
askForNumber oracle tries range@(minValue, maxValue) = do
  putStrLn $ &quot;Guess the number between &quot; ++ show minValue ++ &quot; and &quot; ++ show maxValue ++ &quot; !&quot;
  guessInput &lt;- getLine
  let guessMaybe = readMaybe guessInput :: Maybe Int

  case (\guess -&gt; (oracle guess, guess)) &lt;$&gt; guessMaybe of 
    Nothing -&gt; do
      putStrLn &quot;That is not a number&quot;
      askForNumber oracle tries range
    Just (_, guess) | guess &lt; minValue || guess &gt; maxValue -&gt; do
      putStrLn &quot;Guess out of range&quot;
      askForNumber oracle tries range
    Just (LT, guess) -&gt; do
      putStrLn &quot;Guess less !&quot;
      askForNumber oracle (tries+1) (minValue, guess)
    Just (GT, guess) -&gt; do
      putStrLn &quot;Guess more !&quot;
      askForNumber oracle (tries+1) (guess, maxValue)
    Just (EQ, _) -&gt; do
      putStrLn &quot;You win !&quot;
      putStrLn $ &quot;It took you &quot; ++ (show tries) ++ &quot; tries.&quot;
```