diff options
| author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2021-06-26 14:37:46 +0100 | 
|---|---|---|
| committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2021-06-26 14:37:46 +0100 | 
| commit | 445915a8c72735e3f25cb697ce63c49890ca70f6 (patch) | |
| tree | d646f6f16b973787deeb6ca3c006dc184de5077f /Main.hs | |
| parent | 4a781087c9520ecb700f0f557960e0e0de461a4d (diff) | |
| download | numberdisplay-445915a8c72735e3f25cb697ce63c49890ca70f6.tar.gz numberdisplay-445915a8c72735e3f25cb697ce63c49890ca70f6.tar.bz2 numberdisplay-445915a8c72735e3f25cb697ce63c49890ca70f6.zip  | |
(Code)+main.hs for entrypoint of program
Diffstat (limited to 'Main.hs')
| -rw-r--r-- | Main.hs | 66 | 
1 files changed, 66 insertions, 0 deletions
@@ -0,0 +1,66 @@ +import Math +import System.Environment +import Data.List +import Text.Printf + +errorMessage :: String -> String + +errorMessage "" = "nd v1\nUsage: nd [-int, -prime, -factor, -hcf]" +errorMessage reason = "nd v1\nFAILURE: " ++ reason ++ "\nUsage: nd [-int, -prime, -factor, -hcf]" + +flagHandlerF :: [String] -> Int -> String -> ([String] -> String) -> String + +flagHandlerF args (-1) error fmessage = +  if (length args) == 0 +  then error +  else +    (fmessage args) + +flagHandlerF args nargs error fmessage = +  if (length args) /= nargs +  then error +  else +    (fmessage args) + +flagHandler :: String -> [String] -> String + +flagHandler "-prime" args = +  flagHandlerF args (-1) +    (errorMessage "-prime requires at least one integer argument") +    (\args -> +        foldl (++) "" [show y ++ ": isPrime=" ++ (show $ isPrime y) +                    ++ "\n" | x <- args, let y = (read x) :: Int]) + +flagHandler "-factor" args = +  flagHandlerF args (-1) +    (errorMessage "-factor requires at least one integer argument") +    (\args -> +        foldl (++) "" [show y ++ ": factors=" ++ show (factors $ y) +                        | x <- args, let y = (read x) :: Int]) + +flagHandler "-int" args = +  flagHandlerF args (-1) +    (errorMessage "-int requires at least one integer argument") +    (\args -> +        foldl (++) "" [show y ++ ": (isPrime=" ++ (show $ isPrime y) +                        ++ ", factors=" ++ (show $ factors y) +                        ++ ")\n" | x <- args, let y = (read x) :: Int]) + +flagHandler "-hcf" args = +  flagHandlerF args (2) +    (errorMessage "-hcf requires two integer arguments") +    (\args -> +        let n       = (read $ head args) :: Int +            m       = (read $ head $ tail args) :: Int +            hcf     = head $ reverse $ intersect (factors n) (factors m) in +        "hcf(" ++ show n ++ ", " ++ show m ++ "): hcf=" ++ show hcf) + + +flagHandler _ _ = errorMessage "Didn't recognise flag" + +main = do +  args <- getArgs +  if (length args) == 0 +    then putStrLn $ errorMessage "" +    else +    putStrLn $ flagHandler (head args) (tail args)  | 
