import System.Random
main :: IO()
main = do
putStrLn "Enter range for guessing game"
putStrLn "Enter lower range: "
lRange <- getLine
putStrLn "Enter higher range: "
hRange <- getLine
putStrLn "You try to guess it."
play (randomInt (read lRange) (read hRange)) 0
play :: Integer -> Integer -> IO ()
play number guesses =
do
guess <- getLine
let guessInt = read guess :: Integer
let newGuesses = guesses + 1
putStrLn $ reactToGuess guessInt number
if guessInt == number
then putStrLn ("Correct!!! You found the number in " ++ show newGuesses ++ " guesses!")
else play number newGuesses
reactToGuess :: Integer -> Integer -> String
reactToGuess x y
| x < y = "You are too low."
| x > y = "You are too high."
| otherwise = ""
randomInt :: Integer -> Integer -> Integer
randomInt l h = fst $ randomR (l,h) (mkStdGen 0)
I have bought the “Learn You a Haskell for Great Good!” book.
Only about 1/3 into the book. Trying to write something myself.
Is this how one would/should write this program in haskell?
Two questions focused really but I would like broad feedback on the whole program if possible.
1.
In reactToGuess could otherwise not lead to a empty line being printed?
| otherwise = ""
-
String contataion. Is ++ operator or <> operator best?
("Correct!!! You found the number in " ++ show newGuesses ++ " guesses!")
New contributor
Landin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.