Sml 如何通过给出一年中的两天来获得月份列表?
我有一个名为Sml 如何通过给出一年中的两天来获得月份列表?,sml,Sml,我有一个名为month\u range的函数,它将一年中的两天作为输入(例如65天和128天,假设一年有365天),并返回一个列表,其中包含从第1天到第2天的天数所属的月份数 列表的大小必须为“第2天-第1天+1” 示例:月份范围(25,35)应返回:[1,1,1,1,1,1,1,2,2,2] 我编写了以下代码 fun month_range (day1:int,day2:int) = let val month_days= [31, 28, 31, 30, 31, 30,
month\u range
的函数,它将一年中的两天作为输入(例如65天和128天,假设一年有365天),并返回一个列表,其中包含从第1天到第2天的天数所属的月份数
列表的大小必须为“第2天-第1天+1”
示例:月份范围(25,35)应返回:[1,1,1,1,1,1,1,2,2,2]
我编写了以下代码
fun month_range (day1:int,day2:int) =
let
val month_days= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
fun what_month(day :int) =
let
fun aux(sum :int, numbers: int list) =
let
val numbers_tail = tl numbers
in
if sum <= (hd numbers)
then 1
else
1 + aux(sum, (hd numbers + hd numbers_tail)
:: (tl numbers_tail))
end
in
aux(day, month_days)
end
in
if (day1>day2)
then []
else what_month(day1) @ what_month(day1 + 1)@::what_month(day2)
end
fun month\u范围(第1天:整数,第2天:整数)=
允许
val月日=[31,28,31,30,31,30,30,31,30,31,31,31,31];
乐趣何月(日:整数)=
允许
乐趣辅助(总和:整数,数字:整数列表)=
允许
val编号=tl编号
在里面
如果总和(第2天)
然后[]
否则什么月(第1天)@什么月(第1天+1天)@::什么月(第2天)
终止
但它给了我以下的错误
/tmp/emacs-region5156f3r:21.51-21.54 Error: unbound variable or constructor: @::
/tmp/emacs-region5156f3r:21.12-21.70 Error: operator is not a function [literal]
operator: int
in expression:
(what_month (day1 + 1)) <errorvar>
/tmp/emacs-region5156f3r:21.12-21.70 Error: operator and operand don't agree [literal]
operator domain: 'Z list * 'Z list
operand: int * _
in expression:
what_month day1 @ (((what_month <exp>) <errorvar>) what_month) day2
uncaught exception Error
raised at: ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
../compiler/TopLevel/interact/evalloop.sml:44.55
../compiler/TopLevel/interact/evalloop.sml:296.17-296.20
/tmp/emacs-region5156f3r:21.51-21.54错误:未绑定变量或构造函数:@:
/tmp/emacs-region5156f3r:21.12-21.70错误:运算符不是函数[文字]
接线员:int
在表达上:
(哪个月(第1天+1天))
/tmp/emacs-region5156f3r:21.12-21.70错误:运算符和操作数不一致[文字]
操作员域:“Z列表*”Z列表
操作数:整数*_
在表达上:
what_month day 1@(((what_month))what_month)day 2
未捕获异常错误
在以下位置引发:../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
../compiler/TopLevel/interact/evalloop.sml:44.55
../compiler/TopLevel/interact/evalloop.sml:296.17-296.20
首先,您可以从错误消息中看到,您使用的是@:
,这没有任何意义
您很可能已将函数what_month
用于另一个赋值,因此没有理由将其放入let表达式中
现在,如果我们稍微简化一下您的代码
val month_days= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
fun what_month(day :int) =
let
fun aux(sum :int, numbers: int list) =
let
val numbers_tail = tl numbers
in
if sum <= (hd numbers)
then 1
else
1 + aux(sum, (hd numbers + hd numbers_tail)::(tl numbers_tail))
end
in
aux(day, month_days)
end
fun month_range (day1:int,day2:int) =
if (day1>day2)
then []
else what_month(day1)
由于返回空列表,当达到条件day1>day2
时,递归将给出如下结果
what_month(day1) :: what_month(day1 + 1) :: what_month(day1 + 1 + 1) :: ...
:: what_month(day2) ::[]
fun month_range (day1:int,day2:int) =
if (day1>day2)
then []
else what_month(day1) :: month_range(day1 + 1, day2)
- month_range(25,35);
val it = [1,1,1,1,1,1,1,2,2,2,2] : int list
生成的代码如下所示
what_month(day1) :: what_month(day1 + 1) :: what_month(day1 + 1 + 1) :: ...
:: what_month(day2) ::[]
fun month_range (day1:int,day2:int) =
if (day1>day2)
then []
else what_month(day1) :: month_range(day1 + 1, day2)
- month_range(25,35);
val it = [1,1,1,1,1,1,1,2,2,2,2] : int list
请在添加代码时格式化代码,并始终输入错误消息。。。我会的。