How to resolve the algorithm Guess the number step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Guess the number step by step in the Haskell programming language

Table of Contents

Problem Statement

Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   and the program exits. A   conditional loop   may be used to repeat the guessing until the user is correct.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Guess the number step by step in the Haskell programming language

1st Code:

  • Goal: Continuously prompt the user for a guess until they guess a secret number.

  • Key Concepts:

    • Control.Monad and System.Random: Used for IO and random number generation.
    • until_ function: Repeats the specified action until a predicate is true.
    • answerIs function: Compares the user's guess to the secret number and returns True if correct.
  • Implementation:

    • A secret number is randomly generated between 1 and 10.
    • The user is asked to guess the number.
    • The guess is checked against the secret number using the answerIs function.
    • If correct, the user is notified and the program ends.
    • If incorrect, the user is notified and is asked to guess again.
    • The until_ function repeats the guessing process until a correct guess is made.

2nd Code:

  • Goal: Similar to the 1st code, but with a slightly different approach.

  • Key Concepts:

    • System.Random: Used for random number generation.
    • gameloop function: Defines the game loop that repeatedly prompts the user for guesses until a correct guess is made.
  • Implementation:

    • A secret number is randomly generated between 1 and 10.
    • The gameloop function is called with the secret number as an argument.
    • gameloop prompts the user for a guess.
    • The guess is checked against the secret number.
    • If correct, the user is notified and the gameloop function ends.
    • If incorrect, the user is notified and gameloop is called again to continue the game loop.

Source code in the haskell programming language

import Control.Monad
import System.Random

-- Repeat the action until the predicate is true.
until_ act pred = act >>= pred >>= flip unless (until_ act pred)

answerIs ans guess 
    | ans == guess = putStrLn "You got it!" >> return True
    | otherwise = putStrLn "Nope. Guess again." >> return False

ask = liftM read getLine

main = do
  ans <- randomRIO (1,10) :: IO Int
  putStrLn "Try to guess my secret number between 1 and 10."
  ask `until_` answerIs ans


import System.Random

main = randomRIO (1,10) >>= gameloop

gameloop :: Int -> IO ()
gameloop r = do	
	i <- fmap read getLine
	if i == r 
	  then putStrLn "You got it!"
	  else putStrLn "Nope. Guess again." >> gameloop r


  

You may also check:How to resolve the algorithm Greatest common divisor step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Sort disjoint sublist step by step in the Ruby programming language
You may also check:How to resolve the algorithm Burrows–Wheeler transform step by step in the zkl programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the Standard ML programming language