Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import System.Random
- import Control.Monad
- -- A type for the possible results (C programmers would call this an enum)
- data Result = Lower | Match | Higher
- -- The simple game logic
- game :: Integer -> Integer -> Result
- game guess secret
- | guess == secret = Match
- | guess < secret = Lower
- | guess > secret = Higher
- -- Map the result to a message for the user
- mapResult :: Result -> String
- mapResult Lower = "Number too low"
- mapResult Higher = "Number too high"
- mapResult Match = "You've got it!"
- -- trying to read a Int from stdin
- readAInt :: IO Integer
- readAInt = readLn
- computeSecret :: IO Int
- computeSecret = getStdRandom (randomR (1,100))
- mainloop = do
- secret <- computeSecret
- guess <- readAInt
- putStrLn ("input: " ++ (show guess))
- putStrLn ("secret: " ++ (show secret))
- --readAInt $ computeSecret $ game
- --unless (guess == 0) $ do
- result <- game guess secret
- output <- mapResult result
- putStrLn output
- mainloop
- main = do
- putStrLn "Guess a number between 1 and 100"
- mainloop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement