Created
December 13, 2014 11:28
-
-
Save fujimura/39c0bfadfa4cfd9929b1 to your computer and use it in GitHub Desktop.
Compare "Guessing Game" in The Rust Guide http://doc.rust-lang.org/guide.html#guessing-game
This file contains hidden or 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
import Control.Applicative | |
import Control.Exception | |
import System.Random | |
import Text.Read | |
main :: IO () | |
main = go | |
go :: IO () | |
go = do | |
putStrLn "Guess the number!" | |
secretNumber <- getRandomNumber | |
putStrLn $ "The secret number is: " ++ show secretNumber | |
putStrLn "Please input your guess." | |
line <- strip <$> getLine `catch` | |
\e -> error $ "Failed to read line" ++ show (e :: SomeException) | |
case (readMaybe line :: Maybe Int) of | |
Just n -> case n `compare` secretNumber of | |
LT -> putStrLn "Too small!" >> go | |
GT -> putStrLn "Too big!" >> go | |
EQ -> putStrLn "You win!" | |
Nothing -> putStrLn "Please input number" >> go | |
where | |
getRandomNumber :: IO Int | |
getRandomNumber = (+ 1) . (`mod` 100) <$> randomIO | |
strip :: String -> String | |
strip = reverse . dropWhile (== '\n') . reverse |
This file contains hidden or 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
loop do | |
puts "Guess the number!" | |
secret_number = rand 100 | |
puts "The secret number is: #{secret_number}" | |
puts "Please input your guess." | |
line = gets.strip rescue puts("Failed to read line") | |
puts "you guessed: #{line}" | |
n = begin Integer(line) | |
rescue | |
puts("Please input number") | |
next | |
end | |
if n < secret_number | |
puts "too Small!" | |
elsif n == secret_number | |
puts "You win!" | |
exit | |
elsif n > secret_number | |
puts "Too big!" | |
end | |
end |
This file contains hidden or 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
use std::io; | |
use std::rand; | |
fn main() { | |
loop { | |
println!("Guess the number!"); | |
let secret_number = (rand::random::<uint>() % 100) + 1; | |
println!("The secret number is: {}", secret_number); | |
println!("Please input your guess."); | |
let input = io::stdin() | |
.read_line() | |
.ok() | |
.expect("failed to read line"); | |
let input_num: Option<uint> = from_str(input.as_slice().trim()); | |
let n = match input_num { | |
Some(num) => num, | |
None => { | |
println!("Please input number"); | |
return | |
} | |
}; | |
println!("You guessed: {}", input); | |
match cmp(n, secret_number) { | |
Less => println!("Too small!"), | |
Greater => println!("Too big!"), | |
Equal => { | |
println!("You win!"); | |
return; | |
} | |
} | |
} | |
} | |
fn cmp(a: uint, b: uint) -> Ordering { | |
if a < b { Less } | |
else if a > b { Greater } | |
else { Equal } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment