Created
August 22, 2010 22:21
-
-
Save petermarks/544353 to your computer and use it in GitHub Desktop.
Number guessing game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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