Skip to content

Instantly share code, notes, and snippets.

@bisserlis
Created November 3, 2014 13:36
Show Gist options
  • Save bisserlis/c90478b3d8797548d7ec to your computer and use it in GitHub Desktop.
Save bisserlis/c90478b3d8797548d7ec to your computer and use it in GitHub Desktop.
Number Guesser for JF
-- From e-mail:
-- 1. You must use a 'recursive' function to prompt and check guesses.
-- 2. You should have a running tally of guesses.
-- 3. The program should be able to handle invalid numbers by increasing the guesses.
-- Optional:
-- 1. You can display a list of numbers to choose from and with each proper guess the list should remove the guessed number.
--
-- Approximate effort: 4 minutes
import Data.List (delete)
import System.Random (randomRIO)
main :: IO ()
main = do
putStrLn "Guess the number from 1-10."
secret <- randomRIO (1, 10) :: IO Int
guess 1 (show secret) $ map show ([1..10] :: [Int])
guess :: Int -> String -> [String] -> IO ()
guess try secret range = do
putStrLn $ "Remaining numbers: " ++ unwords range
input <- getLine
if input == secret
then putStrLn $ "Got it in " ++ show try ++ " tries!"
else guess (try + 1) secret $ delete input range
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment