Advertisement
Vladi1442

Untitled

Jun 30th, 2022
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. main :: IO()
  2. main = do
  3.     print $ sunkK 0 database == [("Germany",0),("Gt.Britain",0)]
  4.     print $ sunkK 2 database == [("Germany",0),("Japan",2),("Gt.Britain",0)]
  5.     print $ sunkK 9 database == [("Germany",0),("USA",4),("Japan",2),("Gt.Britain",0)]
  6.     print $ sunkK 20 database == [("Germany",0),("USA",4),("Japan",2),("Gt.Britain",0)]
  7.  
  8. -- algebraic data types
  9.  
  10. type Name = String
  11. type Date = String
  12. type Class = String
  13. type Type = String
  14. type Result = String
  15. type Launched = Int
  16.  
  17. type Battle = (Name, Date)
  18. type Ship = (Name, Class, Launched)
  19. type Outcome = (Name, Name, Result)
  20. type ShipClass = (Name, Type, Name)
  21.  
  22. type Database = ([Outcome], [Battle], [Ship], [ShipClass])
  23.  
  24. outcomes :: [Outcome]
  25. outcomes = [ ("Bismarck", "North Atlantic", "sunk"),
  26. ("California", "Surigao Strait", "ok"), ("Duke of York",
  27. "North Cape", "ok"), ("Fuso", "Surigao Strait", "sunk"),
  28. ("Hood", "North Atlantic", "sunk"), ("King George V", "North
  29. Atlantic", "ok"), ("Kirishima", "Guadalcanal", "sunk"),
  30. ("Prince of Wales", "North Atlantic", "damaged"), ("Rodney",
  31. "North Atlantic", "ok"), ("Schamhorst", "North Cape",
  32. "sunk"), ("South Dakota", "Guadalcanal", "damaged"),
  33. ("Tennessee", "Surigao Strait", "ok"), ("Washington",
  34. "Guadalcanal", "ok"), ("Prince of Wales", "Guadalcanal",
  35. "ok"), ("West Virginia", "Surigao Strait", "ok"),
  36. ("Yamashiro", "Surigao Strait", "sunk"), ("California",
  37. "Guadalcanal", "damaged") ]
  38.  
  39. battles :: [Battle]
  40. battles = [ ("Guadalcanal", "1942-11-15"), ("North Atlantic",
  41. "1941-05-25"), ("North Cape", "1943-12-26"), ("Surigao
  42. Strait", "1944-10-25") ]
  43.  
  44. ships :: [Ship]
  45. ships = [ ("California", "Tennessee", 1921), ("Haruna",
  46. "Kongo", 1916), ("Hiei", "Kongo", 1914), ("Iowa", "Iowa",
  47. 1943), ("Kirishima", "Kongo", 1915), ("Kongo", "Kongo", 1913),
  48. ("Missouri", "Iowa", 1944), ("Musashi", "Yamato", 1942), ("New
  49. Jersey", "Iowa", 1943), ("North Carolina", "North Carolina",
  50. 1941), ("Ramillies", "Revenge", 1917), ("Renown", "Renown",
  51. 1916), ("Repulse", "Renown", 1916), ("Resolution", "Renown",
  52. 1916), ("Revenge", "Revenge", 1916), ("Royal Oak", "Revenge",
  53. 1916), ("Royal Sovereign", "Revenge", 1916), ("Tennessee",
  54. "Tennessee", 1920), ("Washington", "North Carolina", 1941),
  55. ("Wisconsin", "Iowa", 1944), ("Yamato", "Yamato", 1941),
  56. ("Yamashiro", "Yamato", 1947), ("South Dakota", "North
  57. Carolina", 1941), ("Bismarck", "North Carolina", 1911), ("Duke
  58. of York", "Renown", 1916), ("Fuso", "Iowa", 1940), ("Hood",
  59. "Iowa", 1942), ("Rodney", "Yamato", 1915), ("Yanashiro",
  60. "Yamato", 1918), ("Schamhorst", "North Carolina", 1917),
  61. ("Prince of Wales", "North Carolina", 1937), ("King George V",
  62. "Iowa", 1942), ("West Virginia", "Iowa", 1942) ]
  63.  
  64. classes :: [ShipClass]
  65. classes = [("Bismarck", "bb", "Germany"), ("Iowa", "bb",
  66. "USA"), ("Kongo", "bc", "Japan"), ("North Carolina", "bb",
  67. "USA"), ("Renown", "bc", "Gt.Britain"), ("Revenge", "bb",
  68. "Gt.Britain"), ("Tennessee", "bb", "USA"), ("Yamato", "bb",
  69. "Japan")]
  70.  
  71.  
  72. database :: Database
  73. database = (outcomes, battles, ships, classes)
  74.  
  75. -- bs - battles, ss - ships, os - outcomes, sc - shipClass
  76.  
  77. sunkK :: Int -> Database -> [(Name, Int)]
  78. sunkK n (os, bs, ss, sc) = [(name, countOfSunkShips n) | name <- allCountriesName]
  79.     where
  80.         allCountries :: [Name]
  81.         allCountries = [name | (_, _, name) <- sc]
  82.  
  83.         countOfSunkShips :: Name -> [Name]
  84.         countOfSunkShips bName = length [ ship | (shipName, bN , result) <- os, bN == bName, result == "sunk"]
  85.  
  86.        
  87.  
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement