The following code parses a 2D Cartesian coordinate passed as Cart2Pol <x> <y>
and prints the coordinate in rho, theta
(polar) form. The operation itself is trivial, however I am interested in critique on the "boiler plate" code to take the args, parse them, compute and print the result.
The Haskell source is as follows:
module Main where
import System.Environment (getArgs)
import Text.Read (readMaybe)
cart2pol :: Float -> Float -> (Float, Float)
cart2pol x y = (rho, theta)
where
rho = sqrt $ x**2 + y**2
theta = atan2 y x
readFloat :: String -> Maybe Float
readFloat = readMaybe
convert :: Maybe Float -> Maybe Float -> (Float, Float)
convert (Just x) (Just y) = cart2pol x y
convert _ _ = error "x and y must be Float"
main :: IO ()
main = do
args <- getArgs
case args of
[x, y] -> do
let rt = convert (readFloat x) (readFloat y)
putStrLn $ "rho: " ++ show (fst rt) ++ " theta: " ++ show (snd rt)
_ -> putStrLn "Usage: CartToPol x y"
What are some ways I could make this code more succinct?