Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* Задание 1 isLeapYear *)
- fun isLeapYear (year : int) : bool =
- year mod 400 = 0
- orelse year mod 100 <> 0 andalso year mod 4 = 0
- (* Задание 2 isCorrectDate *)
- (* Код выглядит не совсем так как хотел но в sml блок if bool then - else - не раздельны*)
- fun isCorrectDate (d : int, m : int, y : int) : bool =
- if d < 1
- then false
- else if m = 1 orelse m = 3 orelse m = 5 orelse m = 7
- orelse m = 8 orelse m = 10 orelse m = 12
- then if d <= 31 then true else false
- else if m = 4 orelse m = 6 orelse m = 9 orelse m = 11
- then if d <= 30 then true else false
- else if m = 2
- then if d <= 28 then true
- else if d = 29 andalso isLeapYear y
- then true else false
- else false
- (* Задание 3 newStyleCorrection *)
- fun newStyleCorrection (d : int, m : int, y : int) : int =
- let
- val gregorianLY : int = y div 4 - y div 100 + y div 400; (* Определяем количеств високосных годов *)
- val julianLY : int = y div 4; (* Количество високосных годов по юлианскому *)
- val correction : int = if y mod 4 = 0 andalso not (isLeapYear y) (* корректируем на прошло ли 28 февраля если по юлияновском высокосный а по григ. нет *)
- then if m <= 2 then 1 else 0
- else 0
- in
- julianLY - (gregorianLY + correction) - 2
- end
- (* Задание 4 getNthInt *)
- fun getNthInt (lst : int list, n : int) : int =
- if n > 0
- then getNthInt (tl lst, n - 1)
- else hd lst
- (* Задание 5 getNthStr *)
- fun getNthStr (lst : string list, n : int) : string =
- if n > 0
- then getNthStr (tl lst, n - 1)
- else hd lst
- (* Задание 6 lastSmaller *)
- fun lastSmaller (amount : int, lst : int list) : int =
- let
- fun helperFunc (res : int, lst : int list) : int =
- if not (null lst) andalso hd lst < amount (* lst <> nil *)
- then helperFunc(hd lst, tl lst)
- else res
- in
- if hd lst >= amount (* предпологается что список сорт. по взрст.*)
- then 0
- else helperFunc(hd lst, lst)
- end
- (* Списки для выполнения задания 8 (списки поправок) *)
- (* поправка на тысячи года *)
- val thousandCorrection = [ 0, 1390000, 2770000, 1210000, 2590000
- , 1030000, 2420000 ]
- (* поправка на сотни года *)
- val hundredCorrection = [ 0, 430000, 870000, 1300000, 1740000
- , 2170000, 2600000, 80000, 520000, 950000 ]
- (* поправка на десятки года *)
- val decadeCorrection = [ 0, 930000, 1860000, 2790000, 760000
- , 1690000, 2620000, 600000, 1530000, 2460000 ]
- (* поправка на единицы года *)
- val yearCorrection = [ 0, 1860000, 780000, 2640000, 1550000
- , 460000, 2330000, 1240000, 150000, 2020000 ]
- (* поправка на месяц *)
- val monthCorrection = [ 1340000, 1190000, 2420000, 2260000, 2200000
- , 2060000, 2000000, 1840000, 1700000, 1660000
- , 1510000, 1480000 ]
- (* календарная поправка *)
- val calendarCorrection = [0, 20000, 50000, 80000]
- (* поправка для нормализации дня месяца *)
- val reductions = [2953059, 5906118, 8859177, 11812236, 14765295, 17718354]
- (* Задание 7 firstNewMoonInt *)
- fun firstNewMoonInt (d : int, m : int, y : int) : int option =
- let
- val tmpY : int = if (m = 2 orelse m = 1) then y - 1 else y
- val delta : int = newStyleCorrection(d, m, tmpY) * 100000
- val thousand : int = getNthInt(thousandCorrection, tmpY div 1000)
- val hundred : int = getNthInt(hundredCorrection, (tmpY mod 1000) div 100)
- val decade : int = getNthInt(decadeCorrection, (tmpY mod 100) div 10)
- val year : int = getNthInt(yearCorrection, tmpY mod 10);
- val month : int = getNthInt(monthCorrection, m - 1)
- val calendar : int = getNthInt(calendarCorrection, tmpY mod 4)
- val sum : int = delta + thousand + hundred + decade + year + month + calendar
- val lastSmall : int = lastSmaller(sum - 100000, reductions);
- val res : int = sum - lastSmall;
- val wholeRes : int = res div 100000;
- in
- if m = 2
- then if wholeRes <= 28 orelse
- isLeapYear y andalso wholeRes = 29
- then SOME res else NONE
- else if wholeRes <= 30
- then SOME res
- else NONE
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement