-- | This is a collection of really useful functions. They are mostly aimed towards writing console applications. -- -- For example: -- -- > import AlexPrelude -- > -- > main :: IO () -- > main = do -- > clear_screen -- > print "Hello my name is Alex" -- > name <- prompt "What is your name? " -- > print ("Hello, " ++ name) -- > print "Let me ponder on what I think about your name..." -- > sleep 10 -- > print "It is ridiculous! Why would your parents do that to you!" -- > print_banner -- -- As you can see, these functions try to fill in the missing gaps for when you want to write a real application in haskell. module AlexPrelude ( prompt , sleep , clear_screen , print_banner ) where import System.IO import Data.Time.Clock.POSIX -- | Asks a user for some input prompt :: String -> IO String prompt question = do -- This is INSANELY tricky. Sometimes it does not print the question! Fortunately I found the answer on stackoverflow: you need to use `hFlush stdout`. putStr question putStr " " hFlush stdout answer <- getLine hFlush stdout -- To make sure? I don't really see how `getLine` works return answer -- | Wait for a given number of seconds sleep :: Int -> IO () sleep secs = do now <- get_time_as_int let goal = now + secs sleep_helper goal sleep_helper :: Int -> IO () sleep_helper goal = do now <- get_time_as_int if now >= goal then return () else sleep_helper goal get_time_as_int :: IO Int get_time_as_int = -- What does fmap do?? I found it here: https://stackoverflow.com/questions/17909770/get-time-as-int round `fmap` getPOSIXTime -- | This clears the screen. Unfortunately, this only works if the terminal has less than 80 rows. If the terminal has more rows, you can just call this a couple of times until the entire screen is cleared. clear_screen :: IO () clear_screen = do print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" print "\n" -- | This prints a banner to show that your application was created using AlexPrelude. You are not obliged to use this, but I would be thankful. print_banner :: IO () print_banner = do print " ----- 8 ------------------- MADE POSSIBLE BY ------------------- " print " 8 " print ".oPYo. 8 .oPYo. `o o' .oPYo. .oPYo. o o .oPYo. oPYo. .oPYo. " print ".oooo8 8 8oooo8 `bd' Yb.. .oooo8 8 8 8oooo8 8 `' Yb.. " print "8 8 8 8. d'`b 'Yb. 8 8 8 8 8. 8 'Yb. " print "`YooP8 8 `Yooo' o' `o `YooP' `YooP8 `YooP8 `Yooo' 8 `YooP' " print " 8 " print " --------- SMOKE WEED EVERYDAY --------- ooP' ------------------- "