大久保弘崇
Errata-9
最終更新:
hirotakaohkubo
-
view
Ch.18
p.139, p.144 definition of newplans
"qms = moves q" should be in OUTER "where" level.
p.140 psearch'
Wrong: | solved q = Just (reverse ms)
Correct: | solved q = Just ms
Correct: | solved q = Just ms
p.143 definition of solved, covers
"= 20", "= 0" should be "== 20", "== 0"
Refactoring the code
Every occurence of
is so annoying, so hide'em by some utility functions.
vh (r,f) = r > f - 7 -- vehicle is horizontal
vd (r,f) | vh (r, f) = 1 -- delta of vehicle
| otherwise = 7
vl (r,f) = case r-f of -- absolute length of vehicle
1 -> 2
2 -> 3
7 -> 2
14 -> 3
(These values are invariant for one game,
so, instead of calculating every time,
you may store them in some array, with using vehicle number as index.)
so, instead of calculating every time,
you may store them in some array, with using vehicle number as index.)
Using there functions, some clauses are merged.
fillcells (r,f) = [r,r+d .. f] where d = vd (r,f)
adjs (r,f) = [f+d, r−d] where d = vd (r,f)
adjust (r,f) c = if f < c then (r+d, c) else (c, f-d) where d = vd (r,f)
covers c (r,f) = c `elem` fillcells (r,f)
expand g (v,c) = if c > f then [(v,p) | p ← [f+d,f+2∗d..c]]
else [(v,p) | p ← [r−d,r−2∗d..c]]
where (r,f) = g !! v
d = vd (r,f)
The function "freeingmoves" is the hard one.
freeingmoves c (v,(r,f)) = (if c'+l'<7 then [pos] else []) ++ (if c'−l'>0 then [neg] else [])
where d = vd (r,f)
c'= if vh (r,f) then (c mod 7) else (c div 7 + 1)
l'= vl (r,f)
len = l' * d
pos = [(v,j) | j <- [f+d,f+2∗d .. c+len]]
neg = [(v,j) | j <- [r−d,r−2∗d .. c−len]]
This definition is slightly different to the original.
When the vehicle is vertical, original definition returns [neg, pos].
On the other hand, my definition returns [pos, neg].
But they are essentially same.
When the vehicle is vertical, original definition returns [neg, pos].
On the other hand, my definition returns [pos, neg].
But they are essentially same.
Another refactoring is elimination of search.
I do not care how these modification affects to efficiency or compiler optimization.