给定2010年到2019年之间的日期,该函数应该返回对应于星期日到星期六的int 0到6。该功能基于2010年1月1日是星期五的事实。
为什么对结尾else if year = 2019
的else语句引出整个错误的错误:“这个表达式有int类型但是表达式需要类型unit”?
let day_of_week (year: int) (month: int) (day: int): int =
if year < 2010 || year > 2019 then failwith "year out of range"
else if month < 1 || month > 12 then failwith "invalid month"
else if day < 1 then failwith "invalid day"
else if is_leap_year year != true then (
if month = 2 then (
if day > 28 then failwith "invalid day"
)
) else if month = 2 then
if day > 29 then failwith "invalid day"
else if month = 4 || month = 6 || month = 9 || month = 11 then
if day > 30 then failwith "invalid day"
else if day > 31 then failwith "invalid day"
else if year = 2010 then (
if month = 1 then ((day mod 7) + 4) mod 7
else if month = 2 then (((31 + day) mod 7) + 4) mod 7
else if month = 3 then (((59 + day) mod 7) + 4) mod 7
else if month = 4 then (((90 + day) mod 7) + 4) mod 7
else if month = 5 then (((120 + day) mod 7) + 4) mod 7
else if month = 6 then (((151 + day) mod 7) + 4) mod 7
else if month = 7 then (((181 + day) mod 7) + 4) mod 7
else if month = 8 then (((212 + day) mod 7) + 4) mod 7
else if month = 9 then (((243 + day) mod 7) + 4) mod 7
else if month = 10 then (((273 + day) mod 7) + 4) mod 7
else if month = 11 then (((304 + day) mod 7) + 4) mod 7
else (((334 + day) mod 7) + 4) mod 7
) else if year = 2011 then (
if month = 1 then (((365 + day) mod 7) + 4) mod 7
else if month = 2 then (((365 + 31 + day) mod 7) + 4) mod 7
else if month = 3 then (((365 + 59 + day) mod 7) + 4) mod 7
else if month = 4 then (((365 + 90 + day) mod 7) + 4) mod 7
else if month = 5 then (((365 + 120 + day) mod 7) + 4) mod 7
else if month = 6 then (((365 + 151 + day) mod 7) + 4) mod 7
else if month = 7 then (((365 + 181 + day) mod 7) + 4) mod 7
else if month = 8 then (((365 + 212 + day) mod 7) + 4) mod 7
else if month = 9 then (((365 + 243 + day) mod 7) + 4) mod 7
else if month = 10 then (((365 + 273 + day) mod 7) + 4) mod 7
else if month = 11 then (((365 + 304 + day) mod 7) + 4) mod 7
else (((365 + 334 + day) mod 7) + 4) mod 7
) else if year = 2012 then (
if month = 1 then (((730 + day) mod 7) + 4) mod 7
else if month = 2 then (((730 + 31 + day) mod 7) + 4) mod 7
else if month = 3 then (((730 + 60 + day) mod 7) + 4) mod 7
else if month = 4 then (((730 + 91 + day) mod 7) + 4) mod 7
else if month = 5 then (((730 + 121 + day) mod 7) + 4) mod 7
else if month = 6 then (((730 + 152 + day) mod 7) + 4) mod 7
else if month = 7 then (((730 + 182 + day) mod 7) + 4) mod 7
else if month = 8 then (((730 + 213 + day) mod 7) + 4) mod 7
else if month = 9 then (((730 + 244 + day) mod 7) + 4) mod 7
else if month = 10 then (((730 + 274 + day) mod 7) + 4) mod 7
else if month = 11 then (((730 + 305 + day) mod 7) + 4) mod 7
else (((730 + 335 + day) mod 7) + 4) mod 7
) else if year = 2013 then (
if month = 1 then (((1096 + day) mod 7) + 4) mod 7
else if month = 2 then (((1096 + 31 + day) mod 7) + 4) mod 7
else if month = 3 then (((1096 + 59 + day) mod 7) + 4) mod 7
else if month = 4 then (((1096 + 90 + day) mod 7) + 4) mod 7
else if month = 5 then (((1096 + 120 + day) mod 7) + 4) mod 7
else if month = 6 then (((1096 + 151 + day) mod 7) + 4) mod 7
else if month = 7 then (((1096 + 181 + day) mod 7) + 4) mod 7
else if month = 8 then (((1096 + 212 + day) mod 7) + 4) mod 7
else if month = 9 then (((1096 + 243 + day) mod 7) + 4) mod 7
else if month = 10 then (((1096 + 273 + day) mod 7) + 4) mod 7
else if month = 11 then (((1096 + 304 + day) mod 7) + 4) mod 7
else (((1096 + 334 + day) mod 7) + 4) mod 7
) else if year = 2014 then (
if month = 1 then (((1461 + day) mod 7) + 4) mod 7
else if month = 2 then (((1461 + 31 + day) mod 7) + 4) mod 7
else if month = 3 then (((1461 + 59 + day) mod 7) + 4) mod 7
else if month = 4 then (((1461 + 90 + day) mod 7) + 4) mod 7
else if month = 5 then (((1461 + 120 + day) mod 7) + 4) mod 7
else if month = 6 then (((1461 + 151 + day) mod 7) + 4) mod 7
else if month = 7 then (((1461 + 181 + day) mod 7) + 4) mod 7
else if month = 8 then (((1461 + 212 + day) mod 7) + 4) mod 7
else if month = 9 then (((1461 + 243 + day) mod 7) + 4) mod 7
else if month = 10 then (((1461 + 273 + day) mod 7) + 4) mod 7
else if month = 11 then (((1461 + 304 + day) mod 7) + 4) mod 7
else (((1461 + 334 + day) mod 7) + 4) mod 7
) else if year = 2015 then (
if month = 1 then (((1826 + day) mod 7) + 4) mod 7
else if month = 2 then (((1826 + 31 + day) mod 7) + 4) mod 7
else if month = 3 then (((1826 + 59 + day) mod 7) + 4) mod 7
else if month = 4 then (((1826 + 90 + day) mod 7) + 4) mod 7
else if month = 5 then (((1826 + 120 + day) mod 7) + 4) mod 7
else if month = 6 then (((1826 + 151 + day) mod 7) + 4) mod 7
else if month = 7 then (((1826 + 181 + day) mod 7) + 4) mod 7
else if month = 8 then (((1826 + 212 + day) mod 7) + 4) mod 7
else if month = 9 then (((1826 + 243 + day) mod 7) + 4) mod 7
else if month = 10 then (((1826 + 273 + day) mod 7) + 4) mod 7
else if month = 11 then (((1826 + 304 + day) mod 7) + 4) mod 7
else (((1826 + 334 + day) mod 7) + 4) mod 7
) else if year = 2016 then (
if month = 1 then (((2191 + day) mod 7) + 4) mod 7
else if month = 2 then (((2191 + 31 + day) mod 7) + 4) mod 7
else if month = 3 then (((2191 + 60 + day) mod 7) + 4) mod 7
else if month = 4 then (((2191 + 91 + day) mod 7) + 4) mod 7
else if month = 5 then (((2191 + 121 + day) mod 7) + 4) mod 7
else if month = 6 then (((2191 + 152 + day) mod 7) + 4) mod 7
else if month = 7 then (((2191 + 182 + day) mod 7) + 4) mod 7
else if month = 8 then (((2191 + 213 + day) mod 7) + 4) mod 7
else if month = 9 then (((2191 + 244 + day) mod 7) + 4) mod 7
else if month = 10 then (((2191 + 274 + day) mod 7) + 4) mod 7
else if month = 11 then (((2191 + 305 + day) mod 7) + 4) mod 7
else (((2191 + 335 + day) mod 7) + 4) mod 7
) else if year = 2017 then (
if month = 1 then (((2557 + day) mod 7) + 4) mod 7
else if month = 2 then (((2557 + 31 + day) mod 7) + 4) mod 7
else if month = 3 then (((2557 + 59 + day) mod 7) + 4) mod 7
else if month = 4 then (((2557 + 90 + day) mod 7) + 4) mod 7
else if month = 5 then (((2557 + 120 + day) mod 7) + 4) mod 7
else if month = 6 then (((2557 + 151 + day) mod 7) + 4) mod 7
else if month = 7 then (((2557 + 181 + day) mod 7) + 4) mod 7
else if month = 8 then (((2557 + 212 + day) mod 7) + 4) mod 7
else if month = 9 then (((2557 + 243 + day) mod 7) + 4) mod 7
else if month = 10 then (((2557 + 273 + day) mod 7) + 4) mod 7
else if month = 11 then (((2557 + 304 + day) mod 7) + 4) mod 7
else (((2557 + 334 + day) mod 7) + 4) mod 7
) else if year = 2018 then (
if month = 1 then (((2922 + day) mod 7) + 4) mod 7
else if month = 2 then (((2922 + 31 + day) mod 7) + 4) mod 7
else if month = 3 then (((2922 + 59 + day) mod 7) + 4) mod 7
else if month = 4 then (((2922 + 90 + day) mod 7) + 4) mod 7
else if month = 5 then (((2922 + 120 + day) mod 7) + 4) mod 7
else if month = 6 then (((2922 + 151 + day) mod 7) + 4) mod 7
else if month = 7 then (((2922 + 181 + day) mod 7) + 4) mod 7
else if month = 8 then (((2922 + 212 + day) mod 7) + 4) mod 7
else if month = 9 then (((2922 + 243 + day) mod 7) + 4) mod 7
else if month = 10 then (((2922 + 273 + day) mod 7) + 4) mod 7
else if month = 11 then (((2922 + 304 + day) mod 7) + 4) mod 7
else (((2922 + 334 + day) mod 7) + 4) mod 7
) else if year = 2019 then (
if month = 1 then (((3287 + day) mod 7) + 4) mod 7
else if month = 2 then (((3287 + 31 + day) mod 7) + 4) mod 7
else if month = 3 then (((3287 + 59 + day) mod 7) + 4) mod 7
else if month = 4 then (((3287 + 90 + day) mod 7) + 4) mod 7
else if month = 5 then (((3287 + 120 + day) mod 7) + 4) mod 7
else if month = 6 then (((3287 + 151 + day) mod 7) + 4) mod 7
else if month = 7 then (((3287 + 181 + day) mod 7) + 4) mod 7
else if month = 8 then (((3287 + 212 + day) mod 7) + 4) mod 7
else if month = 9 then (((3287 + 243 + day) mod 7) + 4) mod 7
else if month = 10 then (((3287 + 273 + day) mod 7) + 4) mod 7
else if month = 11 then (((3287 + 304 + day) mod 7) + 4) mod 7
else (((3287 + 334 + day) mod 7) + 4) mod 7
)
答案 0 :(得分:3)
我相信你的问题就在这一行:
if day > 28 then failwith "invalid day"
没有else
部分。缺少其他部分相当于()
,这会导致输入问题。
实际上编译器是对的。事实上,这个代码实际上会在2月29日以外的几天内返回()非闰年,在我看来。
一种可能的解决方法是将代码部分更改为:
else if is_leap_year year != true && month = 2 && day > 28 then failwith "invalid day"
else . . .
作为旁注,我认为is_leap_year != true
更明确地表达为not is_leap_year
。
(正如另一方评论,Jeff Mercado是正确的。你永远不会在一个真实的项目中编写这样的代码。有很多方法可以做得更好。)