How to resolve the algorithm Guess the number/With feedback (player) step by step in the Haskell programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Guess the number/With feedback (player) step by step in the Haskell programming language
Table of Contents
Problem Statement
Write a player for the game that follows the following rules: The computer should guess intelligently based on the accumulated scores given. One way is to use a Binary search based algorithm.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Guess the number/With feedback (player) step by step in the Haskell programming language
Detailed Haskell Source Code Explanation:
Source Code 1:
-
main
Function:- This is the entry point of the program.
- It prompts the user to enter the range of numbers (from and to).
- It reads the user input and checks if it's valid using the
reads
function. - If the input is valid, it calls the
loop
function with the start and end of the range. - If the input is invalid, it prints an error message and calls
main
again to restart the program.
-
loop
Function:- This function takes the start and end of the range as input.
- It calculates the midpoint (guess) of the range and prints it to the user, asking them if it's the correct value.
- It reads the user's response and takes appropriate action based on the response:
- If the guess is correct ("c"), it prints "Awesome!"
- If the guess is too low ("l"), it calls
loop
again with the updated range (from the start to the guess). - If the guess is too high ("h"), it calls
loop
again with the updated range (from the guess to the end). - If the user's input is invalid, it prints an error message and calls
loop
again with the same range.
Source Code 2 (Alternative Version):
main
Function:- This function is simpler and uses more concise syntax.
- It sets up a recursive function
f
with a default range of 0 to 100. - The
f
function computes the midpoint of the range, prints it to the user, and prompts them for input. - It then handles the user's response in the same way as the
loop
function in Source Code 1:- If the guess is correct ("c"), it prints "Yay!"
- If the guess is too low ("l"), it calls
f
with the updated range (from the start to the midpoint). - If the guess is too high ("h"), it calls
f
with the updated range (from the midpoint to the end).
Source code in the haskell programming language
main :: IO ()
main = do
putStrLn "Please enter the range:"
putStr "From: "
from <- getLine
putStr "To: "
to <- getLine
case (from, to) of
(_) | [(from', "")] <- reads from
, [(to' , "")] <- reads to
, from' < to' -> loop from' to'
(_) -> putStrLn "Invalid input." >> main
loop :: Integer -> Integer -> IO ()
loop from to = do
let guess = (to + from) `div` 2
putStrLn $ "Is it " ++ show guess ++ "? ((l)ower, (c)orrect, (h)igher)"
answer <- getLine
case answer of
"c" -> putStrLn "Awesome!"
"l" -> loop from guess
"h" -> loop guess to
(_) -> putStrLn "Invalid answer." >> loop from to
main = f 0 100
where f x y = let g = div (x + y) 2 in
putStrLn (show g ++ "? (l,h,c)") >>
getLine >>= \a -> case a of
"l" -> f x g
"h" -> f g y
"c" -> putStrLn "Yay!"
You may also check:How to resolve the algorithm Array concatenation step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the D programming language
You may also check:How to resolve the algorithm Handle a signal step by step in the Erlang programming language
You may also check:How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Run BASIC programming language