Mercurial > 12ws.info2
view exercises/src/Exercise_13.hs @ 34:f62e3ed2c90d
week 13 tutorial
author | Markus Kaiser <markus.kaiser@in.tum.de> |
---|---|
date | Wed, 30 Jan 2013 22:07:58 +0100 |
parents | 71b56f8b3069 |
children |
line wrap: on
line source
module Exercise_13 where import Data.Char (isDigit, digitToInt) import Data.List (find, isInfixOf, isSuffixOf, nub, sort, intersect) import System.Random (randomRIO) import Test.QuickCheck helloWorld :: IO () helloWorld = putStrLn "Hello, World!" greeter :: IO () greeter = do putStr "Tell me your name: " name <- getLine if name == "bye" then putStrLn "Goodbye!" else do putStrLn $ "Ah, hi there, " ++ name ++ "!" greeter randomisator :: IO Int randomisator = do let bs = (1, 10) a <- randomRIO bs b <- randomRIO bs c <- randomRIO bs return $ a + b + c {---------------------------------------------------------------------} {- Aufgabe G13.1 -} nRandomR :: (Int, Int) -> Int -> IO [Int] nRandomR lowhigh n = randHelp [] n where randHelp :: [Int] -> Int -> IO [Int] randHelp xs 0 = return xs randHelp xs n = do x <- randomRIO lowhigh if x `elem` xs then randHelp xs n else randHelp (x : xs) (n-1) nRandomR' :: (Int, Int) -> Int -> IO [Int] nRandomR' _ 0 = return [] nRandomR' lowhigh n = do i <- randomRIO lowhigh is <- nRandomR' lowhigh (n-1) if i `elem` is then nRandomR' lowhigh n else return (i:is) {---------------------------------------------------------------------} {- Aufgabe G13.2 -} getLineInt :: IO Int getLineInt = do putStr "Enter a Number: " line <- getLine if (not . null) line && all isDigit line then return $ read line else do putStrLn "Not a nonnegative number!" getLineInt guessNum :: IO Int guessNum = do rnd <- randomRIO (0, 100) putStrLn "Guess my number!" doGuessNum rnd 1 where doGuessNum :: Int -> Int -> IO Int doGuessNum rnd n = do x <- getLineInt if x < rnd then do putStrLn "Blubberlutsch." doGuessNum rnd (n+1) else if x > rnd then do putStrLn "Brawarkl" doGuessNum rnd (n+1) else do putStrLn "(...)" return n {---------------------------------------------------------------------} {- Aufgabe H13.1 -} getSorted :: IO [String] getSorted = undefined {---------------------------------------------------------------------} {- Aufgabe H13.2 -} bullsAndBears :: IO () bullsAndBears = undefined