home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / ML / GIML / FLUNKY.TXT < prev    next >
Text File  |  1996-12-09  |  3KB  |  65 lines

  1. (* flunky *)
  2. val path = if (!System.architecture=".hppa") then 
  3.     "/users/staff/sez125/election/" else
  4.     "/usr/local/etc/ftp/pub/a.cumming/election/";
  5. use (path^"parse.sml");
  6. val allFiles = map (fn s=>path^"1983."^s^".txt") ["01","02","03","04","05",
  7.          "06","07","08","09","10", "11","12","13","14","15", "16","17"];
  8. fun uniq l = let
  9.   fun uniq' acc nil = acc
  10.   |   uniq' acc(h::t) = if member acc h then uniq' acc t
  11.                       else uniq' (h::acc) t
  12. in uniq' nil l end;
  13.  
  14. (* Find the party which gained most seats in the most recent contest *)
  15. fun flunky allFiles = let
  16.   val allSeats = flatten(map read allFiles)
  17.   val winners = map (party o hd o hd o rev o snd) allSeats
  18.   val contenders = uniq winners
  19.   fun countSeats p = length(filter (member [p]) winners)
  20.   val psc = map (fn p => (p, countSeats p)) contenders
  21.   val fl = fst(hd(sort (fn(a,b)=>snd a>snd b) psc))
  22. in fl end;
  23.  
  24. (* Find the name of the MP for constituency number x *)
  25. fun MP allFiles x = let
  26.   val allConstituencies = flatten(map read allFiles)
  27.   val targetConstituency = nth(allConstituencies,x-1)
  28.   fun nameOf s = substring(s,24,min(30,size s - 24))
  29. in nameOf(hd(hd(rev(snd targetConstituency)))) end;
  30.  
  31. (* Find the key Tory marginals - Conservative seat where the margin
  32.    between the winner and the runner up is less than or equal to x *)
  33. fun marginal allFiles x = let
  34.   val allSeats = flatten(map read allFiles)
  35.   fun isTorySeatP con = (party(hd(hd(rev(snd con))))="C      ")
  36.   fun isMarginalP con = let
  37.     val contest = hd(rev(snd con))
  38.     val winner_votes = votes(hd contest)
  39.     val runner_up_votes = votes(hd(tl contest))
  40.   in winner_votes - runner_up_votes <= x end
  41.   val targetSeats = filter (fn c => (isTorySeatP c) andalso (isMarginalP c))
  42.         allSeats
  43. in map fst targetSeats end;
  44.  
  45. (* Redistribute the seats based on popular vote - exclude parties with
  46.    less than barValue% of the national vote *)
  47. fun pr allFiles barValue = let
  48.   val allSeats = flatten(map read allFiles)
  49.   val contests = map (hd o rev o snd) allSeats
  50.   val whoGotWhat = map (fn c => (party c,votes c)) (flatten contests)
  51.   val contenders = uniq (map fst whoGotWhat)
  52.   fun voteTot p = (p,sum(map snd(filter (fn(pn,_)=>pn=p)whoGotWhat)))
  53.   val whoGotWhatCondensed = map voteTot contenders
  54.   val bar = floor(real(sum(map snd whoGotWhatCondensed))*barValue/100.0)
  55.   val newContenders = filter (fn(_,v)=> v>=bar) whoGotWhatCondensed
  56.   val newVoteTot = real(sum(map snd newContenders))
  57.   fun redistrib(p,v) = (p,650.0*(real v)/newVoteTot)
  58. in filter (fn(_,v)=>v>0.5) (map redistrib newContenders) end;
  59.  
  60. (* Try them out *)
  61. flunky allFiles;
  62. MP allFiles 568;
  63. app print (marginal allFiles 1000);
  64. pr allFiles 5.0;
  65.