Created
January 15, 2012 00:41
-
-
Save ymirpl/1613621 to your computer and use it in GitHub Desktop.
Parser maybe
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
module Parser where | |
import Data.Time | |
import Prelude hiding (catch) | |
import qualified Control.OldException as C | |
-- funkcja wczytujaca date z konsoli (waliduje jej poprawnosc) | |
-- return: IO Day - data podana z konsoli | |
getDay :: IO Day | |
getDay = do | |
y <- maybeRead | |
case y of | |
Nothing -> do | |
putStrLn "Blad!!! Zly format daty. Podaj date jeszcze raz" | |
getDay | |
Just n -> return n | |
-- funkcja wczytujaca liczbe z konsoli (waliduje jej poprawnosc) | |
-- return: IO Int - liczba podana z konsoli | |
getInt :: IO Int | |
getInt = do | |
y <- maybeRead | |
case y of | |
Nothing -> do | |
putStrLn "Blad!!! Wpisany znak nie jest liczba. Podaj go jeszcze raz" | |
getInt | |
Just n -> return n | |
-- funkcja wczytujaca liste liczb z konsoli (waliduje jej poprawnosc) | |
-- return: IO [Int] - list liczb podana z konsoli | |
getIntList :: IO [Int] | |
getIntList = do | |
y <- maybeRead | |
case y of | |
Nothing -> do | |
putStrLn "Blad!!! Bledny format" | |
getIntList | |
Just n -> return n | |
-- funkcja wczytujaca liczb z konsoli (waliduje jej poprawnosc) | |
-- return: IO Integer - list liczb podana z konsoli | |
getInteger :: IO Integer | |
getInteger = do | |
y <- maybeRead | |
case y of | |
Nothing -> do | |
putStrLn "Blad!!! Bledny format" | |
getInteger | |
Just n -> return n | |
-- funkcja sprawdza poprawnosc danych | |
-- return (Maybe a) - gdy poprawne dane zwraca (Just n) w innym wypadku | |
-- Nothing | |
maybeRead :: Read a => IO (Maybe a) | |
maybeRead = C.catch | |
(do x <- getLine | |
let n = read x | |
n `seq` return (Just n)) | |
(const (return Nothing)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment