
Answering my own plea for help, I now have the following, which seems
neater to me.
company_to_companyfile = [("ibm","data/ibm.dat"),("cisco","data/cisco.dat")]
displaymode_to_modestring = [("points", "using 1:2 with linespoints"),
("candles","using 1:($2+$3+$4+$5)/4:4:3 with yerrorbars")]
displaymode_to_titleend = [("points","daily
prices"),("candles","opening prices")]
financial_output_wrapper :: String -> String -> String -> String -> IO ()
financial_output_wrapper company displaymode startDate endDate =
do
let maybeCompanyFile = lookup company company_to_companyfile
case maybeCompanyFile of Nothing -> error $ "no company file for "
++ company
_ -> return ()
let maybeModeString = lookup displaymode displaymode_to_modestring
case maybeModeString of Nothing -> error $ "no mode string for "
++ displaymode
_ -> return ()
let maybeTitleEnd = lookup displaymode displaymode_to_titleend
case maybeTitleEnd of Nothing -> error $ "no title end for " ++ displaymode
_ -> return ()
let maybeScript = gen_gnuplot_financial_script company
( maybeCompanyFile )
( maybeModeString )
( maybeTitleEnd )
startDate endDate
case maybeScript of
Just script -> putStrLn script
_ -> error $ "bad script"
gen_gnuplot_financial_script :: String -> Maybe String -> Maybe String
-> Maybe String -> String -> String -> Maybe String
gen_gnuplot_financial_script company (Just companyfile ) ( Just
modestring) ( Just titleEnd ) startDate endDate
= Just $ gnuplot_timeseries_settings ++ "\n" ++
"plot [\"" ++ startDate ++ "\":\"" ++
endDate ++ "\"]"
++ " '" ++ companyfile ++ "'"
++ modestring
++ " title \"" ++ company ++ " " ++
titleEnd ++ "\""
2007/4/12, Thomas Hartman
I was translating some perl code to haskell as a learning exercise and wound up with the following. (below) Simple code that accepts some string arguments, and prints a string -- so, of type String -> String -> String -> String -> IO ().
I like to be concise, but I get the feeling something went awry. What seems to be costing me the most is checking whether the various arguments are legitimate, and printing a helpful error message if not.
I was able to achieve this by using Maybe and error on failed pattern match, but as said, seems kind of overly complicated.
Is there a simpler way to do the following, eg for function
gen_gnuplot_financial_script :: String -> String -> String -> String -> IO ()
?
By the way this is being used in
http://code.google.com/p/gnuplotwebinterface/
**************
module Common where
gnuplot_png_settings = "set terminal png transparent nocrop enhanced size 600,400\n" ++ "set pm3d implicit at s"
gnuplot_math_settings = gnuplot_png_settings ++ "\n" ++ "set border 4095 \n\ \ set xlabel \"x\" \n\ \ set ylabel \"y\""
gnuplot_timeseries_settings = gnuplot_png_settings ++ "\n" ++ "set xdata time # The x axis data is time \n" ++ "set timefmt \"%d-%b-%y\" # The dates in the file look like 10-Jun-04 \n" ++ "set format x \"%b %d\" #On the x-axis, we want tics like Jun 10"
gen_gnuplot_math_script :: String -> String -> IO () gen_gnuplot_math_script style function = let maybePlotCmd = lookup style style_to_plotcmd style_to_plotcmd = [("math-2d","plot"),("math-3d","splot")] in case maybePlotCmd of Just plotcmd -> putStrLn $ gnuplot_math_settings ++ "\n" ++ plotcmd ++ " " ++ function _ -> error $ "bad style: " ++ style
gen_gnuplot_financial_script :: String -> String -> String -> String -> IO () gen_gnuplot_financial_script company displaymode startDate endDate = let maybeCompanyFile = lookup company company_to_companyfile maybeModeString = lookup displaymode displaymode_to_modestring maybeTitleEnd = lookup displaymode displaymode_to_titleend company_to_companyfile = [("ibm","data/ibm.dat"),("cisco","data/cisco.dat")] displaymode_to_modestring = [("points", "using 1:2 with linespoints"), ("candles","using 1:($2+$3+$4+$5)/4:4:3 with yerrorbars")] displaymode_to_titleend = [("points","daily prices"),("candles","opening prices")] in case ( maybeCompanyFile, maybeModeString, maybeTitleEnd ) of ( Just companyfile, Just modestring, Just titleEnd) -> putStrLn $ gnuplot_timeseries_settings ++ "\n" ++ "plot [\"" ++ startDate ++ "\":\"" ++ endDate ++ "\"]" ++ " '" ++ companyfile ++ "'" ++ modestring ++ " title \"" ++ company ++ " " ++ titleEnd ++ "\"" _ -> error $ "bad lookup. " ++ company ++ " -> company file: " ++ ( show maybeCompanyFile ) ++ "\n" ++ " " ++ displaymode ++ " -> displaymode: " ++ ( show maybeModeString ) ++ "\n" ++ " " ++ displaymode ++ " -> titleEnd: " ++ ( show maybeTitleEnd)