From 318edd7c95b841024c2f7cfc7478aa9c601e12e5 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 23 Jul 2021 22:11:17 +0100 Subject: (General)~moved cli files into seperate folder to make space for website --- cli/Main.hs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cli/Makefile | 11 ++++++++++ 2 files changed, 78 insertions(+) create mode 100644 cli/Main.hs create mode 100644 cli/Makefile (limited to 'cli') diff --git a/cli/Main.hs b/cli/Main.hs new file mode 100644 index 0000000..2c7aa7d --- /dev/null +++ b/cli/Main.hs @@ -0,0 +1,67 @@ +import Math +import System.Environment +import Data.List +import Text.Printf + +errorMessage :: String -> String + +errorMessage "" = "nd v1\nUsage: nd [-help, -int, -prime, -factor, -hcf]\n" +errorMessage reason = "nd v1\nFAILURE: " + ++ reason + ++ "\nUsage: nd [-int, -prime, -factor, -hcf]\n" + +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 "Flag not recognised" + + +main = do + args <- getArgs + putStr $ + case args of + [] -> errorMessage "" + (x: xs) -> if x == "-help" then errorMessage "" else flagHandler x xs diff --git a/cli/Makefile b/cli/Makefile new file mode 100644 index 0000000..827617d --- /dev/null +++ b/cli/Makefile @@ -0,0 +1,11 @@ +CC=ghc +FILES=Main.hs ../Math.hs +OUT=nd + +$(OUT): $(FILES) + $(CC) $^ -o $@ + +.PHONY: run +run: + $(MAKE) + ./$(OUT) -int 200 300 400 10009 -- cgit v1.2.3-13-gbd6f