Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
16
Task/Guess-the-number/Haskell/guess-the-number-1.hs
Normal file
16
Task/Guess-the-number/Haskell/guess-the-number-1.hs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import Control.Monad
|
||||
import System.Random
|
||||
|
||||
-- Repeat the action until the predicate is true.
|
||||
until_ act pred = act >>= pred >>= flip unless (until_ act pred)
|
||||
|
||||
answerIs ans guess
|
||||
| ans == guess = putStrLn "You got it!" >> return True
|
||||
| otherwise = putStrLn "Nope. Guess again." >> return False
|
||||
|
||||
ask = liftM read getLine
|
||||
|
||||
main = do
|
||||
ans <- randomRIO (1,10) :: IO Int
|
||||
putStrLn "Try to guess my secret number between 1 and 10."
|
||||
ask `until_` answerIs ans
|
||||
10
Task/Guess-the-number/Haskell/guess-the-number-2.hs
Normal file
10
Task/Guess-the-number/Haskell/guess-the-number-2.hs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import System.Random
|
||||
|
||||
main = randomRIO (1,10) >>= gameloop
|
||||
|
||||
gameloop :: Int -> IO ()
|
||||
gameloop r = do
|
||||
i <- fmap read getLine
|
||||
if i == r
|
||||
then putStrLn "You got it!"
|
||||
else putStrLn "Nope. Guess again." >> gameloop r
|
||||
Loading…
Add table
Add a link
Reference in a new issue