-- Number guessing game: guess a number between 1 and 100 with higher/lower -- feedback, then play again. The game asks the Random effect for each -- round's secret without knowing where randomness comes from; main picks -- the interpretation by installing a handler. import IO import Maybe exposing (Maybe(..)) import Random import String guessLoop secret = Console.write "Your guess: " case readLine() of Nothing -> print "Goodbye!" Just line -> case String.toInt line.text of Nothing -> print "Please enter a number." guessLoop secret Just guess -> case guess == secret of True -> print "Correct!" False -> print (if guess < secret then "Too low." else "Too high.") guessLoop secret playRound() = secret = Random.int 1 100 print "I picked a number between 1 and 100." guessLoop secret Console.write "Play again? (y/n): " case readLine() of Nothing -> print "Goodbye!" Just answer -> case answer.text == "y" of True -> playRound() False -> print "Goodbye!" -- Swap runSeeded for Random.runSystem to play against real randomness. main : () ->{Console} () main() = Random.runSeeded 42 playRound
Guess a number from 1 to 100. Randomness is an effect, so main chooses a seeded handler.