Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. (*Question 2 FINAL FINAL*)
  2. local
  3. fun min var1 var2 = if var1<var2 then var1 else var2
  4. fun makeMinFirst var [] = [] |
  5. makeMinFirst var (x::xs) = (min var x):: (makeMinFirst var xs)
  6. in
  7. fun max_city [] _ = [] |
  8. max_city (west::xs) north = makeMinFirst west north :: max_city xs north
  9.  
  10. end;
  11.  
  12. local
  13. fun transpo ([]::_) = []
  14. |transpo rows =
  15. (map hd rows) :: transpo (map tl rows);
  16.  
  17. fun initList 0 = [] | initList size = (~1):: (initList (size-1));
  18.  
  19. fun correctLine [] _ _ _= [] |
  20. correctLine (north::xs) var numOfRepeats maxRepatNorth =
  21.  
  22. if((var=north) andalso (numOfRepeats=1) andalso (maxRepatNorth >= 1)) then var::(initList (length xs))
  23. else if ((var=north) andalso (numOfRepeats >1 ) andalso (maxRepatNorth = 1)) then var::correctLine xs var (numOfRepeats-1) maxRepatNorth
  24. else if ((var=north) andalso numOfRepeats >1) then (~1)::correctLine xs var (numOfRepeats-1) (maxRepatNorth-1)
  25. else if ((north > var) andalso (maxRepatNorth = 0)) then var::(initList (length xs))
  26. else (~1)::correctLine xs var numOfRepeats maxRepatNorth ;
  27.  
  28.  
  29. fun maxRepeat x [] repeats = repeats |
  30. maxRepeat x (y::ys) repeats = if(x=y) then maxRepeat x ys (repeats+1) else maxRepeat x ys repeats ;
  31.  
  32.  
  33. fun apearannceTillIndexOfWest _ 0 _ repeatance = repeatance |
  34. apearannceTillIndexOfWest [] _ _ repeatance = repeatance|
  35. apearannceTillIndexOfWest (west::xs) lineNum var repeatance= if(var=west) then (apearannceTillIndexOfWest xs (lineNum-1) var (repeatance+1))
  36. else apearannceTillIndexOfWest xs (lineNum-1) var repeatance ;
  37.  
  38. fun buildLine x west north lineNum = correctLine north x (apearannceTillIndexOfWest west lineNum x 0) (maxRepeat x north 0) ;
  39.  
  40. fun buildMat [] _ _ _ = [] |
  41. buildMat (x::xs) west north lineNum= (buildLine x west north lineNum)::(buildMat xs west north (lineNum+1));
  42.  
  43. fun unionLine [] [] = [] |
  44. unionLine (x::xs) (y::ys) = if(x=(~1)) then y::unionLine xs ys else x::unionLine xs ys;
  45.  
  46. fun unionmMat [] [] = [] |
  47. unionmMat (x::xs) (y::ys) = (unionLine x y)::(unionmMat xs ys);
  48.  
  49. fun buildUnionMat west north = unionmMat (buildMat west west north 1) (transpo (buildMat north north west 1));
  50.  
  51. fun replaceZero [] = [] |
  52. replaceZero (x::xs) = if(x=(~1)) then (0 :: replaceZero xs) else x::replaceZero xs ;
  53. fun makeZero [] = [] |
  54. makeZero (x::xs) = replaceZero x:: makeZero xs;
  55.  
  56. in
  57. fun min_city west north = makeZero (buildUnionMat west north)
  58.  
  59.  
  60. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement