Skip to content

Instantly share code, notes, and snippets.

@petermarks
Created August 22, 2010 22:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petermarks/544353 to your computer and use it in GitHub Desktop.
Save petermarks/544353 to your computer and use it in GitHub Desktop.
Number guessing game
module Main where
import System.Random
import Data.Function
processGuess :: Int -> IO () -> IO ()
processGuess answer cont = do
n <- getLine
case compare (read n) answer of
EQ -> putStrLn "Awesome!!!!"
LT -> putStrLn "guess is too low" >> cont
GT -> putStrLn "guess is too high" >> cont
limiter :: Int -> a -> (a -> a) -> a
limiter 0 terminal _ = terminal
limiter limit terminal f = f (limiter (limit - 1) terminal f)
limited = do
answer <- randomRIO (1, 100)
putStrLn "Can you guess the number between 1 and 100"
limiter 5 (putStrLn "Loooooser") (processGuess answer)
unlimited = do
answer <- randomRIO (1, 100)
putStrLn "Can you guess the number between 1 and 100"
fix (processGuess answer)
module Main where
import System.Random
processGuess :: Int -> Int -> IO ()
processGuess 0 answer = putStrLn "Too Late!"
processGuess limit answer = do
n <- getLine
case compare (read n) answer of
EQ -> putStrLn "Awesome!!!!"
LT -> putStrLn "guess is too low" >> processGuess (limit - 1) answer
GT -> putStrLn "guess is too high" >> processGuess (limit - 1) answer
main = do
answer <- randomRIO (1, 100)
putStrLn "Can you guess the number between 1 and 100"
processGuess 5 answer
module Main where
import System.Random
processGuess :: Int -> IO ()
processGuess answer = do
n <- getLine
case compare (read n) answer of
EQ -> putStrLn "Awesome!!!!"
LT -> putStrLn "guess is too low" >> processGuess answer
GT -> putStrLn "guess is too high" >> processGuess answer
main = do
answer <- randomRIO (1, 100)
putStrLn "Can you guess the number between 1 and 100"
processGuess answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment