public
Last active

Number guessing game

  • Download Gist
GuessGame-abstracted.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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)
GuessGame-limited.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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
GuessGuess-simple.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.