Revisão | 60c48182945a24d2f95e9bdff42cdabd01ad6010 (tree) |
---|---|
Hora | 2010-10-13 07:23:50 |
Autor | lorenzo |
Commiter | lorenzo |
Apart from some minor modifications, I am now properly iterating a function
on a list of file following the suggestions by Daniel. Now I avoid generating
(though in a lazy way) a list with the contents of each file since that means
opening all those files and I can run short of handles.
@@ -17,37 +17,35 @@ | ||
17 | 17 | |
18 | 18 | -- Uncomment the bit above if you want to read the list of files from a file |
19 | 19 | |
20 | - let nums=[1..260] | |
20 | + let nums=[1..2] | |
21 | 21 | |
22 | 22 | |
23 | - fl <- getAllLengths nums | |
24 | - | |
25 | - -- putStrLn "fl is, " | |
26 | - -- print fl | |
27 | - | |
28 | - cl <- file_conv_all nums | |
29 | - | |
30 | - -- putStrLn "cl is, " | |
31 | - -- print cl | |
32 | 23 | |
33 | - -- putStrLn "len cl is, " | |
34 | - -- print (length cl) | |
35 | 24 | |
36 | - -- putStrLn "len cl!!1 is, " | |
37 | - -- print (length $ cl!!1) | |
25 | + -- cl <- file_conv_all nums | |
26 | + | |
38 | 27 | |
39 | - let entr_all= map entropy_list cl | |
28 | + -- let entr_all= map entropy_list cl | |
40 | 29 | |
41 | - -- putStrLn "entr_all is, " | |
42 | - -- print (entr_all) | |
30 | + -- save_vector "entropy.dat" entr_all | |
31 | + | |
32 | +-- NB: the 3 lines above (which are now comments) calculate correctly the entropy | |
33 | +-- but they have the problem if using too many file handles. The procedure I | |
34 | +-- follow below is really the correct one. | |
35 | + | |
36 | + | |
37 | + my_entropy <- get_all_entropies nums | |
43 | 38 | |
44 | - save_vector "entropy.dat" entr_all | |
45 | - | |
39 | + save_vector "entropy.dat" my_entropy | |
40 | + | |
41 | + | |
46 | 42 | putStrLn "So far so good " |
47 | 43 | |
48 | 44 | |
45 | + -- ######################################################### | |
46 | + | |
49 | 47 | filename :: Int -> FilePath |
50 | -filename i = "time_series_" ++ show i ++ "_.dat" | |
48 | +filename i = "file" ++ show i ++ ".dat" | |
51 | 49 | |
52 | 50 | fileLength :: FilePath -> IO Int |
53 | 51 | fileLength file = fmap length (readFile file) |
@@ -55,21 +53,30 @@ | ||
55 | 53 | getAllLengths :: [Int] -> IO [Int] |
56 | 54 | getAllLengths nums = mapM (fileLength . filename) nums |
57 | 55 | |
56 | + -- ######################################################### | |
57 | + | |
58 | +-- the 3 functions above are examples by Daniel to calculate the lengths of multiple files | |
59 | +-- and they are a kind of template: the whole point is to replace fileLength with my own function | |
60 | +-- and getAllLengths with the iteration of my own function. | |
61 | + | |
62 | + | |
63 | +entropy_on_file file = fmap entropy_list (conv_to_list file) | |
64 | + | |
65 | +-- entropy_on_file is along the lines of fileLength by Daniel, just I need to be careful when | |
66 | +-- reading something (a string NOT consisting only of numbers [there are also letters]) into a list | |
67 | +-- (I now need to use conv_to_list instead of readFile). | |
68 | + | |
69 | + | |
70 | +get_all_entropies nums = mapM (entropy_on_file . filename) nums | |
71 | + | |
72 | +-- get_all_entropies is precisely along the lines of getAllLengths by Daniel and it iterates | |
73 | +-- entropy_on_file on my file list. | |
74 | + | |
58 | 75 | |
59 | 76 | conv_to_list file = fmap lines ( (readFile file)) |
60 | 77 | |
61 | 78 | file_conv_all nums = mapM (conv_to_list . filename) nums |
62 | 79 | |
63 | --- entropy_calc_all nums = mapM (entropy_list .file_conv_all) nums | |
64 | - | |
65 | - | |
66 | --- getAllentr :: [Int] -> IO [Int] | |
67 | --- getAllentr nums = mapM (fileLength . filename) nums | |
68 | - | |
69 | - | |
70 | - | |
71 | - | |
72 | - | |
73 | 80 | is_sublist sublist list = sublist `isInfixOf` list |
74 | 81 | |
75 | 82 |