[project @ 1997-09-03 15:33:15 by simonm]
[ghc-hetmet.git] / ghc / tests / programs / barton-mangler-bug / Plot.lhs
1 The functions in this file (well, the single function) will allow the
2 user to plot different functions using the Gnuplot program.  In fact,
3 all it really does is output a number of points on the list and allow
4 the user to activate Gnuplot and use the plotting program to get the
5 appropriate output.
6
7 The first line just gives the module name.  For the moment, I don't
8 anticipate using any modules (although this may change).
9
10 > module Plot where
11 > import IO
12
13 Now we give the type of the function.  This consists of a file name, a
14 list of values, and a function that goes from the appropriate types.
15
16 > plot2d:: (Show a, Show b) => String -> [a] -> (a -> b) -> IO()
17 > plot2d fl inp f = openFile fl WriteMode      >>= \flh ->
18 >                   plot2d' flh inp f         >>
19 >                   hClose flh
20
21 > plot2d':: (Show a, Show b) => Handle -> [a] -> (a -> b) -> IO()
22 > plot2d' fl [] f     = return ()
23 > plot2d' fl (x:xs) f = hPutStr fl  (show x)        >>
24 >                       hPutStr fl  "  "            >>
25 >                       hPutStr fl  (show (f x))    >>
26 >                       hPutStr fl  "\n"            >>
27 >                       plot2d' fl xs f
28
29 > plot3d:: (Show a, Show b, Show c) => String -> [a] -> [b] -> 
30 >                                      (a -> b -> c) -> IO()
31 > plot3d fl inp1 inp2 f = openFile fl WriteMode      >>= \flh ->
32 >                         plot3d' flh inp1 inp2 f    >>
33 >                         hClose flh
34
35 > plot3d':: (Show a, Show b, Show c) => Handle -> [a] -> [b] -> 
36 >                                       (a -> b -> c) -> IO()
37 > plot3d' fl []     inp f = return ()
38 > plot3d' fl (x:xs) inp f = plot3d'' fl x inp f           >>
39 >                           hPutStr fl "\n"               >>
40 >                           plot3d' fl xs inp f
41
42 > plot3d'':: (Show a, Show b, Show c) => Handle -> a -> [b] -> 
43 >                                        (a -> b -> c) -> IO()
44 > plot3d'' fl inp [] f        = return ()
45 > plot3d'' fl x (y:ys) f = hPutStr fl  (show x)        >>
46 >                          hPutStr fl  "  "            >>
47 >                          hPutStr fl  (show y)        >>
48 >                          hPutStr fl  "  "            >>
49 >                          hPutStr fl  (show (f x y))  >>
50 >                          hPutStr fl  "\n"            >>
51 >                          plot3d'' fl x ys f
52
53
54 And now, let's create a function to make a range out of a triple of a
55 start point, an end point, and an increment.
56
57 > createRange:: (Num a, Ord a) => a -> a -> a -> [a]
58 > createRange s e i = if s > e then []
59 >                     else s : createRange (s+i) e i
60
61 We now settle down to a couple of more specific functions that do
62 things that are more unique to gnuplot.  First, we have something that
63 creates the appropriate gnuplot command file.
64
65 > createGnuPlot:: Show a => String -> a -> a -> IO()
66 > createGnuPlot fl s e = openFile (fl ++ ".gnp") WriteMode   >>= \flh ->
67 >                        hPutStr flh "set terminal latex\n"  >>
68 >                        hPutStr flh "set output \""         >>
69 >                        hPutStr flh (fl ++ ".tex\"\n")      >>
70 >                        hPutStr flh "set nokey\n"           >>
71 >                        hPutStr flh "plot ["                >>
72 >                        hPutStr flh (show s)                >>
73 >                        hPutStr flh ":"                     >>
74 >                        hPutStr flh (show e)                >>
75 >                        hPutStr flh "] \""                  >>
76 >                        hPutStr flh (fl ++ ".plt\"")        >>
77 >                        hPutStr flh " with lines\n"         >>
78 >                        hClose  flh
79
80 And now we create a fairly specific plotExam function that takes a
81 string, a function, and two floats and produces the correct files
82
83 > plotExam:: String -> Float -> Float -> (Float -> Float) -> IO()
84 > plotExam fl s e f = plot2d (fl++".plt") r f                >>
85 >                     createGnuPlot fl s e
86 >               where r = createRange s e ((e - s) / 2500)