Компиляция кода
15.12.2013, 17:52. Показов 963. Ответов 1
Здраствуйте,есть код на хаскелле,при компиляции через сервис http://codepad.org/ жалуется на 9ую строку(хотя это только начало).
код
| Haskell | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
| import System.IO
import Array
import qualified Data.Set as Set
import Data.List
import Data.Maybe
import Text.Printf
computeCost :: Int -> Int -> Array(Int,Int) Double -> Array Int Double -> Array (Int , Int) Double -> Array Int Double -> Double
computeCost nStations nConcs costs concCosts conns concsOn =
let part1 = sum $ zipWith (*) (elems concsOn)(elems concCosts)
part2 = sum $map (\ i —>
sum $map (\ j —> conns!(i, j) * costs!(i, j)) [0.. nConcs ]
) [ 1 .. nStations ]
in part1 + part2
getStationConc :: Int —> Int —> Array (Int , Int) Double —> Int
getStationConc station nConcs conns = let Just curConc = find (\ conc —> conns !( station , conc) == 1) [0.. nConcs] in curConc
getNewConcDiff::Int->Int->Int->Array(Int , Int) Double -> Array (Int,Int , Int) Double —>Double
getNewConcDiff station newConc nConcs conns diffs = diffs !(station , newConc, getStationConc station nConcs conns )
reconnectStations ::[ Int ] —> Int —> Int —> Array (Int , Int) Double —> Array (Int , Int) Double
reconnectStations stations conc nConcs conns =let newConns = map (\ station —> ((station,conc), 1)) stations
oldConns = map (\ station oldConns = map (\ station —> ((station , getStationConc station nConcs conns), 0)) stations
in conns//( newConns ++ oldConns)
addConcsCore :: Int —> Int —> Int —> Array (Int , Int) Double —> Array Int Double —> Array (Int , Int , Int)
Double —> Set. Set Int —> Array (Int , Int) Double —> Array Int Double —> Double —> IO ()
addConcsCore nStations nConcs maxStations costs concCosts diffs newConcs conns concsOn cost = do res <— mapM (\ conc —> do
let concsOn’ = concsOn//[( conc , 1)]
stationsCost = map (\ station —> ( station , getNewConcDiff s tation conc nConcs conns diffs)) ([ 1 .. nStations — 1] ++ [nStations])
stationsCostSorted = sortBy (\(_, cost1 ) (_, cost2) —> if cost1 < cost2 then GT else LT) $ [] ++ stationsCost stationsToConnect = map fst $takeWhile (\(_, cost) —> cost > 0)$ take maxStations stationsCostSorted
conns ’ = reconnectStations stationsToConnect conc nConcs conns
cost’ = computeCost nStations nConcs costs concCosts conns’ concsOn’
return (conns ’ , concsOn ’ , cost ’ , conc)
) $ reverse (Set . toAscList newConcs)
let target = find (\(_, c, _) —> c < cost) res if isJust target then do
let Just (conns’, concsOn’, cost’, conc) = target
addConcsCore nStations nConcs maxStations costs concCosts diffs (Set. delete conc newConcs) conns’ concsOn ’ cost ’ else do
outH <— openFile ”rc — lab3.add” WriteMode hPrintf outH ”Add„mode\n%f\n” (cost :: Double)
mapM_ (\ i —> hPrintf outH ”%10.3f” (concsOn !i :: Double)) [0.. nConcs] hPutStrLn outH ”\n”
mapM_ (\ i —> do
mapM_ (\j —> hPrintf outH ”%10.3f” (conns !(i, j) :: Double)) [0..nConcs]
hPutStrLn outH ””
) [ 1 .. nStations ]
hClose outH
— Optimizes the network by adding concentrators .
addConcs: : Int —> Int —> Int —> Array (Int , Int) Double —>Array Int Double —> IO ()
addConcs nStations nConcs maxStations costs concCosts = do
— Connect all stations to the node.
let conns = array ((1, 0), (nStations , nConcs)) $
[((i, j), 0.0) | i <— [ 1 .. nStations ] , j <— [1..nConcs]]
++ [((i, 0), 1.0) | i <— [ 1 .. nStations ] ] — station —> node
— All concentrators are in the network already.
concsOn = array (0, nConcs) $ (0, 1.0) : [(i, 0.0) | i <— [1..nConcs]]
— The set of concentrators which have not been included in the —— n e t w o r k y e t .
newConcs = Set.fromList [1..nConcs]
— The initial cost of the network.
cost = computeCost nStations nConcs costs concCosts conns concsOn
— All pairs of differences of costs.
diffs = array ((1, 0, 0), (nStations, nConcs, nConcs))
[((i , j , k) , costs !(i , k) — costs !(i , j ))| i <— [ 1.. nStations ] , j <— [0..nConcs], k <— [0..nConcs]]
—— The sets of stations connected to each of the concentrators .
stations = array (1, nConcs) [(i, Set.empty) | i <— [0..nConcs]]
statCons = map ( \ station —>
map (\ conc—> (conc, getNewConcDiff station conc nConcs conns diffs)) [ 1 .. nConcs ] ) [ 1 .. nStations ] maxStatConcs = map maxList statConcs conns ' = conns
addConcsCore nStations nConcs maxStations costs concCosts diffs newConcs conns ' concsOn cost return ()
remConcsCore :: Int —> Int —> Int —> Array (Int , Int) Double —> Array Int Double —> Array (Int , Int , Int)
Double —> Set . Set Int —> Array (Int , Int) Double —> Array Int Double —> Double —> IO ()
remConcsCore nStations nConcs maxStations costs concCosts diffs newConcs conns concsOn cost = do res <— mapM (\ conc —> do
let concsOn ’ = concsOn//[ ( conc , 1)]
stationsCost = map (\ station —> ( station , getNewConcDiff s tation conc nConcs conns diffs)) [ 1 .. nStations ] stationsCostSorted = sortBy (\(_, cost1 ) (_, cost2) —>
if cost1 < cost2 then GT else LT) stationsCost
stationsToConnect = map fst $ takeWhile (\(_, cost) —> cost > 0) $ take maxStations stationsCostSorted
conns’ = reconnectStations stationsToConnect conc nConcs conns
cost’ = computeCost nStations nConcs costs concCosts conns’ concsOn’
return (conns ’ , concsOn ’ , cost ’ , conc)
) ( Set. toAscList newConcs)
let target = find (\(_, _, c, _) —> c < cost) res if isJust target then do
let Just (conns’, concsOn’, cost’, conc) = target
remConcsCore nStations nConcs maxStations costs concCosts diffs (Set. delete conc newConcs) conns’ concsOn ’ cost ’ e l s e do
outH <— openFile ”rc — lab3.rem” WriteMode
hPrintf outH ” \ nRemove„mode: \ n%f \ n” (cost :: Double)
mapM_ (\ i —> hPrintf outH ”%10.3f” (concsOn !i :: Double)) [0.. nConcs] hPutStrLn outH ”\n”
mapM_ (\ i —> do
mapM_ (\j —> hPrintf outH ”%10.3f” (conns !(i, j) :: Double)) [0..nConcs]
hPutStrLn outH ””
) [ 1 .. nStations ]
hClose outH
maxList :: [(a, Double)] —> (a, Double)
maxList list = foldr (\(a, el) (b, res) —> if el > res then (a, el) else (b, res)) (head list) list
— Optimizes the network by removing concentrators.
remConcs :: Int —> Int —> Int —> Array (Int , Int) Double —> Array Int Double —> IO ()
remConcs nStations nConcs maxStations costs concCosts = do
— Connect all stations to the node.
let conns = array ((1, 0), (nStations, nConcs)) $
[((i, j), 0.0) | i <— [ 1 .. nStations ] , j <— [1..nConcs]]
++ [((i, 0), 1.0) | i <— [ 1 .. nStations ] ] — station —> node
— All concentrators are in the network yet.
concsOn = array (0, nConcs) $ [(i, 0.0) | i <— [0.. nConcs]]
— The set of concentrators which have not been excluded from the —— n e t w o r k y e t .
newConcs = Set.fromList [1..nConcs]
— The initial cost of the network.
cost = computeCost nStations nConcs costs concCosts conns concsOn
— All pairs of differences of costs.
diffs = array ((1, 0, 0), (nStations, nConcs, nConcs))
[((i , j , k) , costs !(i , k) — costs !(i , j ))
| i <— [ 1.. nStations ] , j <— [0..nConcs], k <— [0..nConcs]]
—— The sets of stations connected to each of the concentrators .
stations = array (1, nConcs) [(i, Set.empty) | i <— [0..nConcs]]
remConcsCore nStations nConcs maxStations costs concCosts diffs newConcs conns concsOn cost
return ()
main : : IO () main = do
inH <— openFile ”rc—lab3 . in” ReadMode
—— The number of stations . nStations <— readIntLine inH
—— The number of concentrators . nConcs <— readIntLine inH
— The maximal number of stations that can be connected to a —— concentrator .
maxStations <— readIntLine inH
— The station —concentrator connection cost matrix. hGetLine inH
costList <— mapM (\ i —> do
line <— hGetLine inH
let vals = map (\ cost —> read cost :: Double) $ words line
return $ map (\(j , val) —> ((i, j), val)) $ zip [0..nConcs] vals ) [ 1.. nStations ]
let costs = array ((1, 0), (nStations , nConcs)) $ foldr1 (+ + ) costList
—— The array of costs of concentrators . hGetLine inH
concCostList <— mapM (\ i —> do
line <— hGetLine inH
return $ (i , (read line :: Double))
) [ 0 . . nConcs ]
let concCosts = array (0, nConcs) concCostList
hClose inH
addConcs nStations nConcs maxStations costs concCosts
remConcs nStations nConcs maxStations costs concCosts
where readIntLine handle = do
line <— hGetLine handle return $ (read line :: Int) |
|
Добавлено через 9 минут

Сообщение от Vasyan
Здраствуйте,есть код на хаскелле,при компиляции через сервис http://codepad.org/ жалуется на 9ую строку(хотя это только начало).
код
| Haskell | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
| import System.IO
import Array
import qualified Data.Set as Set
import Data.List
import Data.Maybe
import Text.Printf
computeCost :: Int -> Int -> Array(Int,Int) Double -> Array Int Double -> Array (Int , Int) Double -> Array Int Double -> Double
computeCost nStations nConcs costs concCosts conns concsOn =
let part1 = sum $ zipWith (*) (elems concsOn)(elems concCosts)
part2 = sum $map (\ i —>
sum $map (\ j —> conns!(i, j) * costs!(i, j)) [0.. nConcs ]
) [ 1 .. nStations ]
in part1 + part2
getStationConc :: Int —> Int —> Array (Int , Int) Double —> Int
getStationConc station nConcs conns = let Just curConc = find (\ conc —> conns !( station , conc) == 1) [0.. nConcs] in curConc
getNewConcDiff::Int->Int->Int->Array(Int , Int) Double -> Array (Int,Int , Int) Double —>Double
getNewConcDiff station newConc nConcs conns diffs = diffs !(station , newConc, getStationConc station nConcs conns )
reconnectStations ::[ Int ] —> Int —> Int —> Array (Int , Int) Double —> Array (Int , Int) Double
reconnectStations stations conc nConcs conns =let newConns = map (\ station —> ((station,conc), 1)) stations
oldConns = map (\ station oldConns = map (\ station —> ((station , getStationConc station nConcs conns), 0)) stations
in conns//( newConns ++ oldConns)
addConcsCore :: Int —> Int —> Int —> Array (Int , Int) Double —> Array Int Double —> Array (Int , Int , Int)
Double —> Set. Set Int —> Array (Int , Int) Double —> Array Int Double —> Double —> IO ()
addConcsCore nStations nConcs maxStations costs concCosts diffs newConcs conns concsOn cost = do res <— mapM (\ conc —> do
let concsOn’ = concsOn//[( conc , 1)]
stationsCost = map (\ station —> ( station , getNewConcDiff s tation conc nConcs conns diffs)) ([ 1 .. nStations — 1] ++ [nStations])
stationsCostSorted = sortBy (\(_, cost1 ) (_, cost2) —> if cost1 < cost2 then GT else LT) $ [] ++ stationsCost stationsToConnect = map fst $takeWhile (\(_, cost) —> cost > 0)$ take maxStations stationsCostSorted
conns ’ = reconnectStations stationsToConnect conc nConcs conns
cost’ = computeCost nStations nConcs costs concCosts conns’ concsOn’
return (conns ’ , concsOn ’ , cost ’ , conc)
) $ reverse (Set . toAscList newConcs)
let target = find (\(_, c, _) —> c < cost) res if isJust target then do
let Just (conns’, concsOn’, cost’, conc) = target
addConcsCore nStations nConcs maxStations costs concCosts diffs (Set. delete conc newConcs) conns’ concsOn ’ cost ’ else do
outH <— openFile ”rc — lab3.add” WriteMode hPrintf outH ”Add„mode\n%f\n” (cost :: Double)
mapM_ (\ i —> hPrintf outH ”%10.3f” (concsOn !i :: Double)) [0.. nConcs] hPutStrLn outH ”\n”
mapM_ (\ i —> do
mapM_ (\j —> hPrintf outH ”%10.3f” (conns !(i, j) :: Double)) [0..nConcs]
hPutStrLn outH ””
) [ 1 .. nStations ]
hClose outH
— Optimizes the network by adding concentrators .
addConcs: : Int —> Int —> Int —> Array (Int , Int) Double —>Array Int Double —> IO ()
addConcs nStations nConcs maxStations costs concCosts = do
— Connect all stations to the node.
let conns = array ((1, 0), (nStations , nConcs)) $
[((i, j), 0.0) | i <— [ 1 .. nStations ] , j <— [1..nConcs]]
++ [((i, 0), 1.0) | i <— [ 1 .. nStations ] ] — station —> node
— All concentrators are in the network already.
concsOn = array (0, nConcs) $ (0, 1.0) : [(i, 0.0) | i <— [1..nConcs]]
— The set of concentrators which have not been included in the —— n e t w o r k y e t .
newConcs = Set.fromList [1..nConcs]
— The initial cost of the network.
cost = computeCost nStations nConcs costs concCosts conns concsOn
— All pairs of differences of costs.
diffs = array ((1, 0, 0), (nStations, nConcs, nConcs))
[((i , j , k) , costs !(i , k) — costs !(i , j ))| i <— [ 1.. nStations ] , j <— [0..nConcs], k <— [0..nConcs]]
—— The sets of stations connected to each of the concentrators .
stations = array (1, nConcs) [(i, Set.empty) | i <— [0..nConcs]]
statCons = map ( \ station —>
map (\ conc—> (conc, getNewConcDiff station conc nConcs conns diffs)) [ 1 .. nConcs ] ) [ 1 .. nStations ] maxStatConcs = map maxList statConcs conns ' = conns
addConcsCore nStations nConcs maxStations costs concCosts diffs newConcs conns ' concsOn cost return ()
remConcsCore :: Int —> Int —> Int —> Array (Int , Int) Double —> Array Int Double —> Array (Int , Int , Int)
Double —> Set . Set Int —> Array (Int , Int) Double —> Array Int Double —> Double —> IO ()
remConcsCore nStations nConcs maxStations costs concCosts diffs newConcs conns concsOn cost = do res <— mapM (\ conc —> do
let concsOn ’ = concsOn//[ ( conc , 1)]
stationsCost = map (\ station —> ( station , getNewConcDiff s tation conc nConcs conns diffs)) [ 1 .. nStations ] stationsCostSorted = sortBy (\(_, cost1 ) (_, cost2) —>
if cost1 < cost2 then GT else LT) stationsCost
stationsToConnect = map fst $ takeWhile (\(_, cost) —> cost > 0) $ take maxStations stationsCostSorted
conns’ = reconnectStations stationsToConnect conc nConcs conns
cost’ = computeCost nStations nConcs costs concCosts conns’ concsOn’
return (conns ’ , concsOn ’ , cost ’ , conc)
) ( Set. toAscList newConcs)
let target = find (\(_, _, c, _) —> c < cost) res if isJust target then do
let Just (conns’, concsOn’, cost’, conc) = target
remConcsCore nStations nConcs maxStations costs concCosts diffs (Set. delete conc newConcs) conns’ concsOn ’ cost ’ e l s e do
outH <— openFile ”rc — lab3.rem” WriteMode
hPrintf outH ” \ nRemove„mode: \ n%f \ n” (cost :: Double)
mapM_ (\ i —> hPrintf outH ”%10.3f” (concsOn !i :: Double)) [0.. nConcs] hPutStrLn outH ”\n”
mapM_ (\ i —> do
mapM_ (\j —> hPrintf outH ”%10.3f” (conns !(i, j) :: Double)) [0..nConcs]
hPutStrLn outH ””
) [ 1 .. nStations ]
hClose outH
maxList :: [(a, Double)] —> (a, Double)
maxList list = foldr (\(a, el) (b, res) —> if el > res then (a, el) else (b, res)) (head list) list
— Optimizes the network by removing concentrators.
remConcs :: Int —> Int —> Int —> Array (Int , Int) Double —> Array Int Double —> IO ()
remConcs nStations nConcs maxStations costs concCosts = do
— Connect all stations to the node.
let conns = array ((1, 0), (nStations, nConcs)) $
[((i, j), 0.0) | i <— [ 1 .. nStations ] , j <— [1..nConcs]]
++ [((i, 0), 1.0) | i <— [ 1 .. nStations ] ] — station —> node
— All concentrators are in the network yet.
concsOn = array (0, nConcs) $ [(i, 0.0) | i <— [0.. nConcs]]
— The set of concentrators which have not been excluded from the —— n e t w o r k y e t .
newConcs = Set.fromList [1..nConcs]
— The initial cost of the network.
cost = computeCost nStations nConcs costs concCosts conns concsOn
— All pairs of differences of costs.
diffs = array ((1, 0, 0), (nStations, nConcs, nConcs))
[((i , j , k) , costs !(i , k) — costs !(i , j ))
| i <— [ 1.. nStations ] , j <— [0..nConcs], k <— [0..nConcs]]
—— The sets of stations connected to each of the concentrators .
stations = array (1, nConcs) [(i, Set.empty) | i <— [0..nConcs]]
remConcsCore nStations nConcs maxStations costs concCosts diffs newConcs conns concsOn cost
return ()
main : : IO () main = do
inH <— openFile ”rc—lab3 . in” ReadMode
—— The number of stations . nStations <— readIntLine inH
—— The number of concentrators . nConcs <— readIntLine inH
— The maximal number of stations that can be connected to a —— concentrator .
maxStations <— readIntLine inH
— The station —concentrator connection cost matrix. hGetLine inH
costList <— mapM (\ i —> do
line <— hGetLine inH
let vals = map (\ cost —> read cost :: Double) $ words line
return $ map (\(j , val) —> ((i, j), val)) $ zip [0..nConcs] vals ) [ 1.. nStations ]
let costs = array ((1, 0), (nStations , nConcs)) $ foldr1 (+ + ) costList
—— The array of costs of concentrators . hGetLine inH
concCostList <— mapM (\ i —> do
line <— hGetLine inH
return $ (i , (read line :: Double))
) [ 0 . . nConcs ]
let concCosts = array (0, nConcs) concCostList
hClose inH
addConcs nStations nConcs maxStations costs concCosts
remConcs nStations nConcs maxStations costs concCosts
where readIntLine handle = do
line <— hGetLine handle return $ (read line :: Int) |
|
|
PS Есть еще Sublime 3 с плагином Haskell (если можно,подскажите как там запустить)
0
|