import Text.Read (readMaybe) import System.Environment -- Úloha 2.1 -- Upravte program hello.hs tak, aby sa po spustení opýtal na meno používateľa a na mesto, kde žije. Následne vypíše pozdrav v formáte: „Hello, from !“. -- Poznámka -- Použite funkcie getLine a putStrLn. Použite let na skonštruovanie reťazca s pozdravom pred jeho vypísaním. -- >>= operator greetingV1 :: IO () greetingV1 = putStrLn "Hello, what is your name?" >> getLine >>= \name -> putStrLn "Where do you live?" >> getLine >>= \city -> let greeting = "Hello, " ++ name ++ " from " ++ city ++ "!" in putStrLn greeting greetingV2 :: IO () greetingV2 = do putStrLn "Hello, what is your name?" name <- getLine putStrLn "Where do you live?" city <- getLine let greeting = "Hello, " ++ name ++ " from " ++ city ++ "!" putStrLn greeting getInt :: IO Int getInt = do putStrLn "Hello, enter an Integer value:" line <- getLine case readMaybe line of Just number -> do let entered =" You entered Integer with value of" ++ show number putStrLn entered pure(number) Nothing -> do let msg = "Invalid input! Please enter a valid number." putStrLn msg getInt getIntV2 :: IO Int getIntV2 = putStrLn "Enter an int:" >> getLine >>= maybe getIntV2 pure . readMaybe -- Úloha 4.1 -- Nech máme definovaný zoznam otázok: -- Definujte funkcie ask a askAll. Prvá sa bude pýtať zadanú otázku a vráti odpoveď. Druhá sa postupne opýta všetky otázky zo zoznamu a vráti zoznam odpovedí. ask :: String -> IO String ask q = do putStrLn q a <- getLine pure a questions :: [String] questions = [ "What is your name?", "What is your favourite language?", "What would you like to build?" ] askAll :: [String] -> IO [String] askAll = mapM ask askAllV2 :: [String] -> IO [String] askAllV2 [] = pure [] askAllV2 (q:qs) = do ans <- ask q rest <- askAllV2 qs pure (ans : rest) -- Poznámka -- Skúste implementovať funkciu askAll pomocou rekurzie aj pomocou funkcie mapM. -- Práca so súbormi cieľ 2 -- Úloha 5.1 -- Upravte program na zobrazovanie histogramu tak, aby čítal zoznam čísel zo súboru, názov ktorého bude zadaný ako argument príkazového riadku. -- Poznámka values :: [Int] values = [6,2,12,10,7,3,0,12,4,8,7,8,10,13,10,8,2,11,17,9,16,12,14,9,9,2,7,3,7,10,1,11,11,13,10,12,15,9,15,13,9,8,16,1,18,9,10,13,15,5,13,15,11,10,8,1,9,13,10,9,7,14,6,6,9,14,12,17,7,7,4,12,11,4,7,8,10,6,14,6,13,14,4,1,10,1,8,11] counts :: [Int] -> [Int] counts nums = [length (filter (== d) nums) | d <- [0 .. (maximum nums - 1)]] countsExample :: [Int] countsExample = [1,5,3,2,4,1,5,8,7,9,10,6,6,7,5,4,2,2,1] histogram :: [Int] -> String histogram values = unlines (legendC `beside` (body `above` legendL)) where histCounts = counts values body = histogramBody histCounts legendL = [legendLine width] legendC = legendColumn height width = length histCounts height = maximum histCounts histogramBody :: [Int] -> [String] histogramBody values = map (histogramLine values) levels where levels = reverse [1..maximum values] histogramLine :: [Int] -> Int -> String histogramLine values level = concatMap cell values where cell x | x >= level = " |" | otherwise = " " legendLine :: Int -> String legendLine width = concatMap filledLabel [0..width-1] legendColumn :: Int -> [String] legendColumn height = map filledLabel (reverse [1..height]) ++ [" "] filledLabel :: Int -> String filledLabel x = rfill 2 (legendLabel x) legendLabel :: Int -> String legendLabel x | x `mod` 5 == 0 = show x | otherwise = "" rfill :: Int -> String -> String rfill width str = replicate (width - length str) ' ' ++ str above a b = a ++ b beside a b = zipWith (++) a b loadValuesFromFile :: IO [Int] loadValuesFromFile = do args <- getArgs case args of [fileName] -> do content <- readFile fileName let values = map read (words [if c == ',' then ' ' else c | c <- content]) :: [Int] putStrLn "Loaded file values:" print values pure values _ -> do putStrLn "Usage: ./start " pure [] -- Úloha 6.1 -- Vytvorte aplikáciu pre hádanie mysleného čísla: Nech počítač si myslí číslo a používateľ háda myslené číslo. Počítač nech po nesprávnom odhade povie, či je myslené číslo väčšie alebo menšie ako hádané. -- Aby sme mohli používať doplňujúcu knižnicu random, vytvoríme nový projekt. V novom adresári guess spustíte príkaz pre vytvorenie projektu: -- cabal init -- Na všetky otázky stačí dať predvolenú odpoveď. -- V súbore guess.cabal pridajte závislosť na balík random: -- Other library packages from which modules are imported. -- build-depends: base ^>=4.20.2.0, -- random -- Môžete použiť pomocnú funkciu randNumber na generovanie náhodného čísla na základe špecifikovaného intervalu a odovzdaného generátora: -- module Main where -- import System.Random -- import Text.Read (readMaybe) -- guess :: IO Int -- guess = putStrLn "Enter an guessed number:" >> getLine >>= maybe guess pure . readMaybe -- loop :: Int -> IO () -- loop sec = do -- ans <- guess -- case compare ans sec of -- LT -> do -- putStrLn "The number I'm thinking of is LARGER!" -- loop sec -- GT -> do -- putStrLn "The number I'm thinking of is SMALLER!" -- loop sec -- EQ -> putStrLn "YOU DID IT!" -- main :: IO () -- main = do -- number <- randNumber 1 10 -- loop number -- randNumber :: Int -> Int -> IO Int -- randNumber a b = getStdGen >>= \gen -> return (fst (randomR (a, b) gen)) -- Teraz na spúšťanie programu vám stačí použiť príkaz: -- cabal update -- cabal run main :: IO () main = do -- greetingV1 -- greetingV2 -- _ <- getInt -- pure () -- _ <- getIntV2 -- pure () -- _ <- ask (questions !! 1) -- pure () -- _ <- askAll questions -- pure () -- answers <- askAllV2 questions -- putStrLn "Summary:" -- print answers values <- loadValuesFromFile putStr (histogram values)